-
Notifications
You must be signed in to change notification settings - Fork 0
/
add.go
179 lines (143 loc) · 4.54 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
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package profile
import (
"errors"
"fmt"
"net/http"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"bunnyshell.com/cli/pkg/lib"
"bunnyshell.com/cli/pkg/util"
"bunnyshell.com/sdk"
)
func init() {
var profile lib.Profile
var profileName string
var addProfileCommand = &cobra.Command{
Use: "add name [token organization project]",
PreRunE: func(cmd *cobra.Command, args []string) error {
var err error
if profileName == "" {
profileName, err = util.Ask("Name:", getProfileNameValidator())
} else {
err = getProfileNameValidator()(profileName)
}
if err != nil {
return err
}
lib.CLIContext.Timeout = 0 * time.Second
for {
if err := ensureToken(&profile); err != nil {
return err
}
organizations, r, err := getOrganizations(profile)
if err != nil {
lib.FormatCommandError(cmd, err)
profile.Token = ""
continue
}
if organizations.Embedded == nil || len(organizations.Embedded.Item) == 0 {
return fmt.Errorf("create an organization in: %s", r.Request.Host)
}
ok, err := util.Confirm("Set a default organization")
if err != nil {
return err
}
if ok {
if err := setOrganization(&profile, organizations.Embedded.Item); err != nil {
return err
}
}
break
}
err = lib.AddProfile(profile, profileName)
if err != nil {
return lib.FormatCommandError(cmd, err)
}
if err := viper.WriteConfig(); err != nil {
return lib.FormatCommandError(cmd, err)
}
return nil
},
Run: func(cmd *cobra.Command, args []string) {
if lib.CLIContext.Verbosity != 0 {
lib.FormatCommandData(cmd, map[string]interface{}{
"message": "Saved config file",
"data": viper.ConfigFileUsed(),
})
}
ok, err := util.Confirm("Set as default profile?")
if err != nil {
cmd.PrintErr("Could not determine user input", err)
return
}
if !ok {
return
}
root := mainCmd.Root()
root.SetArgs([]string{"configure", "profiles", "default", "--name", profileName})
root.Execute()
},
}
addProfileCommand.Flags().StringVar(&profile.Host, "host", profile.Host, "Host for the new profile")
addProfileCommand.Flags().StringVar(&profile.Token, "token", profile.Token, "Token for the new profile")
addProfileCommand.Flags().StringVar(&profile.Context.Organization, "organization", profile.Context.Organization, "AutoFilter on Organization")
addProfileCommand.Flags().StringVar(&profile.Context.Project, "project", profile.Context.Project, "AutoFilter on Project")
addProfileCommand.Flags().StringVar(&profile.Context.Environment, "environment", profile.Context.Environment, "AutoFilter on Enviroment")
addProfileCommand.Flags().StringVar(&profile.Context.ServiceComponent, "serviceComponent", profile.Context.ServiceComponent, "AutoFilter on ServiceComponent")
addProfileCommand.Flags().StringVar(&profileName, "name", profileName, "Unique name for the new profile")
mainCmd.AddCommand(addProfileCommand)
}
func getProfileNameValidator() func(interface{}) error {
return util.All(
util.Lowercase(),
util.AssertMinimumLength(4),
)
}
func ensureToken(profile *lib.Profile) error {
if profile.Token != "" {
return nil
}
token, err := util.AskSecretWithHelp("Token:", "Get yours from: https://environments.bunnyshell.com/access-token", validateToken)
if err != nil {
return err
}
profile.Token = token
return nil
}
func setOrganization(profile *lib.Profile, organizations []sdk.OrganizationCollection) error {
if profile.Context.Organization != "" {
return nil
}
index, _, err := util.Choose("Select Organization", getOrganizationNames(organizations))
profile.Context.Organization = *organizations[index].Id
return err
}
func getOrganizationNames(organizations []sdk.OrganizationCollection) []string {
var result []string
for _, organization := range organizations {
result = append(result, *organization.Name)
}
return result
}
func validateToken(input interface{}) error {
chunks := strings.Split(input.(string), ":")
if len(chunks) != 2 {
return errors.New("invalid token detected")
}
if len(chunks[1]) != 32 {
return errors.New("invalid token detected")
}
if _, err := strconv.Atoi(chunks[0]); err != nil {
return errors.New("invalid token detected")
}
return nil
}
func getOrganizations(profile lib.Profile) (*sdk.PaginatedOrganizationCollection, *http.Response, error) {
ctx, cancel := lib.GetContextFromProfile(profile)
defer cancel()
request := lib.GetApiFromProfile(profile).OrganizationApi.OrganizationList(ctx)
return request.Execute()
}