-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
config_remove.go
59 lines (51 loc) · 1.47 KB
/
config_remove.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
package cmd
import (
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/cmd/keyring"
"go.uber.org/zap"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/spf13/cobra"
)
func init() {
configCmd.AddCommand(configRemoveCmd)
}
var configRemoveCmd = &cobra.Command{
Use: "remove",
Short: i18n.T("Remove a Jenkins config"),
Long: i18n.T("Remove a Jenkins config"),
Args: cobra.MinimumNArgs(1),
ValidArgsFunction: ValidJenkinsNames,
RunE: func(_ *cobra.Command, args []string) error {
target := args[0]
return removeJenkins(target)
},
}
func removeJenkins(name string) (err error) {
current := getCurrentJenkins()
if name == current.Name {
err = fmt.Errorf("you cannot remove current Jenkins, if you want to remove it, can select other items before")
return
}
index := -1
config := getConfig()
for i, jenkins := range config.JenkinsServers {
if name == jenkins.Name {
index = i
break
}
}
if index == -1 {
err = fmt.Errorf("cannot found by name %s", name)
} else {
targetJenkins := config.JenkinsServers[index]
if err = keyring.DelToken(targetJenkins); err == nil {
logger.Info("delete keyring item successfully", zap.String("name", targetJenkins.Name))
} else {
logger.Error("cannot delete keyring item", zap.String("name", targetJenkins.Name),
zap.Error(err))
}
config.JenkinsServers = append(config.JenkinsServers[:index], config.JenkinsServers[index+1:]...)
err = saveConfig()
}
return
}