-
Notifications
You must be signed in to change notification settings - Fork 786
/
create_variable.go
229 lines (201 loc) · 6.16 KB
/
create_variable.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package create
import (
"fmt"
"path/filepath"
"github.com/jenkins-x/jx/v2/pkg/cmd/create/options"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"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/config"
"github.com/jenkins-x/jx/v2/pkg/jenkinsfile"
"github.com/jenkins-x/jx/v2/pkg/jenkinsfile/gitresolver"
"github.com/jenkins-x/jx/v2/pkg/kube"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
createVariableLong = templates.LongDesc(`
Creates an environment variable in the Jenkins X Pipeline
`)
createVariableExample = templates.Examples(`
# Create a new environment variable with a name and value
jx create var -n CHEESE -v Edam
# Create a new environment variable with a name and ask the user for the value
jx create var -n CHEESE
# Overrides an environment variable from the build pack
jx create var
`)
)
// CreateVariableOptions the options for the create spring command
type CreateVariableOptions struct {
options.CreateOptions
Dir string
Name string
Value string
}
// NewCmdCreateVariable creates a command object for the "create" command
func NewCmdCreateVariable(commonOpts *opts.CommonOptions) *cobra.Command {
options := &CreateVariableOptions{
CreateOptions: options.CreateOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "variable",
Short: "Creates an environment variable in the Jenkins X Pipeline",
Aliases: []string{"var", "envvar"},
Long: createVariableLong,
Example: createVariableExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Name, "name", "n", "", "The name of the environment variable to set")
cmd.Flags().StringVarP(&options.Value, "value", "v", "", "The value of the environment variable to set")
cmd.Flags().StringVarP(&options.Dir, "dir", "d", "", "The root project directory. Defaults to the current dir")
return cmd
}
// Run implements the command
func (o *CreateVariableOptions) Run() error {
dir := o.Dir
var err error
if dir == "" {
dir, _, err := o.Git().FindGitConfigDir(o.Dir)
if err != nil {
return err
}
if dir == "" {
dir = "."
}
}
projectConfig, fileName, err := config.LoadProjectConfig(dir)
if err != nil {
return err
}
enrichedProjectConfig, _, err := config.LoadProjectConfig(dir)
if err != nil {
return err
}
name := o.Name
value := o.Value
if o.BatchMode {
if name == "" {
return util.MissingOption("name")
}
if value == "" {
return util.MissingOption("value")
}
}
defaultValues, err := o.loadEnvVars(enrichedProjectConfig)
if err != nil {
return err
}
keys := util.SortedMapKeys(defaultValues)
if name == "" {
message := "environment variable name: "
help := "the name of the environment variable which is usually upper case without spaces or dashes"
if len(keys) == 0 {
name, err = util.PickValue(message, "", true, help, o.GetIOFileHandles())
} else {
name, err = util.PickName(keys, message, help, o.GetIOFileHandles())
}
if err != nil {
return err
}
if name == "" {
return util.MissingOption("name")
}
}
if value == "" {
message := "environment variable value: "
value, err = util.PickValue(message, defaultValues[name], true, "", o.GetIOFileHandles())
if err != nil {
return err
}
if name == "" {
return util.MissingOption("name")
}
}
if projectConfig.PipelineConfig == nil {
projectConfig.PipelineConfig = &jenkinsfile.PipelineConfig{}
}
projectConfig.PipelineConfig.Env = kube.SetEnvVar(projectConfig.PipelineConfig.Env, name, value)
err = projectConfig.SaveConfig(fileName)
if err != nil {
return err
}
log.Logger().Infof("Updated Jenkins X Pipeline file: %s", util.ColorInfo(fileName))
return nil
}
func (o *CreateVariableOptions) loadEnvVars(projectConfig *config.ProjectConfig) (map[string]string, error) {
answer := map[string]string{}
teamSettings, err := o.TeamSettings()
if err != nil {
return answer, err
}
packsDir, err := gitresolver.InitBuildPack(o.Git(), teamSettings.BuildPackURL, teamSettings.BuildPackRef)
if err != nil {
return answer, err
}
resolver, err := gitresolver.CreateResolver(packsDir, o.Git())
if err != nil {
return answer, err
}
name := projectConfig.BuildPack
if name == "" {
name, err = o.DiscoverBuildPack(o.Dir, projectConfig, name)
if err != nil {
return answer, err
}
}
if projectConfig.PipelineConfig == nil {
projectConfig.PipelineConfig = &jenkinsfile.PipelineConfig{}
}
pipelineConfig := projectConfig.PipelineConfig
if name != "none" {
packDir := filepath.Join(packsDir, name)
pipelineFile := filepath.Join(packDir, jenkinsfile.PipelineConfigFileName)
exists, err := util.FileExists(pipelineFile)
if err != nil {
return answer, errors.Wrapf(err, "failed to find build pack pipeline YAML: %s", pipelineFile)
}
if !exists {
return answer, fmt.Errorf("no build pack for %s exists at directory %s", name, packDir)
}
buildPackPipelineConfig, err := jenkinsfile.LoadPipelineConfig(pipelineFile, resolver, true, false)
if err != nil {
return answer, errors.Wrapf(err, "failed to load build pack pipeline YAML: %s", pipelineFile)
}
err = pipelineConfig.ExtendPipeline(buildPackPipelineConfig, false)
if err != nil {
return answer, errors.Wrapf(err, "failed to override PipelineConfig using configuration in file %s", pipelineFile)
}
}
kubeClient, ns, err := o.KubeClientAndDevNamespace()
if err != nil {
return answer, err
}
answer = pipelineConfig.GetAllEnvVars()
podTemplates, err := kube.LoadPodTemplates(kubeClient, ns)
if err != nil {
return answer, err
}
containerName := pipelineConfig.Agent.GetImage()
if containerName != "" && podTemplates != nil && podTemplates[containerName] != nil {
podTemplate := podTemplates[containerName]
if len(podTemplate.Spec.Containers) > 0 {
container := podTemplate.Spec.Containers[0]
for _, env := range container.Env {
if env.Value != "" || answer[env.Name] == "" {
answer[env.Name] = env.Value
}
}
}
}
return answer, nil
}