Skip to content

Commit bb9e31b

Browse files
authored
Merge pull request #2430 from alexandear/refactor/deep-exit
cmd/limactl: return error instead of calling os.Exit
2 parents 9f8b406 + 597c276 commit bb9e31b

File tree

4 files changed

+62
-8
lines changed

4 files changed

+62
-8
lines changed

.golangci.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ linters-settings:
102102
- filepathJoin
103103
errorlint:
104104
asserts: false
105+
revive:
106+
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md
107+
rules:
108+
- name: blank-imports
109+
- name: context-as-argument
110+
- name: context-keys-type
111+
- name: deep-exit
112+
- name: dot-imports
113+
- name: empty-block
114+
- name: error-naming
115+
- name: error-return
116+
- name: error-strings
117+
- name: errorf
118+
- name: exported
119+
- name: increment-decrement
120+
- name: indent-error-flow
121+
- name: package-comments
122+
- name: range
123+
- name: receiver-naming
124+
- name: redefines-builtin-id
125+
- name: superfluous-else
126+
- name: time-naming
127+
- name: unexported-return
128+
- name: unreachable-code
129+
- name: unused-parameter
130+
- name: var-declaration
131+
- name: var-naming
105132
issues:
106133
# Maximum issues count per one linter.
107134
max-issues-per-linter: 0

cmd/limactl/list.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,19 @@ func instanceMatches(arg string, instances []string) []string {
7373
return matches
7474
}
7575

76+
// unmatchedInstancesError is created when unmatched instance names found.
77+
type unmatchedInstancesError struct{}
78+
79+
// Error implements error.
80+
func (unmatchedInstancesError) Error() string {
81+
return "unmatched instances"
82+
}
83+
84+
// ExitCode implements ExitCoder.
85+
func (unmatchedInstancesError) ExitCode() int {
86+
return 1
87+
}
88+
7689
func listAction(cmd *cobra.Command, args []string) error {
7790
quiet, err := cmd.Flags().GetBool("quiet")
7891
if err != nil {
@@ -148,7 +161,7 @@ func listAction(cmd *cobra.Command, args []string) error {
148161
fmt.Fprintln(cmd.OutOrStdout(), instName)
149162
}
150163
if unmatchedInstances {
151-
os.Exit(1)
164+
return unmatchedInstancesError{}
152165
}
153166
return nil
154167
}
@@ -186,7 +199,7 @@ func listAction(cmd *cobra.Command, args []string) error {
186199

187200
err = store.PrintInstances(out, instances, format, &options)
188201
if err == nil && unmatchedInstances {
189-
os.Exit(1)
202+
return unmatchedInstancesError{}
190203
}
191204
return err
192205
}

cmd/limactl/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ func handleExitCoder(err error) {
156156
}
157157

158158
if exitErr, ok := err.(ExitCoder); ok {
159-
os.Exit(exitErr.ExitCode())
159+
os.Exit(exitErr.ExitCode()) //nolint:revive // it's intentional to call os.Exit in this function
160160
return
161161
}
162162
}

cmd/limactl/start.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,21 @@ func modifyInPlace(st *creatorState, yq string) error {
373373
return nil
374374
}
375375

376+
// exitSuccessError is an error that indicates a successful exit.
377+
type exitSuccessError struct {
378+
Msg string
379+
}
380+
381+
// Error implements error.
382+
func (e exitSuccessError) Error() string {
383+
return e.Msg
384+
}
385+
386+
// ExitCode implements ExitCoder.
387+
func (exitSuccessError) ExitCode() int {
388+
return 0
389+
}
390+
376391
func chooseNextCreatorState(st *creatorState, yq string) (*creatorState, error) {
377392
for {
378393
if err := modifyInPlace(st, yq); err != nil {
@@ -411,9 +426,9 @@ func chooseNextCreatorState(st *creatorState, yq string) (*creatorState, error)
411426
return st, err
412427
}
413428
if len(st.yBytes) == 0 {
414-
logrus.Info("Aborting, as requested by saving the file with empty content")
415-
os.Exit(0)
416-
return st, errors.New("should not reach here")
429+
const msg = "Aborting, as requested by saving the file with empty content"
430+
logrus.Info(msg)
431+
return nil, exitSuccessError{Msg: msg}
417432
}
418433
return st, nil
419434
case 2: // "Choose another template..."
@@ -446,8 +461,7 @@ func chooseNextCreatorState(st *creatorState, yq string) (*creatorState, error)
446461
}
447462
continue
448463
case 3: // "Exit"
449-
os.Exit(0)
450-
return st, errors.New("should not reach here")
464+
return nil, exitSuccessError{Msg: "Choosing to exit"}
451465
default:
452466
return st, fmt.Errorf("unexpected answer %q", ans)
453467
}

0 commit comments

Comments
 (0)