/
add.go
87 lines (81 loc) · 2.22 KB
/
add.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
78
79
80
81
82
83
84
85
86
87
package resource
import (
"errors"
"strings"
"github.com/appcelerator/amp/api/rpc/resource"
"github.com/appcelerator/amp/cli"
"github.com/appcelerator/amp/data/accounts"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"google.golang.org/grpc/status"
)
type addTeamResOptions struct {
org string
team string
}
// NewAddTeamResCommand returns a new instance of the add team resource command.
func NewAddTeamResCommand(c cli.Interface) *cobra.Command {
opts := addTeamResOptions{}
cmd := &cobra.Command{
Use: "add [OPTIONS] RESOURCE(S)",
Short: "Add one or more resources",
PreRunE: cli.AtLeastArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return addTeamRes(c, cmd, args, opts)
},
}
flags := cmd.Flags()
//flags.StringVar(&opts.org, "org", "", "Organization name")
flags.StringVar(&opts.team, "team", "", "Team name")
return cmd
}
func addTeamRes(c cli.Interface, cmd *cobra.Command, args []string, opts addTeamResOptions) error {
var errs []string
opts.org = accounts.DefaultOrganization
//org, err := cli.ReadOrg(c.Server())
//if !cmd.Flag("org").Changed {
// switch {
// case err == nil:
// opts.org = org
// c.Console().Println("organization name:", opts.org)
// default:
// opts.org = c.Console().GetInput("organization name")
// }
//}
team, err := cli.ReadTeam(c.Server())
if !cmd.Flag("team").Changed {
switch {
case err == nil:
opts.team = team
c.Console().Println("team name:", opts.team)
default:
opts.team = c.Console().GetInput("team name")
}
}
conn := c.ClientConn()
client := resource.NewResourceClient(conn)
for _, res := range args {
request := &resource.AddToTeamRequest{
ResourceId: res,
OrganizationName: opts.org,
TeamName: opts.team,
}
if _, err := client.ResourceAddToTeam(context.Background(), request); err != nil {
if s, ok := status.FromError(err); ok {
errs = append(errs, s.Message())
continue
}
}
}
if len(errs) > 0 {
return errors.New(strings.Join(errs, "\n"))
}
//if err := cli.SaveOrg(opts.org, c.Server()); err != nil {
// return err
//}
if err := cli.SaveTeam(opts.team, c.Server()); err != nil {
return err
}
c.Console().Println("Resource(s) have been added to team.")
return nil
}