-
Notifications
You must be signed in to change notification settings - Fork 785
/
edit.go
140 lines (122 loc) · 3.61 KB
/
edit.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
package edit
import (
"fmt"
"reflect"
"strconv"
"github.com/jenkins-x/jx/v2/pkg/cmd/edit/requirements"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/spf13/cobra"
v1 "github.com/jenkins-x/jx-api/pkg/apis/jenkins.io/v1"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/jenkins-x/jx/v2/pkg/util"
)
// EditOptions contains the CLI options
type EditOptions struct {
*opts.CommonOptions
}
var (
exit_long = templates.LongDesc(`
Edit a resource
`)
exit_example = templates.Examples(`
# Lets edit the staging Environment
jx edit env staging
`)
)
// NewCmdEdit creates the edit command
func NewCmdEdit(commonOpts *opts.CommonOptions) *cobra.Command {
options := &EditOptions{
commonOpts,
}
cmd := &cobra.Command{
Use: "edit [flags]",
Short: "Edit a resource",
Long: exit_long,
Example: exit_example,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
SuggestFor: []string{"modify"},
}
cmd.AddCommand(NewCmdEditAddon(commonOpts))
cmd.AddCommand(NewCmdEditAppJenkinsPlugins(commonOpts))
cmd.AddCommand(NewCmdEditBuildpack(commonOpts))
cmd.AddCommand(NewCmdEditConfig(commonOpts))
cmd.AddCommand(NewCmdEditDeployKind(commonOpts))
cmd.AddCommand(NewCmdEditEnv(commonOpts))
cmd.AddCommand(NewCmdEditHelmBin(commonOpts))
cmd.AddCommand(requirements.NewCmdEditRequirements(commonOpts))
cmd.AddCommand(NewCmdEditStorage(commonOpts))
cmd.AddCommand(NewCmdEditUserRole(commonOpts))
err := addTeamSettingsCommandsFromTags(cmd, options)
helper.CheckErr(err)
return cmd
}
// Run implements this command
func (o *EditOptions) Run() error {
return o.Cmd.Help()
}
func addTeamSettingsCommandsFromTags(baseCmd *cobra.Command, options *EditOptions) error {
teamSettings := &v1.TeamSettings{}
value := reflect.ValueOf(teamSettings).Elem()
t := value.Type()
for i := 0; i < value.NumField(); i++ {
field := value.Field(i)
structField := t.Field(i)
tag := structField.Tag
command, ok := tag.Lookup("command")
if !ok {
continue
}
commandUsage, ok := tag.Lookup("commandUsage")
if !ok {
continue
}
cmd := &cobra.Command{
Use: command,
Short: commandUsage,
Run: func(cmd *cobra.Command, args []string) {
var value interface{}
var err error
if len(args) > 0 {
if structField.Type.String() == "string" {
value = args[0]
} else if structField.Type.String() == "bool" {
value, err = strconv.ParseBool(args[0])
helper.CheckErr(err)
}
} else if !options.BatchMode {
var err error
if structField.Type.String() == "string" {
value, err = util.PickValue(commandUsage+":", field.String(), true, "", options.GetIOFileHandles())
} else if structField.Type.String() == "bool" {
value, err = util.Confirm(commandUsage+":", field.Bool(), "", options.GetIOFileHandles())
}
helper.CheckErr(err)
} else {
helper.Fatal(fmt.Sprintf("No value to set %s", command), 1)
}
callback := func(env *v1.Environment) error {
teamSettings := &env.Spec.TeamSettings
valueField := reflect.ValueOf(teamSettings).Elem().FieldByName(structField.Name)
switch value.(type) {
case string:
valueField.SetString(value.(string))
case bool:
valueField.SetBool(value.(bool))
}
log.Logger().Infof("Setting the team %s to: %s", util.ColorInfo(command), util.ColorInfo(value))
return nil
}
helper.CheckErr(options.ModifyDevEnvironment(callback))
},
}
baseCmd.AddCommand(cmd)
}
return nil
}