Skip to content

Commit 490c5bb

Browse files
refactor: cmd options ToOptions method, enforce lll on cmd package (#2776)
* refactor: cmd options ToOptions method, enforce lll on cmd package * chore: enforce mnd on cmd package * fix: resolve #2775 * fix: generate parseNodeFlags regression * chore: enable wsl on cmd package * chore: appease a few deepsource nits
1 parent a10c421 commit 490c5bb

34 files changed

+2075
-806
lines changed

.golangci.yml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ linters:
1212
- govet
1313
- ineffassign
1414
- importas
15+
- lll
1516
- misspell
17+
- mnd
1618
- nakedret
1719
- noctx
1820
- nolintlint
@@ -21,6 +23,7 @@ linters:
2123
- unparam
2224
- unused
2325
- whitespace
26+
- wsl_v5
2427
disable:
2528
# disabling for *now* so ci can pass nicely, but (some?) of these likely should be adressed
2629
- dupl
@@ -30,14 +33,11 @@ linters:
3033
- goconst
3134
- gocyclo
3235
- gosec
33-
- lll
34-
- mnd
3536
- nestif
3637
- perfsprint
3738
- prealloc
3839
- revive
3940
- wrapcheck
40-
- wsl_v5
4141
settings:
4242
dupl:
4343
threshold: 100
@@ -95,6 +95,12 @@ linters:
9595
- linters:
9696
- staticcheck
9797
text: could remove embedded field
98+
# currently slowly enforcing some things, starting with cmd package
99+
- linters:
100+
- lll
101+
- mnd
102+
- wsl_v5
103+
path-except: cmd
98104
formatters:
99105
enable:
100106
- gofmt

cmd/config.go

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ import (
1616

1717
func configCmd(o *Options) (*cobra.Command, error) {
1818
c := &cobra.Command{
19-
Use: "config",
20-
Short: "configure a lab",
21-
Long: "configure a lab based on templates and variables from the topology definition file\nreference: https://containerlab.dev/cmd/config/",
19+
Use: "config",
20+
Short: "configure a lab",
21+
Long: "configure a lab based on templates and variables from the topology definition " +
22+
"file\n reference: https://containerlab.dev/cmd/config/",
2223
Aliases: []string{"conf"},
2324
ValidArgs: []string{"commit", "send", "compare", "template"},
2425
SilenceUsage: true,
@@ -105,9 +106,10 @@ func configSubCmds(c *cobra.Command, o *Options) {
105106
compareC.Flags().AddFlagSet(c.Flags())
106107

107108
templateC := &cobra.Command{
108-
Use: "template",
109-
Short: "render a template",
110-
Long: "render a template based on variables from the topology definition file\nreference: https://containerlab.dev/cmd/config/template",
109+
Use: "template",
110+
Short: "render a template",
111+
Long: "render a template based on variables from the topology definition file\n" +
112+
"reference: https://containerlab.dev/cmd/config/template",
111113
Aliases: []string{"conf"},
112114
SilenceUsage: true,
113115
RunE: func(_ *cobra.Command, _ []string) error {
@@ -117,8 +119,13 @@ func configSubCmds(c *cobra.Command, o *Options) {
117119

118120
c.AddCommand(templateC)
119121
templateC.Flags().AddFlagSet(c.Flags())
120-
templateC.Flags().BoolVarP(&o.Config.TemplateVarOnly, "vars", "v", o.Config.TemplateVarOnly,
121-
"show variable used for template rendering")
122+
templateC.Flags().BoolVarP(
123+
&o.Config.TemplateVarOnly,
124+
"vars",
125+
"v",
126+
o.Config.TemplateVarOnly,
127+
"show variable used for template rendering",
128+
)
122129
templateC.Flags().SortFlags = false
123130
}
124131

@@ -128,12 +135,7 @@ func configRun(_ *cobra.Command, args []string, o *Options) error {
128135
transport.DebugCount = o.Global.DebugCount
129136
clabcoreconfig.DebugCount = o.Global.DebugCount
130137

131-
c, err := clabcore.NewContainerLab(
132-
clabcore.WithTimeout(o.Global.Timeout),
133-
clabcore.WithTopoPath(o.Global.TopologyFile, o.Global.VarsFile),
134-
clabcore.WithNodeFilter(o.Filter.NodeFilter),
135-
clabcore.WithDebug(o.Global.DebugCount > 0),
136-
)
138+
c, err := clabcore.NewContainerLab(o.ToClabOptions()...)
137139
if err != nil {
138140
return err
139141
}
@@ -168,12 +170,14 @@ func configRun(_ *cobra.Command, args []string, o *Options) error {
168170
}
169171

170172
var wg sync.WaitGroup
173+
171174
deploy := func(n string) {
172175
defer wg.Done()
173176

174177
cs, ok := allConfig[n]
175178
if !ok {
176179
log.Errorf("Invalid node in filter: %s", n)
180+
177181
return
178182
}
179183

@@ -182,7 +186,9 @@ func configRun(_ *cobra.Command, args []string, o *Options) error {
182186
log.Warnf("%s: %s", cs.TargetNode.ShortName, err)
183187
}
184188
}
189+
185190
wg.Add(len(o.Filter.LabelFilter))
191+
186192
for _, node := range o.Filter.LabelFilter {
187193
// On debug this will not be executed concurrently
188194
if log.GetLevel() == (log.DebugLevel) {
@@ -191,6 +197,7 @@ func configRun(_ *cobra.Command, args []string, o *Options) error {
191197
go deploy(node)
192198
}
193199
}
200+
194201
wg.Wait()
195202

196203
return nil
@@ -201,11 +208,7 @@ func configTemplate(o *Options) error {
201208

202209
clabcoreconfig.DebugCount = o.Global.DebugCount
203210

204-
c, err := clabcore.NewContainerLab(
205-
clabcore.WithTimeout(o.Global.Timeout),
206-
clabcore.WithTopoPath(o.Global.TopologyFile, o.Global.VarsFile),
207-
clabcore.WithDebug(o.Global.DebugCount > 0),
208-
)
211+
c, err := clabcore.NewContainerLab(o.ToClabOptions()...)
209212
if err != nil {
210213
return err
211214
}
@@ -216,11 +219,13 @@ func configTemplate(o *Options) error {
216219
}
217220

218221
allConfig := clabcoreconfig.PrepareVars(c)
222+
219223
if o.Config.TemplateVarOnly {
220224
for _, n := range o.Filter.LabelFilter {
221225
conf := allConfig[n]
222226
conf.Print(true, false)
223227
}
228+
224229
return nil
225230
}
226231

@@ -241,17 +246,21 @@ func validateFilter(nodes map[string]clabnodes.Node, o *Options) error {
241246
for n := range nodes {
242247
o.Filter.LabelFilter = append(o.Filter.LabelFilter, n)
243248
}
249+
244250
return nil
245251
}
246252

247253
var mis []string
254+
248255
for _, nn := range o.Filter.LabelFilter {
249256
if _, ok := nodes[nn]; !ok {
250257
mis = append(mis, nn)
251258
}
252259
}
260+
253261
if len(mis) > 0 {
254262
return fmt.Errorf("invalid nodes in filter: %s", strings.Join(mis, ", "))
255263
}
264+
256265
return nil
257266
}

cmd/context.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import (
1111
"github.com/spf13/cobra"
1212
)
1313

14+
const (
15+
maxCancelledDestroyTimeout = 20 * time.Second
16+
)
17+
1418
var onlyOneSignalHandler = make(chan struct{}) //nolint: gochecknoglobals
1519

1620
// SignalHandledContext returns a context that will be canceled if a SIGINT or SIGTERM is
@@ -27,23 +31,35 @@ func SignalHandledContext() (context.Context, context.CancelFunc) {
2731

2832
go func() {
2933
sig := <-sigs
30-
log.Errorf("received signal %q, canceling context and cleaning deployment...", sig)
34+
35+
log.Errorf("received signal %q, canceling context", sig)
3136

3237
cancel()
3338

34-
destroyCtx, destroyCancel := context.WithTimeout(context.Background(), 20*time.Second)
39+
defer os.Exit(1)
40+
41+
options := GetOptions()
42+
43+
if !options.Global.CleanOnCancel {
44+
log.Debug("clean on cancel is not true, exiting")
45+
46+
return
47+
}
48+
49+
destroyCtx, destroyCancel := context.WithTimeout(
50+
context.Background(),
51+
maxCancelledDestroyTimeout,
52+
)
3553
defer destroyCancel()
3654

3755
// destroyFn requires a cobra.Command but only needs the ctx from it
3856
destroyCmd := &cobra.Command{}
3957
destroyCmd.SetContext(destroyCtx)
4058

41-
err := destroyFn(destroyCmd, GetOptions())
59+
err := destroyFn(destroyCmd, options)
4260
if err != nil {
4361
log.Errorf("failed destroying lab after cancellation signal: %v", err)
4462
}
45-
46-
os.Exit(1) // skipcq: RVV-A0003
4763
}()
4864

4965
return ctx, cancel

0 commit comments

Comments
 (0)