Skip to content

Commit 9612b9e

Browse files
committed
rework error handling to avoid os.Exit() in multiple places
1 parent 69372bc commit 9612b9e

40 files changed

+290
-299
lines changed

cmd/alter/alter-partition.go

+14-13
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package alter
33
import (
44
"strconv"
55

6+
"github.com/deviceinsight/kafkactl/v5/internal"
7+
68
"github.com/deviceinsight/kafkactl/v5/cmd/validation"
79
"github.com/deviceinsight/kafkactl/v5/internal/k8s"
8-
"github.com/deviceinsight/kafkactl/v5/internal/output"
910
"github.com/deviceinsight/kafkactl/v5/internal/partition"
1011
"github.com/deviceinsight/kafkactl/v5/internal/topic"
1112
"github.com/pkg/errors"
@@ -20,21 +21,21 @@ func newAlterPartitionCmd() *cobra.Command {
2021
Use: "partition TOPIC PARTITION",
2122
Short: "alter a partition",
2223
Args: cobra.ExactArgs(2),
23-
Run: func(cmd *cobra.Command, args []string) {
24-
if !k8s.NewOperation().TryRun(cmd, args) {
25-
26-
var partitionID int32
24+
RunE: func(cmd *cobra.Command, args []string) error {
25+
if internal.IsKubernetesEnabled() {
26+
return k8s.NewOperation().Run(cmd, args)
27+
}
2728

28-
if i, err := strconv.ParseInt(args[1], 10, 64); err != nil {
29-
output.Fail(errors.Errorf("argument 2 needs to be a partition %s", args[1]))
30-
} else {
31-
partitionID = int32(i)
32-
}
29+
var (
30+
partitionID int64
31+
err error
32+
)
3333

34-
if err := (&partition.Operation{}).AlterPartition(args[0], partitionID, flags); err != nil {
35-
output.Fail(err)
36-
}
34+
if partitionID, err = strconv.ParseInt(args[1], 10, 64); err != nil {
35+
return errors.Errorf("argument 2 needs to be a partition %s", args[1])
3736
}
37+
38+
return (&partition.Operation{}).AlterPartition(args[0], int32(partitionID), flags)
3839
},
3940
PreRunE: func(cmd *cobra.Command, _ []string) error {
4041
return validation.ValidateAtLeastOneRequiredFlag(cmd)

cmd/alter/alter-topic.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package alter
22

33
import (
44
"github.com/deviceinsight/kafkactl/v5/cmd/validation"
5+
"github.com/deviceinsight/kafkactl/v5/internal"
56
"github.com/deviceinsight/kafkactl/v5/internal/k8s"
6-
"github.com/deviceinsight/kafkactl/v5/internal/output"
77
"github.com/deviceinsight/kafkactl/v5/internal/topic"
88
"github.com/spf13/cobra"
99
)
@@ -16,12 +16,11 @@ func newAlterTopicCmd() *cobra.Command {
1616
Use: "topic TOPIC",
1717
Short: "alter a topic",
1818
Args: cobra.ExactArgs(1),
19-
Run: func(cmd *cobra.Command, args []string) {
20-
if !k8s.NewOperation().TryRun(cmd, args) {
21-
if err := (&topic.Operation{}).AlterTopic(args[0], flags); err != nil {
22-
output.Fail(err)
23-
}
19+
RunE: func(cmd *cobra.Command, args []string) error {
20+
if internal.IsKubernetesEnabled() {
21+
return k8s.NewOperation().Run(cmd, args)
2422
}
23+
return (&topic.Operation{}).AlterTopic(args[0], flags)
2524
},
2625
PreRunE: func(cmd *cobra.Command, _ []string) error {
2726
return validation.ValidateAtLeastOneRequiredFlag(cmd)

cmd/attach/attach.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package attach
22

33
import (
44
"github.com/deviceinsight/kafkactl/v5/internal/k8s"
5-
"github.com/deviceinsight/kafkactl/v5/internal/output"
65
"github.com/spf13/cobra"
76
)
87

@@ -12,10 +11,8 @@ func NewAttachCmd() *cobra.Command {
1211
Use: "attach",
1312
Short: "run kafkactl pod in kubernetes and attach to it",
1413
Args: cobra.NoArgs,
15-
Run: func(_ *cobra.Command, _ []string) {
16-
if err := k8s.NewOperation().Attach(); err != nil {
17-
output.Fail(err)
18-
}
14+
RunE: func(_ *cobra.Command, _ []string) error {
15+
return k8s.NewOperation().Attach()
1916
},
2017
}
2118

cmd/clone/clone-consumergroup.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package clone
22

33
import (
4+
"github.com/deviceinsight/kafkactl/v5/internal"
45
"github.com/deviceinsight/kafkactl/v5/internal/consumergroupoffsets"
56
"github.com/deviceinsight/kafkactl/v5/internal/consumergroups"
67
"github.com/deviceinsight/kafkactl/v5/internal/k8s"
7-
"github.com/deviceinsight/kafkactl/v5/internal/output"
88
"github.com/spf13/cobra"
99
)
1010

@@ -15,12 +15,11 @@ func newCloneConsumerGroupCmd() *cobra.Command {
1515
Aliases: []string{"cg"},
1616
Short: "clone existing consumerGroup with all offsets",
1717
Args: cobra.ExactArgs(2),
18-
Run: func(cmd *cobra.Command, args []string) {
19-
if !k8s.NewOperation().TryRun(cmd, args) {
20-
if err := (&consumergroupoffsets.ConsumerGroupOffsetOperation{}).CloneConsumerGroup(args[0], args[1]); err != nil {
21-
output.Fail(err)
22-
}
18+
RunE: func(cmd *cobra.Command, args []string) error {
19+
if internal.IsKubernetesEnabled() {
20+
return k8s.NewOperation().Run(cmd, args)
2321
}
22+
return (&consumergroupoffsets.ConsumerGroupOffsetOperation{}).CloneConsumerGroup(args[0], args[1])
2423
},
2524
ValidArgsFunction: consumergroups.CompleteConsumerGroups,
2625
}

cmd/clone/clone-topic.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package clone
22

33
import (
4+
"github.com/deviceinsight/kafkactl/v5/internal"
45
"github.com/deviceinsight/kafkactl/v5/internal/k8s"
5-
"github.com/deviceinsight/kafkactl/v5/internal/output"
66
"github.com/deviceinsight/kafkactl/v5/internal/topic"
77
"github.com/spf13/cobra"
88
)
@@ -13,12 +13,11 @@ func newCloneTopicCmd() *cobra.Command {
1313
Use: "topic SOURCE_TOPIC TARGET_TOPIC",
1414
Short: "clone existing topic (number of partitions, replication factor, config entries) to new one",
1515
Args: cobra.ExactArgs(2),
16-
Run: func(cmd *cobra.Command, args []string) {
17-
if !k8s.NewOperation().TryRun(cmd, args) {
18-
if err := (&topic.Operation{}).CloneTopic(args[0], args[1]); err != nil {
19-
output.Fail(err)
20-
}
16+
RunE: func(cmd *cobra.Command, args []string) error {
17+
if internal.IsKubernetesEnabled() {
18+
return k8s.NewOperation().Run(cmd, args)
2119
}
20+
return (&topic.Operation{}).CloneTopic(args[0], args[1])
2221
},
2322
ValidArgsFunction: topic.CompleteTopicNames,
2423
}

cmd/completion.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cmd
33
import (
44
"os"
55

6-
"github.com/deviceinsight/kafkactl/v5/internal/output"
76
"github.com/pkg/errors"
87
"github.com/spf13/cobra"
98
)
@@ -41,7 +40,7 @@ $ kafkactl completion fish > ~/.config/fish/completions/kafkactl.fish
4140
DisableFlagsInUseLine: true,
4241
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
4342
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
44-
Run: func(cmd *cobra.Command, args []string) {
43+
RunE: func(cmd *cobra.Command, args []string) error {
4544

4645
var err error
4746

@@ -59,8 +58,9 @@ $ kafkactl completion fish > ~/.config/fish/completions/kafkactl.fish
5958
}
6059

6160
if err != nil {
62-
output.Fail(errors.Wrap(err, "unable to generate shell completion"))
61+
return errors.Wrap(err, "unable to generate shell completion")
6362
}
63+
return nil
6464
},
6565
}
6666

cmd/config/currentContext.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,13 @@ func newCurrentContextCmd() *cobra.Command {
1212
Aliases: []string{"currentContext"},
1313
Short: "show current context",
1414
Long: `Displays the name of context that is currently active`,
15-
Run: func(_ *cobra.Command, _ []string) {
16-
context := global.GetCurrentContext()
15+
RunE: func(_ *cobra.Command, _ []string) error {
16+
context, err := global.GetCurrentContext()
17+
if err != nil {
18+
return err
19+
}
1720
output.Infof("%s", context)
21+
return nil
1822
},
1923
}
2024

cmd/config/getContexts.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ func newGetContextsCmd() *cobra.Command {
1717
Aliases: []string{"getContexts"},
1818
Short: "list configured contexts",
1919
Long: `Output names of all configured contexts`,
20-
Run: func(_ *cobra.Command, _ []string) {
20+
RunE: func(_ *cobra.Command, _ []string) error {
2121
contexts := viper.GetStringMap("contexts")
22-
currentContext := global.GetCurrentContext()
23-
22+
currentContext, err := global.GetCurrentContext()
23+
if err != nil {
24+
return err
25+
}
2426
if outputFormat == "compact" {
2527
for name := range contexts {
2628
output.Infof("%s", name)
@@ -29,24 +31,25 @@ func newGetContextsCmd() *cobra.Command {
2931
writer := output.CreateTableWriter()
3032

3133
if err := writer.WriteHeader("ACTIVE", "NAME"); err != nil {
32-
output.Fail(err)
34+
return err
3335
}
3436
for context := range contexts {
3537
if currentContext == context {
3638
if err := writer.Write("*", context); err != nil {
37-
output.Fail(err)
39+
return err
3840
}
3941
} else {
4042
if err := writer.Write("", context); err != nil {
41-
output.Fail(err)
43+
return err
4244
}
4345
}
4446
}
4547

4648
if err := writer.Flush(); err != nil {
47-
output.Fail(err)
49+
return err
4850
}
4951
}
52+
return nil
5053
},
5154
}
5255

cmd/config/useContext.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package config
22

33
import (
44
"github.com/deviceinsight/kafkactl/v5/internal/global"
5-
"github.com/deviceinsight/kafkactl/v5/internal/output"
65
"github.com/pkg/errors"
76

87
"strings"
@@ -19,20 +18,21 @@ func newUseContextCmd() *cobra.Command {
1918
Short: "switch active context",
2019
Long: `command to switch active context`,
2120
Args: cobra.MinimumNArgs(1),
22-
Run: func(_ *cobra.Command, args []string) {
21+
RunE: func(_ *cobra.Command, args []string) error {
2322

2423
context := strings.Join(args, " ")
2524

2625
contexts := viper.GetStringMap("contexts")
2726

2827
// check if it is an existing context
2928
if _, ok := contexts[context]; !ok {
30-
output.Fail(errors.Errorf("not a valid context: %s", context))
29+
return errors.Errorf("not a valid context: %s", context)
3130
}
3231

3332
if err := global.SetCurrentContext(context); err != nil {
34-
output.Fail(errors.Wrap(err, "unable to write config"))
33+
return errors.Wrap(err, "unable to write config")
3534
}
35+
return nil
3636
},
3737
ValidArgsFunction: func(_ *cobra.Command, args []string, _ string) ([]string, cobra.ShellCompDirective) {
3838
if len(args) != 0 {

cmd/config/view.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ func newViewCmd() *cobra.Command {
1515
Use: "view",
1616
Short: "show contents of config file",
1717
Long: `Shows the contents of the config file that is currently used`,
18-
Run: func(_ *cobra.Command, _ []string) {
18+
RunE: func(_ *cobra.Command, _ []string) error {
1919

2020
yamlFile, err := os.ReadFile(viper.ConfigFileUsed())
2121
if err != nil {
22-
output.Fail(errors.Wrap(err, "unable to read config"))
22+
return errors.Wrap(err, "unable to read config")
2323
}
2424

2525
output.Infof("%s", yamlFile)
26+
return nil
2627
},
2728
}
2829

cmd/consume/consume.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package consume
22

33
import (
4+
"github.com/deviceinsight/kafkactl/v5/internal"
45
"github.com/deviceinsight/kafkactl/v5/internal/consume"
56
"github.com/deviceinsight/kafkactl/v5/internal/consumergroups"
67
"github.com/deviceinsight/kafkactl/v5/internal/k8s"
7-
"github.com/deviceinsight/kafkactl/v5/internal/output"
88
"github.com/deviceinsight/kafkactl/v5/internal/topic"
99
"github.com/spf13/cobra"
1010
)
@@ -17,12 +17,11 @@ func NewConsumeCmd() *cobra.Command {
1717
Use: "consume TOPIC",
1818
Short: "consume messages from a topic",
1919
Args: cobra.ExactArgs(1),
20-
Run: func(cmd *cobra.Command, args []string) {
21-
if !k8s.NewOperation().TryRun(cmd, args) {
22-
if err := (&consume.Operation{}).Consume(args[0], flags); err != nil {
23-
output.Fail(err)
24-
}
20+
RunE: func(cmd *cobra.Command, args []string) error {
21+
if internal.IsKubernetesEnabled() {
22+
return k8s.NewOperation().Run(cmd, args)
2523
}
24+
return (&consume.Operation{}).Consume(args[0], flags)
2625
},
2726
ValidArgsFunction: topic.CompleteTopicNames,
2827
}

cmd/create/create-acl.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package create
22

33
import (
4+
"github.com/deviceinsight/kafkactl/v5/internal"
45
"github.com/deviceinsight/kafkactl/v5/internal/acl"
56
"github.com/deviceinsight/kafkactl/v5/internal/consumergroups"
67
"github.com/deviceinsight/kafkactl/v5/internal/k8s"
7-
"github.com/deviceinsight/kafkactl/v5/internal/output"
88
"github.com/deviceinsight/kafkactl/v5/internal/topic"
99
"github.com/spf13/cobra"
1010
)
@@ -18,12 +18,11 @@ func newCreateACLCmd() *cobra.Command {
1818
Aliases: []string{"acl"},
1919
Short: "create an acl",
2020
Args: cobra.MaximumNArgs(0),
21-
Run: func(cmd *cobra.Command, args []string) {
22-
if !k8s.NewOperation().TryRun(cmd, args) {
23-
if err := (&acl.Operation{}).CreateACL(flags); err != nil {
24-
output.Fail(err)
25-
}
21+
RunE: func(cmd *cobra.Command, args []string) error {
22+
if internal.IsKubernetesEnabled() {
23+
return k8s.NewOperation().Run(cmd, args)
2624
}
25+
return (&acl.Operation{}).CreateACL(flags)
2726
},
2827
ValidArgsFunction: acl.CompleteCreateACL,
2928
}

cmd/create/create-consumergroup.go

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package create
22

33
import (
4+
"github.com/deviceinsight/kafkactl/v5/internal"
45
"github.com/deviceinsight/kafkactl/v5/internal/consumergroupoffsets"
56
"github.com/deviceinsight/kafkactl/v5/internal/k8s"
6-
"github.com/deviceinsight/kafkactl/v5/internal/output"
77
"github.com/spf13/cobra"
88
)
99

@@ -15,12 +15,11 @@ func newCreateConsumerGroupCmd() *cobra.Command {
1515
Aliases: []string{"cg"},
1616
Short: "create a consumerGroup",
1717
Args: cobra.ExactArgs(1),
18-
Run: func(cmd *cobra.Command, args []string) {
19-
if !k8s.NewOperation().TryRun(cmd, args) {
20-
if err := (&consumergroupoffsets.ConsumerGroupOffsetOperation{}).CreateConsumerGroup(cgFlags, args[0]); err != nil {
21-
output.Fail(err)
22-
}
18+
RunE: func(cmd *cobra.Command, args []string) error {
19+
if internal.IsKubernetesEnabled() {
20+
return k8s.NewOperation().Run(cmd, args)
2321
}
22+
return (&consumergroupoffsets.ConsumerGroupOffsetOperation{}).CreateConsumerGroup(cgFlags, args[0])
2423
},
2524
}
2625

0 commit comments

Comments
 (0)