forked from jenkins-x/jx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathedit_addon.go
114 lines (97 loc) · 2.39 KB
/
edit_addon.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
package cmd
import (
"fmt"
"io"
"strings"
"github.com/jenkins-x/jx/pkg/addon"
"github.com/jenkins-x/jx/pkg/auth"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/spf13/cobra"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/util"
)
const (
optionEnabled = "enabled"
)
var (
editAddonLong = templates.LongDesc(`
Edits an addon
`)
editAddonExample = templates.Examples(`
# Enables or disbles an addon
jx edit addon
# Enables or disables an addon
jx edit addon -e true gitea
`)
)
// EditAddonOptions the options for the create spring command
type EditAddonOptions struct {
EditOptions
Name string
Enabled string
IssuesAuthConfigSvc auth.AuthConfigService
}
// NewCmdEditAddon creates a command object for the "create" command
func NewCmdEditAddon(f Factory, out io.Writer, errOut io.Writer) *cobra.Command {
options := &EditAddonOptions{
EditOptions: EditOptions{
CommonOptions: CommonOptions{
Factory: f,
Out: out,
Err: errOut,
},
},
}
cmd := &cobra.Command{
Use: "addon",
Short: "Edits the addon configuration",
Aliases: []string{"addons"},
Long: editAddonLong,
Example: editAddonExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Enabled, optionEnabled, "e", "", "Enables or disables the addon")
return cmd
}
// Run implements the command
func (o *EditAddonOptions) Run() error {
args := o.Args
if len(args) > 0 {
o.Name = args[0]
}
var err error
charts := kube.AddonCharts
names := util.SortedMapKeys(charts)
if o.Name == "" {
o.Name, err = util.PickName(names, "Pick the addon to configure")
if err != nil {
return err
}
if o.Name == "" {
return fmt.Errorf("No addon name chosen")
}
}
addonConfig, err := addon.LoadAddonsConfig()
if err != nil {
return err
}
config := addonConfig.GetOrCreate(o.Name)
if o.Enabled != "" {
text := strings.ToLower(o.Enabled)
value := false
if text == "true" {
value = true
} else if text != "false" {
return util.InvalidOption(optionEnabled, o.Enabled, []string{"true", "false"})
}
config.Enabled = value
} else {
config.Enabled = util.Confirm("Enable addon "+o.Name, config.Enabled, "If an addon is enabled it is installed when using 'jx create cluster' or 'jx install'")
}
return addonConfig.Save()
}