/
services.go
164 lines (136 loc) · 3.84 KB
/
services.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cmd
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/urfave/cli"
"github.com/manifoldco/torus-cli/api"
"github.com/manifoldco/torus-cli/config"
"github.com/manifoldco/torus-cli/envelope"
"github.com/manifoldco/torus-cli/errs"
"github.com/manifoldco/torus-cli/identity"
"github.com/manifoldco/torus-cli/prompts"
"github.com/manifoldco/torus-cli/ui"
)
func init() {
services := cli.Command{
Name: "services",
Usage: "Manage services within an organization",
Category: "PROJECT STRUCTURE",
Subcommands: []cli.Command{
{
Name: "create",
Usage: "Create a service in an organization",
ArgsUsage: "[name]",
Flags: []cli.Flag{
orgFlag("Create the project in this org", false),
projectFlag("project to create services for", false),
},
Action: chain(
ensureDaemon, ensureSession, loadDirPrefs, loadPrefDefaults,
createServiceCmd,
),
},
{
Name: "list",
Usage: "List services for an organization",
Flags: []cli.Flag{
orgFlag("org to show services for", false),
projectFlag("project to show services for", false),
},
Action: chain(
ensureDaemon, ensureSession, loadDirPrefs, loadPrefDefaults,
setUserEnv, checkRequiredFlags, listServicesCmd,
),
},
},
}
Cmds = append(Cmds, services)
}
const serviceListFailed = "Could not list services."
func listServicesCmd(ctx *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return err
}
client := api.NewClient(cfg)
c := context.Background()
org, _, _, err := selectOrg(c, client, ctx.String("org"), false)
if err != nil {
return err
}
project, _, _, err := selectProject(c, client, org, ctx.String("project"), false)
if err != nil {
return err
}
// Retrieve services for targeted org and project
services, err := listServices(&c, client, org.ID, project.ID, nil, nil)
if err != nil {
return errs.NewErrorExitError(serviceListFailed, err)
}
// Build output of projects/envs
fmt.Println("")
fmt.Printf("%s\n", ui.BoldString("Services"))
for _, s := range services {
fmt.Printf("%s\n", s.Body.Name)
}
fmt.Printf("\nProject /%s/%s has (%s) service%s\n", org.Body.Name, project.Body.Name, ui.FaintString(strconv.Itoa(len(services))), plural(len(services)))
return nil
}
func listServices(ctx *context.Context, client *api.Client, orgID, projID *identity.ID, teamIDs []identity.ID, name *string) ([]envelope.Service, error) {
c, client, err := NewAPIClient(ctx, client)
if err != nil {
return nil, cli.NewExitError(serviceListFailed, -1)
}
var orgIDs []identity.ID
if orgID != nil {
orgIDs = []identity.ID{*orgID}
}
var projectIDs []identity.ID
if projID != nil {
projectIDs = []identity.ID{*projID}
}
var names []string
if name != nil {
names = []string{*name}
}
return client.Services.List(c, orgIDs, projectIDs, nil, names)
}
const serviceCreateFailed = "Could not create service."
func createServiceCmd(ctx *cli.Context) error {
cfg, err := config.LoadConfig()
if err != nil {
return errs.NewExitError(serviceCreateFailed)
}
args := ctx.Args()
serviceName := ""
if len(args) > 0 {
serviceName = args[0]
}
client := api.NewClient(cfg)
c := context.Background()
org, _, _, err := selectOrg(c, client, ctx.String("org"), false)
if err != nil {
return err
}
project, _, _, err := selectProject(c, client, org, ctx.String("project"), false)
if err != nil {
return err
}
serviceName, err = prompts.ServiceName(serviceName, true)
if err != nil {
return err
}
// Create our new service
fmt.Println("")
err = client.Services.Create(c, org.ID, project.ID, serviceName)
if err != nil {
if strings.Contains(err.Error(), "resource exists") {
return errs.NewExitError("Service already exists")
}
return errs.NewErrorExitError(serviceCreateFailed, err)
}
fmt.Printf("Service %s created.\n", serviceName)
return nil
}