-
Notifications
You must be signed in to change notification settings - Fork 12
/
update.go
156 lines (125 loc) · 4.26 KB
/
update.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
// SPDX-License-Identifier: Apache-2.0
package config
import (
"strings"
"github.com/go-vela/cli/internal"
"github.com/spf13/afero"
yaml "gopkg.in/yaml.v3"
"github.com/sirupsen/logrus"
)
// Update modifies one or more fields from the config file based off the provided configuration.
func (c *Config) Update() error {
logrus.Debug("executing update for config file configuration")
// use custom filesystem which enables us to test
//
// https://pkg.go.dev/github.com/spf13/afero?tab=doc#Afero
a := &afero.Afero{
Fs: appFS,
}
logrus.Tracef("reading content from %s", c.File)
// send Filesystem call to read config file
//
// https://pkg.go.dev/github.com/spf13/afero?tab=doc#Afero.ReadFile
data, err := a.ReadFile(c.File)
if err != nil {
return err
}
// create the config file object
//
// https://pkg.go.dev/github.com/go-vela/cli/action/config?tab=doc#ConfigFile
config := new(ConfigFile)
// update the config object with the current content
//
// https://pkg.go.dev/gopkg.in/yaml.v3?tab=doc#Unmarshal
err = yaml.Unmarshal(data, config)
if err != nil {
return err
}
// iterate through all flags to be modified
for key, value := range c.UpdateFlags {
logrus.Tracef("updating key %s with value %s", key, value)
// check if API address flag should be modified
if strings.EqualFold(key, internal.FlagAPIAddress) {
// set the API address field to value provided
config.API.Address = value
}
// check if API token flag should be modified
if strings.EqualFold(key, internal.FlagAPIToken) {
// set the API token field to value provided
config.API.Token = value
}
// check if API access token flag should be modified
if strings.EqualFold(key, internal.FlagAPIAccessToken) {
// set the API access token field to value provided
config.API.AccessToken = value
}
// check if API refresh token flag should be modified
if strings.EqualFold(key, internal.FlagAPIRefreshToken) {
// set the API refresh token field to value provided
config.API.RefreshToken = value
}
// check if API version flag should be modified
if strings.EqualFold(key, internal.FlagAPIVersion) {
// set the API version field to value provided
config.API.Version = value
}
// check if log level flag should be modified
if strings.EqualFold(key, internal.FlagLogLevel) {
// set the log level field to value provided
config.Log.Level = value
}
// check if no git flag should be modified
if strings.EqualFold(key, internal.FlagNoGit) {
// set the no git field to value provided
config.NoGit = value
}
// check if secret engine flag should be modified
if strings.EqualFold(key, internal.FlagSecretEngine) {
// set the secret engine field to value provided
config.Secret.Engine = value
}
// check if secret type flag should be modified
if strings.EqualFold(key, internal.FlagSecretType) {
// set the secret type field to value provided
config.Secret.Type = value
}
// check if compiler github token flag should be modified
if strings.EqualFold(key, internal.FlagCompilerGitHubToken) {
// set the compiler github token field to value provided
config.Compiler.GitHub.Token = value
}
// check if compiler github url flag should be modified
if strings.EqualFold(key, internal.FlagCompilerGitHubURL) {
// set the compiler github url field to value provided
config.Compiler.GitHub.URL = value
}
// check if org flag should be modified
if strings.EqualFold(key, internal.FlagOrg) {
// set the org field to value provided
config.Org = value
}
// check if repo flag should be modified
if strings.EqualFold(key, internal.FlagRepo) {
// set the repo field to value provided
config.Repo = value
}
// check if output flag should be modified
if strings.EqualFold(key, internal.FlagOutput) {
// set the output field to value provided
config.Output = value
}
}
logrus.Trace("creating file content for config file")
// create output for config file
//
// https://pkg.go.dev/gopkg.in/yaml.v3?tab=doc#Marshal
out, err := yaml.Marshal(config)
if err != nil {
return err
}
logrus.Tracef("writing file content to %s", c.File)
// send Filesystem call to create config file
//
// https://pkg.go.dev/github.com/spf13/afero?tab=doc#Afero.WriteFile
return a.WriteFile(c.File, out, 0600)
}