forked from IBM/CodeEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cecli.go
66 lines (58 loc) · 1.8 KB
/
cecli.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strings"
)
// Just a util func that will run the command and if it fails then it'll
// panic. Not really the right thing to do in a real app, but it makes it
// really nice for our main() to just call Run() and not have to check for
// errors after each call.
func Run(out *string, cmdStr string, args ...interface{}) string {
log.Printf("> %s\n", fmt.Sprint(cmdStr))
cmd := exec.Command("/bin/sh", "-c", fmt.Sprintf(cmdStr, args...))
buf, err := cmd.CombinedOutput()
if err != nil {
buf = append(buf, []byte("\n"+err.Error()+"\n")...)
}
if out != nil {
*out = *out + fmt.Sprintf("> %s\n%s\n", cmdStr, string(buf))
}
log.Printf(string(buf) + "\nDone")
if err != nil {
panic(err)
}
return string(buf)
}
func main() {
// These are run during the start-up of the container so that
// we don't have the pay the cost of this on each HTTP request
Run(nil, "ibmcloud login -r "+os.Getenv("REGION")+" --apikey %s -g %s",
strings.TrimSpace(os.Getenv("APIKEY")),
strings.TrimSpace(os.Getenv("GROUP")))
Run(nil, "ibmcloud ce project select -n %s", os.Getenv("PROJECT"))
// Our HTTP handler func
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
result := ""
// Just make sure the user asked for us to list the App
if r.URL.Path != "/list" {
return
}
defer func() {
// On any Run() error (ie panic), return 500 to client
if r := recover(); r != nil {
w.WriteHeader(http.StatusInternalServerError)
}
// Either way, print the output of the Runs
fmt.Fprint(w, result)
}()
// List of commands to run - can be any command, not just CE cmds
Run(&result, "ibmcloud ce project current")
Run(&result, "ibmcloud ce app list")
})
fmt.Printf("Listening on port 8080\n")
http.ListenAndServe(":8080", nil)
}