Skip to content

Commit 8654b62

Browse files
committed
Refactored cli.Commands to return an error instead of panicing
1 parent a48579b commit 8654b62

34 files changed

+110
-278
lines changed

cmd/scw/main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ func main() {
120120
}
121121
cmd.API = api
122122
}
123-
cmd.Exec(cmd, cmd.Flag.Args())
123+
err = cmd.Exec(cmd, cmd.Flag.Args())
124+
if err != nil {
125+
log.Fatalf("Cannot execute '%s': %v", cmd.Name(), err)
126+
}
124127
if cmd.API != nil {
125128
cmd.API.Sync()
126129
}

pkg/cli/attach.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
package cli
66

7-
import (
8-
"github.com/scaleway/scaleway-cli/pkg/commands"
9-
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
10-
)
7+
import "github.com/scaleway/scaleway-cli/pkg/commands"
118

129
var cmdAttach = &Command{
13-
Exec: cmdExecAttach,
10+
Exec: runAttach,
1411
UsageLine: "attach [OPTIONS] SERVER",
1512
Description: "Attach to a server serial console",
1613
Help: "Attach to a running server serial console.",
@@ -30,7 +27,7 @@ func init() {
3027
var attachHelp bool // -h, --help flag
3128
var attachNoStdin bool // --no-stdin flag
3229

33-
func cmdExecAttach(cmd *Command, rawArgs []string) {
30+
func runAttach(cmd *Command, rawArgs []string) error {
3431
if attachHelp {
3532
cmd.PrintUsage()
3633
}
@@ -43,8 +40,5 @@ func cmdExecAttach(cmd *Command, rawArgs []string) {
4340
Server: rawArgs[0],
4441
}
4542
ctx := cmd.GetContext(rawArgs)
46-
err := commands.RunAttach(ctx, args)
47-
if err != nil {
48-
logrus.Fatalf("Cannot execute 'attach': %v", err)
49-
}
43+
return commands.RunAttach(ctx, args)
5044
}

pkg/cli/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
// Command contains everything needed by the cli main loop to calls the workflow, display help and usage, and the context
2323
type Command struct {
2424
// Exec executes the command
25-
Exec func(cmd *Command, args []string)
25+
Exec func(cmd *Command, args []string) error
2626

2727
// Usage is the one-line usage message.
2828
UsageLine string

pkg/cli/commit.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
package cli
66

7-
import (
8-
"github.com/scaleway/scaleway-cli/pkg/commands"
9-
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
10-
)
7+
import "github.com/scaleway/scaleway-cli/pkg/commands"
118

129
var cmdCommit = &Command{
13-
Exec: cmdExecCommit,
10+
Exec: runCommit,
1411
UsageLine: "commit [OPTIONS] SERVER [NAME]",
1512
Description: "Create a new snapshot from a server's volume",
1613
Help: "Create a new snapshot from a server's volume.",
@@ -29,7 +26,7 @@ func init() {
2926
var commitVolume int // -v, --volume flag
3027
var commitHelp bool // -h, --help flag
3128

32-
func cmdExecCommit(cmd *Command, rawArgs []string) {
29+
func runCommit(cmd *Command, rawArgs []string) error {
3330
if commitHelp {
3431
cmd.PrintUsage()
3532
}
@@ -47,8 +44,5 @@ func cmdExecCommit(cmd *Command, rawArgs []string) {
4744
}
4845

4946
ctx := cmd.GetContext(rawArgs)
50-
err := commands.RunCommit(ctx, args)
51-
if err != nil {
52-
logrus.Fatalf("Cannot execute 'commit': %v", err)
53-
}
47+
return commands.RunCommit(ctx, args)
5448
}

pkg/cli/cp.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
package cli
66

7-
import (
8-
"github.com/scaleway/scaleway-cli/pkg/commands"
9-
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
10-
)
7+
import "github.com/scaleway/scaleway-cli/pkg/commands"
118

129
var cmdCp = &Command{
1310
Exec: runCp,
@@ -40,7 +37,7 @@ func init() {
4037
var cpHelp bool // -h, --help flag
4138
var cpGateway string // -g, --gateway flag
4239

43-
func runCp(cmd *Command, rawArgs []string) {
40+
func runCp(cmd *Command, rawArgs []string) error {
4441
if cpHelp {
4542
cmd.PrintUsage()
4643
}
@@ -54,9 +51,5 @@ func runCp(cmd *Command, rawArgs []string) {
5451
Destination: rawArgs[1],
5552
}
5653
ctx := cmd.GetContext(rawArgs)
57-
err := commands.RunCp(ctx, args)
58-
if err != nil {
59-
logrus.Fatalf("Cannot execute 'cp': %v", err)
60-
}
61-
54+
return commands.RunCp(ctx, args)
6255
}

pkg/cli/create.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"strings"
99

1010
"github.com/scaleway/scaleway-cli/pkg/commands"
11-
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
1211
)
1312

1413
var cmdCreate = &Command{
@@ -43,7 +42,7 @@ var createVolume string // -v, --volume flag
4342
var createHelp bool // -h, --help flag
4443
var createTmpSSHKey bool // --tmp-ssh-key flag
4544

46-
func runCreate(cmd *Command, rawArgs []string) {
45+
func runCreate(cmd *Command, rawArgs []string) error {
4746
if createHelp {
4847
cmd.PrintUsage()
4948
}
@@ -65,8 +64,5 @@ func runCreate(cmd *Command, rawArgs []string) {
6564
args.Volumes = strings.Split(runCreateVolume, " ")
6665
}
6766
ctx := cmd.GetContext(rawArgs)
68-
err := commands.RunCreate(ctx, args)
69-
if err != nil {
70-
logrus.Fatalf("Cannot execute 'create': %v", err)
71-
}
67+
return commands.RunCreate(ctx, args)
7268
}

pkg/cli/events.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
package cli
66

7-
import (
8-
"github.com/scaleway/scaleway-cli/pkg/commands"
9-
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
10-
)
7+
import "github.com/scaleway/scaleway-cli/pkg/commands"
118

129
var cmdEvents = &Command{
13-
Exec: cmdExecEvents,
10+
Exec: runEvents,
1411
UsageLine: "events [OPTIONS]",
1512
Description: "Get real time events from the API",
1613
Help: "Get real time events from the API.",
@@ -23,7 +20,7 @@ func init() {
2320
// Flags
2421
var eventsHelp bool // -h, --help flag
2522

26-
func cmdExecEvents(cmd *Command, rawArgs []string) {
23+
func runEvents(cmd *Command, rawArgs []string) error {
2724
if eventsHelp {
2825
cmd.PrintUsage()
2926
}
@@ -33,8 +30,5 @@ func cmdExecEvents(cmd *Command, rawArgs []string) {
3330

3431
args := commands.EventsArgs{}
3532
ctx := cmd.GetContext(rawArgs)
36-
err := commands.RunEvents(ctx, args)
37-
if err != nil {
38-
logrus.Fatalf("Cannot execute 'events': %v", err)
39-
}
33+
return commands.RunEvents(ctx, args)
4034
}

pkg/cli/exec.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,10 @@
44

55
package cli
66

7-
import (
8-
"github.com/scaleway/scaleway-cli/pkg/commands"
9-
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
10-
)
7+
import "github.com/scaleway/scaleway-cli/pkg/commands"
118

129
var cmdExec = &Command{
13-
Exec: cmdExecExec,
10+
Exec: runExec,
1411
UsageLine: "exec [OPTIONS] SERVER [COMMAND] [ARGS...]",
1512
Description: "Run a command on a running server",
1613
Help: "Run a command on a running server.",
@@ -41,7 +38,7 @@ var execTimeout float64 // -T flag
4138
var execHelp bool // -h, --help flag
4239
var execGateway string // -g, --gateway flag
4340

44-
func cmdExecExec(cmd *Command, rawArgs []string) {
41+
func runExec(cmd *Command, rawArgs []string) error {
4542
if execHelp {
4643
cmd.PrintUsage()
4744
}
@@ -57,8 +54,5 @@ func cmdExecExec(cmd *Command, rawArgs []string) {
5754
Command: rawArgs[1:],
5855
}
5956
ctx := cmd.GetContext(rawArgs)
60-
err := commands.RunExec(ctx, args)
61-
if err != nil {
62-
logrus.Fatalf("Cannot exec 'exec': %v", err)
63-
}
57+
return commands.RunExec(ctx, args)
6458
}

pkg/cli/help.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Commands:
5353
Run 'scw COMMAND --help' for more information on a command.
5454
`
5555

56-
func runHelp(cmd *Command, args []string) {
56+
func runHelp(cmd *Command, args []string) error {
5757
if waitHelp {
5858
cmd.PrintUsage()
5959
}
@@ -73,7 +73,8 @@ func runHelp(cmd *Command, args []string) {
7373
t := template.New("top")
7474
template.Must(t.Parse(helpTemplate))
7575
if err := t.Execute(os.Stdout, Commands); err != nil {
76-
panic(err)
76+
return err
7777
}
7878
}
79+
return nil
7980
}

pkg/cli/history.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
package cli
66

7-
import (
8-
"github.com/scaleway/scaleway-cli/pkg/commands"
9-
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
10-
)
7+
import "github.com/scaleway/scaleway-cli/pkg/commands"
118

129
var cmdHistory = &Command{
1310
Exec: runHistory,
@@ -27,7 +24,7 @@ var historyNoTrunc bool // --no-trunc flag
2724
var historyQuiet bool // -q, --quiet flag
2825
var historyHelp bool // -h, --help flag
2926

30-
func runHistory(cmd *Command, rawArgs []string) {
27+
func runHistory(cmd *Command, rawArgs []string) error {
3128
if historyHelp {
3229
cmd.PrintUsage()
3330
}
@@ -41,8 +38,5 @@ func runHistory(cmd *Command, rawArgs []string) {
4138
Image: rawArgs[0],
4239
}
4340
ctx := cmd.GetContext(rawArgs)
44-
err := commands.RunHistory(ctx, args)
45-
if err != nil {
46-
logrus.Fatalf("Cannot execute 'history': %v", err)
47-
}
41+
return commands.RunHistory(ctx, args)
4842
}

0 commit comments

Comments
 (0)