-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathadd.go
217 lines (195 loc) · 7.05 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-License-Identifier: Apache-2.0
package secret
import (
"fmt"
"slices"
"github.com/urfave/cli/v2"
"github.com/go-vela/cli/action"
"github.com/go-vela/cli/action/secret"
"github.com/go-vela/cli/internal"
"github.com/go-vela/cli/internal/client"
"github.com/go-vela/cli/internal/output"
"github.com/go-vela/types/constants"
)
// CommandAdd defines the command for creating a secret.
var CommandAdd = &cli.Command{
Name: "secret",
Description: "Use this command to create a secret.",
Usage: "Add a new secret from the provided configuration",
Action: add,
Flags: []cli.Flag{
// Repo Flags
&cli.StringFlag{
EnvVars: []string{"VELA_ORG", "SECRET_ORG"},
Name: internal.FlagOrg,
Aliases: []string{"o"},
Usage: "provide the organization for the secret",
},
&cli.StringFlag{
EnvVars: []string{"VELA_REPO", "SECRET_REPO"},
Name: internal.FlagRepo,
Aliases: []string{"r"},
Usage: "provide the repository for the secret",
},
// Secret Flags
&cli.StringFlag{
EnvVars: []string{"VELA_ENGINE", "SECRET_ENGINE"},
Name: internal.FlagSecretEngine,
Aliases: []string{"e"},
Usage: "provide the engine that stores the secret",
Value: constants.DriverNative,
},
&cli.StringFlag{
EnvVars: []string{"VELA_TYPE", "SECRET_TYPE"},
Name: internal.FlagSecretType,
Aliases: []string{"ty"},
Usage: "provide the type of secret being stored",
Value: constants.SecretRepo,
},
&cli.StringFlag{
EnvVars: []string{"VELA_TEAM", "SECRET_TEAM"},
Name: "team",
Aliases: []string{"t"},
Usage: "provide the team for the secret",
},
&cli.StringFlag{
EnvVars: []string{"VELA_NAME", "SECRET_NAME"},
Name: "name",
Aliases: []string{"n"},
Usage: "provide the name of the secret",
},
&cli.StringFlag{
EnvVars: []string{"VELA_VALUE", "SECRET_VALUE"},
Name: "value",
Aliases: []string{"v"},
Usage: "provide the value for the secret",
},
&cli.StringSliceFlag{
EnvVars: []string{"VELA_IMAGES", "SECRET_IMAGES"},
Name: "image",
Aliases: []string{"i"},
Usage: "Provide the image(s) that can access this secret",
},
&cli.StringSliceFlag{
EnvVars: []string{"VELA_EVENTS", "SECRET_EVENTS"},
Name: "event",
Aliases: []string{"events", "ev"},
Usage: "provide the event(s) that can access this secret",
},
&cli.StringFlag{
EnvVars: []string{"VELA_COMMAND", "SECRET_COMMAND"},
Name: internal.FlagSecretCommands,
Aliases: []string{"c"},
Usage: "enable a secret to be used for a step with commands (default is false for shared secrets)",
Value: "true",
},
&cli.StringFlag{
EnvVars: []string{"VELA_SUBSTITUTION", "SECRET_SUBSTITUTION"},
Name: internal.FlagSecretSubstitution,
Aliases: []string{"s"},
Usage: "enable a secret to be substituted (default is false for shared secrets)",
Value: "true",
},
&cli.StringFlag{
EnvVars: []string{"VELA_FILE", "SECRET_FILE"},
Name: "file",
Aliases: []string{"f"},
Usage: "provide a file to add the secret(s)",
},
// Output Flags
&cli.StringFlag{
EnvVars: []string{"VELA_OUTPUT", "SECRET_OUTPUT"},
Name: internal.FlagOutput,
Aliases: []string{"op"},
Usage: "format the output in json, spew or yaml",
},
},
CustomHelpTemplate: fmt.Sprintf(`%s
EXAMPLES:
1. Add a repository secret.
$ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar
2. Add a repository secret and disallow usage in commands.
$ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --commands false
3. Add an organization secret.
$ {{.HelpName}} --secret.engine native --secret.type org --org MyOrg --name foo --value bar
4. Add a shared secret.
$ {{.HelpName}} --secret.engine native --secret.type shared --org MyOrg --team octokitties --name foo --value bar
5. Add a repository secret with all event types enabled.
$ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --event comment --event deployment --event pull_request --event push --event tag
6. Add a repository secret with an image whitelist.
$ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --image alpine --image golang:* --image postgres:latest
7. Add a secret with value from a file.
$ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value @secret.txt
8. Add a repository secret with json output.
$ {{.HelpName}} --secret.engine native --secret.type repo --org MyOrg --repo MyRepo --name foo --value bar --output json
9. Add a secret or secrets from a file.
$ {{.HelpName}} --file secret.yml
10. Add a secret when config or environment variables are set.
$ {{.HelpName}} --org MyOrg --repo MyRepo --name foo --value bar
DOCUMENTATION:
https://go-vela.github.io/docs/reference/cli/secret/add/
`, cli.CommandHelpTemplate),
}
// helper function to capture the provided input
// and create the object used to create a secret.
//
func add(c *cli.Context) error {
// load variables from the config file
err := action.Load(c)
if err != nil {
return err
}
// parse the Vela client from the context
//
// https://pkg.go.dev/github.com/go-vela/cli/internal/client?tab=doc#Parse
client, err := client.Parse(c)
if err != nil {
return err
}
// create the secret configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config
s := &secret.Config{
Action: internal.ActionAdd,
Engine: c.String(internal.FlagSecretEngine),
Type: c.String(internal.FlagSecretType),
Org: c.String(internal.FlagOrg),
Repo: c.String(internal.FlagRepo),
Team: c.String("team"),
Name: c.String("name"),
Value: c.String("value"),
Images: c.StringSlice("image"),
AllowEvents: c.StringSlice("event"),
File: c.String("file"),
Output: c.String(internal.FlagOutput),
Color: output.ColorOptionsFromCLIContext(c),
}
// check if allow_command and allow_substitution are provided
// if they are not, server will not update the fields
if slices.Contains(c.FlagNames(), internal.FlagSecretCommands) {
val := c.Bool(internal.FlagSecretCommands)
s.AllowCommand = &val
}
if slices.Contains(c.FlagNames(), internal.FlagSecretSubstitution) {
val := c.Bool(internal.FlagSecretSubstitution)
s.AllowSubstitution = &val
}
// validate secret configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config.Validate
err = s.Validate()
if err != nil {
return err
}
// check if secret file is provided
if len(s.File) > 0 {
// execute the add from file call for the secret configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config.AddFromFile
return s.AddFromFile(client)
}
// execute the add call for the secret configuration
//
// https://pkg.go.dev/github.com/go-vela/cli/action/secret?tab=doc#Config.Add
return s.Add(client)
}