-
Notifications
You must be signed in to change notification settings - Fork 20
/
config_delete.go
63 lines (52 loc) · 1.34 KB
/
config_delete.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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// deleteCmd represents the delete command
var configDeleteCmd = &cobra.Command{
Use: "delete <account name>",
Short: "Delete an account from config file",
Aliases: gDeleteAlias,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return cmd.Usage()
}
if gAllAccount == nil {
return fmt.Errorf("no accounts defined")
}
if !doesAccountExist(args[0]) {
return fmt.Errorf("account %q doesn't exist", args[0])
}
if args[0] == gAllAccount.DefaultAccount {
return fmt.Errorf("cannot delete the default account")
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
if !force {
if !askQuestion(fmt.Sprintf("Do you really want to delete the config for %q?", args[0])) {
return nil
}
}
pos := 0
for i, acc := range gAllAccount.Accounts {
if acc.Name == args[0] {
pos = i
break
}
}
gAllAccount.Accounts = append(gAllAccount.Accounts[:pos], gAllAccount.Accounts[pos+1:]...)
if err := addAccount(viper.ConfigFileUsed(), nil); err != nil {
return err
}
println(args[0])
return nil
},
}
func init() {
configDeleteCmd.Flags().BoolP("force", "f", false, "Attempt to remove an account without prompting for confirmation")
configCmd.AddCommand(configDeleteCmd)
}