Skip to content

Commit bc64a77

Browse files
authored
refactor: os.Exit'ing in main (#216)
2 parents cc1cdc7 + 7c34a47 commit bc64a77

3 files changed

Lines changed: 17 additions & 7 deletions

File tree

internal/cmd.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ func setupColor(ctx context.Context, cmd *cli.Command) (context.Context, error)
272272
}
273273

274274
// Cmd runs the CLI application with the given version string.
275-
func Cmd(version string) {
275+
func Cmd(version string) error {
276276
var configFile string
277277

278278
configSource := altsrc.NewStringPtrSourcer(&configFile)
@@ -291,7 +291,5 @@ func Cmd(version string) {
291291
},
292292
}
293293

294-
if err := app.Run(context.Background(), os.Args); err != nil {
295-
os.Exit(1)
296-
}
294+
return app.Run(context.Background(), os.Args) //nolint:wrapcheck
297295
}

internal/cmd_test.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,13 @@ func TestCmd_Help(t *testing.T) {
154154

155155
defer func() { os.Args = oldArgs }()
156156

157+
var err error
158+
157159
out := testutil.CaptureOutput(t, func() {
158-
Cmd("test-version")
160+
err = Cmd("test-version")
159161
})
160162

163+
require.NoError(t, err)
161164
assert.Contains(t, out, "Utility to interact with T-Mobile Home Internet gateway")
162165
}
163166

@@ -168,10 +171,14 @@ func TestCmd_Version(t *testing.T) {
168171
defer func() { os.Args = oldArgs }()
169172

170173
testVersion := "test-version-123"
174+
175+
var err error
176+
171177
out := testutil.CaptureOutput(t, func() {
172-
Cmd(testVersion)
178+
err = Cmd(testVersion)
173179
})
174180

181+
require.NoError(t, err)
175182
assert.Contains(
176183
t,
177184
out,

main.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@
22
package main
33

44
import (
5+
"os"
6+
57
"github.com/hugoh/tmhi-cli/internal"
68
)
79

810
var version = "dev"
911

1012
func main() {
11-
internal.Cmd(version)
13+
err := internal.Cmd(version)
14+
if err != nil {
15+
os.Exit(1)
16+
}
1217
}

0 commit comments

Comments
 (0)