-
Notifications
You must be signed in to change notification settings - Fork 37
/
bundles.go
287 lines (240 loc) · 9.55 KB
/
bundles.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright © 2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package cmd
import (
"github.com/clearlinux/mixer-tools/builder"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// Top level bundle command ('mixer bundle')
var bundleCmd = &cobra.Command{
Use: "bundle",
Short: "Perform various actions on bundles",
}
// Bundle add command ('mixer bundle add')
type bundleAddCmdFlags struct {
allLocal bool
allUpstream bool
git bool
}
var bundleAddFlags bundleAddCmdFlags
var bundleAddCmd = &cobra.Command{
Use: "add <bundle> [<bundle>...]",
Short: "Add local or upstream bundles to your mix",
Long: `Adds local or upstream bundles to your mix by modifying the Mix Bundle List
(stored in the 'mixbundles' file). The Mix Bundle List is parsed, the new
bundles are confirmed to exist and are added, duplicates are removed, and the
resultant list is written back out in sorted order.`,
RunE: func(cmd *cobra.Command, args []string) error {
if !bundleAddFlags.allLocal && !bundleAddFlags.allUpstream {
if len(args) == 0 {
return errors.New("bundle add requires at least 1 argument if neither --all-local nor --all-upstream are passed")
}
}
b, err := builder.NewFromConfig(configFile)
if err != nil {
fail(err)
}
err = b.AddBundles(args, bundleAddFlags.allLocal, bundleAddFlags.allUpstream, bundleAddFlags.git)
if err != nil {
fail(err)
}
return nil
},
}
// Bundle remove command ('mixer bundle remove')
type bundleRemoveCmdFlags struct {
mix bool
local bool
git bool
}
var bundleRemoveFlags bundleRemoveCmdFlags
var bundleRemoveCmd = &cobra.Command{
Use: "remove <bundle> [<bundle>...]",
Short: "Remove bundles from your mix",
Long: `Removes bundles from your mix by modifying the Mix Bundle List
(stored in the 'mixbundles' file). The Mix Bundle List is parsed, the bundles
are removed, and the resultant list is written back out in sorted order. If
bundles do not exist in the mix, they are skipped.
Passing '--local' will also remove the corresponding bundle definition file from
local-bundles, if it exists. Please note that this is an irrevocable step.
'--mix' defaults to true. Passing '--mix=false' will prevent the bundle from
being removed from your Mix Bundle List. This is useful when used in conjunction
with '--local' to *only* remove a bundle from local-bundles. If the bundle being
removed is an edited version from upstream, the bundle will remain in your mix
and now reference the original upstream version. If the bundle was custom, and
no upstream alternative exists, a warning will be returned.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
b, err := builder.NewFromConfig(configFile)
if err != nil {
fail(err)
}
err = b.RemoveBundles(args, bundleRemoveFlags.mix, bundleRemoveFlags.local, bundleRemoveFlags.git)
if err != nil {
fail(err)
}
},
}
// Bundle list command ('mixer bundle list')
type bundleListCmdFlags struct {
tree bool
}
var bundleListFlags bundleListCmdFlags
var bundleListCmd = &cobra.Command{
Use: "list [mix|local|upstream]",
Short: "List bundles",
Long: `List either:
mix The bundles in the mix, recursively following includes (DEFAULT)
local The available local bundles
upstream The available upstream bundles`,
Args: cobra.OnlyValidArgs,
ValidArgs: []string{"mix", "local", "upstream"},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
return errors.New("bundle list takes at most one argument")
}
b, err := builder.NewFromConfig(configFile)
if err != nil {
fail(err)
}
var listType string
if len(args) > 0 {
listType = args[0]
} else {
listType = "mix"
}
switch listType {
case "upstream":
err = b.ListBundles(builder.UpstreamList, bundleListFlags.tree)
case "local":
err = b.ListBundles(builder.LocalList, bundleListFlags.tree)
default:
err = b.ListBundles(builder.MixList, bundleListFlags.tree)
}
if err != nil {
fail(err)
}
return nil
},
}
// Bundle Edit command ('mixer bundle edit')
type bundleEditCmdFlags struct {
copyOnly bool
add bool
git bool
}
var bundleEditFlags bundleEditCmdFlags
var bundleEditCmd = &cobra.Command{
Use: "edit <bundle> [<bundle>...]",
Short: "Edit local and upstream bundles, or create new bundles",
Long: `Edits local and upstream bundle definition files. This command will locate the
bundle (looking first in local-bundles, then in upstream-bundles), and launch
an editor to edit it. If the bundle is only found upstream, the bundle file will
first be copied to your local-bundles directory for editing. If the bundle is
not found anywhere, a blank template is created with the correct name. When the
editor closes, the bundle file is then parsed for validity.
The editor is configured via environment variables. VISUAL takes precedence to
EDITOR. If neither are set, the tool defaults to nano. If nano is not installed,
the tool will skip editing, and act as if '--suppress-editor' had been passed.
Passing '--suppress-editor' will suppress launching the editor, and will thus
only copy the bundle file to local-bundles (if it is only found upstream), or
create the blank template (if it was not found anywhere). This can be useful if
you want to add a bundle to local-bundles, but wish to edit it at a later time.
Passing '--add' will also add the bundle(s) to your mix. Please note that
bundles are added after all bundles are edited, and thus will not be added if
any errors are encountered earlier on.`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
b, err := builder.NewFromConfig(configFile)
if err != nil {
fail(err)
}
err = b.EditBundles(args, bundleEditFlags.copyOnly, bundleEditFlags.add, bundleEditFlags.git)
if err != nil {
fail(err)
}
},
}
// Bundle validate command ('mixer bundle validate')
type bundleValidateCmdFlags struct {
allLocal bool
strict bool
}
var bundleValidateFlags bundleValidateCmdFlags
var bundleValidateCmd = &cobra.Command{
Use: "validate <bundle> [<bundle>...]",
Short: "Validate local bundle definition files",
Long: `Checks bundle definition files for validity. Only local bundle files are
checked; upstream bundles are trusted as valid. Valid bundles yield no output.
Any invalid bundles will yield a non-zero return code.
Basic validation includes checking syntax and structure, and that the bundle has
a valid name. Commands like 'mixer bundle edit' run basic validation
automatically.
An optional '--strict' flag allows you to additionally check that the bundle
header fields are parsable and non-empty, and that the header 'Title' is itself
valid and matches the bundle filename.
Passing '--all-local' will run validation on all bundles in local-bundles.`,
RunE: func(cmd *cobra.Command, args []string) error {
if !bundleValidateFlags.allLocal && len(args) == 0 {
return errors.New("bundle validate requires at least 1 argument if --all-local is not passed")
}
b, err := builder.NewFromConfig(configFile)
if err != nil {
fail(err)
}
var lvl builder.ValidationLevel
if bundleValidateFlags.strict {
lvl = builder.StrictValidation
} else {
lvl = builder.BasicValidation
}
if bundleValidateFlags.allLocal {
err = b.ValidateLocalBundles(lvl)
} else {
err = b.ValidateBundles(args, lvl)
}
if err != nil {
fail(err)
}
return nil
},
}
// List of all bundle commands
var bundlesCmds = []*cobra.Command{
bundleAddCmd,
bundleRemoveCmd,
bundleListCmd,
bundleEditCmd,
bundleValidateCmd,
}
func init() {
for _, cmd := range bundlesCmds {
bundleCmd.AddCommand(cmd)
cmd.Flags().StringVarP(&configFile, "config", "c", "", "Builder config to use")
}
RootCmd.AddCommand(bundleCmd)
bundleAddCmd.Flags().BoolVar(&bundleAddFlags.allLocal, "all-local", false, "Add all local bundles; takes precedence over bundle list")
bundleAddCmd.Flags().BoolVar(&bundleAddFlags.allUpstream, "all-upstream", false, "Add all upstream bundles; takes precedence over bundle list")
bundleAddCmd.Flags().BoolVar(&bundleAddFlags.git, "git", false, "Automatically apply new git commit")
bundleRemoveCmd.Flags().BoolVar(&bundleRemoveFlags.mix, "mix", true, "Remove bundle from Mix Bundle List")
bundleRemoveCmd.Flags().BoolVar(&bundleRemoveFlags.local, "local", false, "Also remove bundle file from local-bundles (irrevocable)")
bundleRemoveCmd.Flags().BoolVar(&bundleRemoveFlags.git, "git", false, "Automatically apply new git commit")
bundleListCmd.Flags().BoolVar(&bundleListFlags.tree, "tree", false, "Pretty-print the list as a tree.")
bundleEditCmd.Flags().BoolVar(&bundleEditFlags.copyOnly, "suppress-editor", false, "Suppress launching editor (only copy to local-bundles or create template)")
bundleEditCmd.Flags().BoolVar(&bundleEditFlags.add, "add", false, "Add the bundle(s) to your mix")
bundleEditCmd.Flags().BoolVar(&bundleEditFlags.git, "git", false, "Automatically apply new git commit")
bundleValidateCmd.Flags().BoolVar(&bundleValidateFlags.allLocal, "all-local", false, "Validate all local bundles")
bundleValidateCmd.Flags().BoolVar(&bundleValidateFlags.strict, "strict", false, "Strict validation (see usage)")
}