-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathremove.go
182 lines (148 loc) · 4.99 KB
/
remove.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
// SPDX-License-Identifier: Apache-2.0
package config
import (
"strings"
"github.com/sirupsen/logrus"
"github.com/spf13/afero"
yaml "gopkg.in/yaml.v3"
"github.com/go-vela/cli/internal"
)
// Remove deletes one or more fields from the config file based off the provided configuration.
func (c *Config) Remove() error {
logrus.Debug("executing remove 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,
}
// check if remove flags are empty
if len(c.RemoveFlags) == 0 {
logrus.Tracef("removing config file %s", c.File)
// send Filesystem call to delete config file
//
// https://pkg.go.dev/github.com/spf13/afero?tab=doc#Afero.Remove
return a.Remove(c.File)
}
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 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 removed
for _, flag := range c.RemoveFlags {
logrus.Tracef("removing key %s", flag)
// check if API address flag should be removed
if strings.EqualFold(flag, internal.FlagAPIAddress) {
// set the API address field to empty in config
config.API.Address = ""
}
// check if API token flag should be removed
if strings.EqualFold(flag, internal.FlagAPIToken) {
// set the API token field to empty in config
config.API.Token = ""
}
// check if API access token flag should be removed
if strings.EqualFold(flag, internal.FlagAPIAccessToken) {
// set the API access token field to empty in config
config.API.AccessToken = ""
}
// check if API refresh token flag should be removed
if strings.EqualFold(flag, internal.FlagAPIRefreshToken) {
// set the API refresh token field to empty in config
config.API.RefreshToken = ""
}
// check if API version flag should be removed
if strings.EqualFold(flag, internal.FlagAPIVersion) {
// set the API version field to empty in config
config.API.Version = ""
}
// check if log level flag should be removed
if strings.EqualFold(flag, internal.FlagLogLevel) {
// set the log level field to empty in config
config.Log.Level = ""
}
// check if no git flag should be removed
if strings.EqualFold(flag, internal.FlagNoGit) {
// set the no git field to empty in config
config.NoGit = ""
}
// check if secret engine flag should be removed
if strings.EqualFold(flag, internal.FlagSecretEngine) {
// set the secret engine field to empty in config
config.Secret.Engine = ""
}
// check if secret type flag should be removed
if strings.EqualFold(flag, internal.FlagSecretType) {
// set the secret type field to empty in config
config.Secret.Type = ""
}
// check if compiler github token flag should be removed
if strings.EqualFold(flag, internal.FlagCompilerGitHubToken) {
// set the compiler github token field to empty in config
config.Compiler.GitHub.Token = ""
}
// check if compiler github url flag should be removed
if strings.EqualFold(flag, internal.FlagCompilerGitHubURL) {
// set the compiler github url field to empty in config
config.Compiler.GitHub.URL = ""
}
// check if org flag should be removed
if strings.EqualFold(flag, internal.FlagOrg) {
// set the org field to empty in config
config.Org = ""
}
// check if repo flag should be removed
if strings.EqualFold(flag, internal.FlagRepo) {
// set the repo field to empty in config
config.Repo = ""
}
// check if output flag should be removed
if strings.EqualFold(flag, internal.FlagOutput) {
// set the output field to empty in config
config.Output = ""
}
// check if color flag should be removed
if strings.EqualFold(flag, internal.FlagColor) {
// set the color field to empty in config
config.Color = nil
}
// check if color format flag should be removed
if strings.EqualFold(flag, internal.FlagColorFormat) {
// set the color format field to empty in config
config.ColorFormat = ""
}
// check if color theme flag should be removed
if strings.EqualFold(flag, internal.FlagColorTheme) {
// set the color theme to empty in config
config.ColorTheme = ""
}
}
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)
}