/
app_create.go
57 lines (53 loc) · 1.27 KB
/
app_create.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
package app
import (
"encoding/json"
"fmt"
"github.com/appbaseio/abc/appbase/common"
"github.com/appbaseio/abc/appbase/session"
"github.com/appbaseio/abc/appbase/spinner"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
type createdAppDetails struct {
ID int64 `json:"id"`
}
type createRespBody struct {
Body createdAppDetails `json:"body"`
}
// RunAppCreate runs app create command
func RunAppCreate(appName string, esVersion string, category string) error {
spinner.Start()
defer spinner.Stop()
// create app
body := fmt.Sprintf(`{"category": %s, "es_version": %s}`, category, esVersion)
req, err := http.NewRequest("PUT", common.AccAPIURL+"/app/"+appName, strings.NewReader(body))
if err != nil {
return err
}
resp, err := session.SendRequest(req)
if err != nil {
return err
}
spinner.Stop()
// status code not 200
if resp.StatusCode != 200 {
defer resp.Body.Close()
bodyBytes, _ := ioutil.ReadAll(resp.Body)
return fmt.Errorf("There was an error %s", string(bodyBytes))
}
// decode
var res createRespBody
dec := json.NewDecoder(resp.Body)
err = dec.Decode(&res)
if err != nil {
return err
}
// output
fmt.Printf("ID: %d\n", res.Body.ID)
fmt.Printf("Name: %s\n", appName)
// permissions
ShowAppPerms(strconv.FormatInt(res.Body.ID, 10))
return nil
}