Skip to content

Commit f60a6aa

Browse files
authored
Add support update config item token (#547)
1 parent 0539c06 commit f60a6aa

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

app/cmd/config.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ func init() {
4040
flags.BoolVarP(&configOptions.Decrypt, "decrypt", "", false,
4141
`Decrypt the credential field`)
4242

43-
configCmd.AddCommand(cmdCfg.NewConfigPluginCmd(&configOptions.Option))
43+
configCmd.AddCommand(cmdCfg.NewConfigPluginCmd(&configOptions.Option),
44+
createConfigUpdateCmd())
4445
}
4546

4647
var configCmd = &cobra.Command{

app/cmd/config_update.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
type configUpdateOption struct {
9+
name string
10+
token string
11+
}
12+
13+
func createConfigUpdateCmd() (cmd *cobra.Command) {
14+
opt := &configUpdateOption{}
15+
16+
cmd = &cobra.Command{
17+
Use: "update",
18+
Aliases: []string{"up"},
19+
Short: "Update a Jenkins config",
20+
Example: "jcli config update --token",
21+
PreRunE: opt.preRunE,
22+
ValidArgsFunction: ValidJenkinsNames,
23+
RunE: opt.runE,
24+
}
25+
26+
flags := cmd.Flags()
27+
flags.StringVarP(&opt.token, "token", "", "",
28+
"The token of Jenkins config item")
29+
return
30+
}
31+
32+
func (o *configUpdateOption) preRunE(_ *cobra.Command, args []string) (err error) {
33+
if o.token == "" {
34+
err = fmt.Errorf("no token provided")
35+
}
36+
37+
if len(args) > 0 {
38+
o.name = args[0]
39+
}
40+
return
41+
}
42+
43+
func (o *configUpdateOption) runE(_ *cobra.Command, _ []string) (err error) {
44+
found := false
45+
for i, cfg := range config.JenkinsServers {
46+
if cfg.Name == o.name {
47+
found = true
48+
config.JenkinsServers[i].Token = o.token
49+
err = saveConfig()
50+
break
51+
}
52+
}
53+
54+
if !found {
55+
err = fmt.Errorf("jenkins '%s' does not exist", o.name)
56+
}
57+
return
58+
}

0 commit comments

Comments
 (0)