/
create.go
77 lines (63 loc) · 1.65 KB
/
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package config
import (
"errors"
"fmt"
"io/ioutil"
"github.com/appcelerator/amp/api/rpc/config"
"github.com/appcelerator/amp/cli"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"google.golang.org/grpc/status"
)
type CreateOpts struct {
Labels []string
}
var createOpts = &CreateOpts{
Labels: []string{},
}
// NewCreateCommand returns a new instance of the create command for creating a config.
func NewCreateCommand(c cli.Interface) *cobra.Command {
cmd := &cobra.Command{
Use: "create [OPTIONS] CONFIG FILE|-",
Short: "Create a config from a file or STDIN as content",
PreRunE: cli.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
return create(c, cmd, args)
},
}
flags := cmd.Flags()
flags.StringSliceVarP(&createOpts.Labels, "labels", "l", []string{}, "Config labels")
return cmd
}
func create(c cli.Interface, cmd *cobra.Command, args []string) error {
name := args[0]
source := args[1]
var data []byte
var err error
if source == "-" {
data, err = ioutil.ReadAll(c.In())
if err != nil {
return fmt.Errorf("error reading content from STDIN: %s", err.Error())
}
} else {
data, err = ioutil.ReadFile(source)
if err != nil {
return fmt.Errorf("error reading from file '%s': %s", source, err.Error())
}
}
conn := c.ClientConn()
client := config.NewConfigClient(conn)
request := &config.CreateRequest{
Name: name,
Data: data,
}
reply, err := client.ConfigCreate(context.Background(), request)
if err != nil {
if s, ok := status.FromError(err); ok {
return errors.New(s.Message())
}
return fmt.Errorf("error creating config: %s", err)
}
fmt.Println(reply.GetId())
return nil
}