Skip to content

Commit

Permalink
Merge pull request #2 from darklore/refactor
Browse files Browse the repository at this point in the history
refactor, use pkg/errors
  • Loading branch information
darklore authored Mar 26, 2022
2 parents dca026a + 20d6426 commit 3bb0668
Show file tree
Hide file tree
Showing 25 changed files with 109 additions and 75 deletions.
3 changes: 1 addition & 2 deletions cmd/kccli/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"log"
"os"
"runtime/debug"
Expand All @@ -25,7 +24,7 @@ func main() {

c := cmd.NewCmd(version)
if err := c.Execute(); err != nil {
fmt.Fprintln(os.Stderr, "Error:", err)
log.Printf("%+v", err)
os.Exit(1)
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ require github.com/spf13/cobra v1.4.0

require (
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/pkg/errors v0.9.1
github.com/spf13/pflag v1.0.5 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
github.com/cpuguy83/go-md2man/v2 v2.0.1/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.4.0 h1:y+wJpx64xcgO1V+RcnwW0LEHxTKRi2ZDPSBjWnrg88Q=
github.com/spf13/cobra v1.4.0/go.mod h1:Wo4iy3BUC+X2Fybo0PDqwJIv3dNRiZLHQymsfxlB84g=
Expand Down
3 changes: 3 additions & 0 deletions pkg/cmd/connector/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package connector

import (
"github.com/darklore/kafka-connect-cli/pkg/cmd/connector/tasks"
"github.com/darklore/kafka-connect-cli/pkg/cmd/util"
"github.com/spf13/cobra"
)

Expand All @@ -11,6 +12,8 @@ func NewCmd() *cobra.Command {
Short: "Commands for connectors",
}

util.AddEndpointFlag(cmd)

// add subcommands
cmd.AddCommand(newListCmd())
cmd.AddCommand(newConfigCmd())
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func newConfigCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/connector/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"os"

"github.com/darklore/kafka-connect-cli/pkg/cmd/util"
"github.com/darklore/kafka-connect-cli/pkg/kafka/connect"
"github.com/spf13/cobra"
)
Expand All @@ -20,7 +21,7 @@ func newCreateCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func newDeleteCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newDescribeCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/connector/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"os"

"github.com/darklore/kafka-connect-cli/pkg/cmd/util"
"github.com/darklore/kafka-connect-cli/pkg/kafka/connect"
"github.com/spf13/cobra"
)
Expand All @@ -13,7 +14,7 @@ func newListCmd() *cobra.Command {
Use: "list",
Short: "List connectors' name",
RunE: func(cmd *cobra.Command, _ []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newPauseCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func newRestartCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func newResumeCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newStatusCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/connector/tasks/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tasks
import (
"fmt"

"github.com/darklore/kafka-connect-cli/pkg/cmd/util"
"github.com/darklore/kafka-connect-cli/pkg/kafka/connect"
"github.com/spf13/cobra"
)
Expand All @@ -20,7 +21,7 @@ func NewTasksCmd() *cobra.Command {
}

func validateTaskArgs(cmd *cobra.Command, connector string) ([]string, cobra.ShellCompDirective) {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return nil, cobra.ShellCompDirectiveError
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/tasks/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newListCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/tasks/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func newRestartCmd() *cobra.Command {
}
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/tasks/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newStatusCmd() *cobra.Command {
}
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func newTopicsCmd() *cobra.Command {
return nil, cobra.ShellCompDirectiveNoFileComp
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/connector/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func newUpdateCmd() *cobra.Command {
}
},
RunE: func(cmd *cobra.Command, args []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/cmd/plugins/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package plugins

import (
"github.com/darklore/kafka-connect-cli/pkg/cmd/util"
"github.com/spf13/cobra"
)

Expand All @@ -10,6 +11,7 @@ func NewConnectorPluginCmd() *cobra.Command {
Short: "Commands for connector plugin",
}

util.AddEndpointFlag(cmd)
cmd.AddCommand(newConnectorPluginListCmd())
return cmd
}
3 changes: 2 additions & 1 deletion pkg/cmd/plugins/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"os"

"github.com/darklore/kafka-connect-cli/pkg/cmd/util"
"github.com/darklore/kafka-connect-cli/pkg/kafka/connect"
"github.com/spf13/cobra"
)
Expand All @@ -13,7 +14,7 @@ func newConnectorPluginListCmd() *cobra.Command {
Use: "list",
Short: "List connector plugins",
RunE: func(cmd *cobra.Command, _ []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand Down
2 changes: 0 additions & 2 deletions pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ func NewCmd(version string) *cobra.Command {
}
cmd.Version = version

cmd.PersistentFlags().StringP("endpoint", "e", "http://localhost:8083", "Kafka connect REST endpoint")

// subcommands
cmd.AddCommand(newVersionCmd())

Expand Down
23 changes: 22 additions & 1 deletion pkg/cmd/util/util.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
package util

import (
"log"

"github.com/darklore/kafka-connect-cli/pkg/kafka/connect"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func ValidConnectorArgs(cmd *cobra.Command) ([]string, cobra.ShellCompDirective) {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := GetEndpoint(cmd)
if err != nil {
log.Printf("%+v", errors.Wrap(err, ""))
return []string{}, cobra.ShellCompDirectiveError
}

connectors, err := connect.ListConnectorNames(endpoint)
if err != nil {
log.Println(err)
return []string{}, cobra.ShellCompDirectiveError
}

return connectors, cobra.ShellCompDirectiveNoFileComp
}

func AddEndpointFlag(cmd *cobra.Command) {
cmd.PersistentFlags().StringP("endpoint", "e", "http://localhost:8083", "Kafka connect REST endpoint")
}

func GetEndpoint(cmd *cobra.Command) (string, error) {
return cmd.InheritedFlags().GetString("endpoint")
}

func AddOutputFormatFlag(cmd *cobra.Command) {
cmd.Flags().StringP("output", "o", "json", "Output format")
}

func GetOutputFormat(cmd *cobra.Command) (string, error) {
return cmd.Flags().GetString("output")
}
4 changes: 3 additions & 1 deletion pkg/cmd/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"os"

"github.com/darklore/kafka-connect-cli/pkg/cmd/util"
"github.com/darklore/kafka-connect-cli/pkg/kafka/connect"
"github.com/spf13/cobra"
)
Expand All @@ -13,7 +14,7 @@ func newWorkerCmd() *cobra.Command {
Use: "worker",
Short: "Get a connect worker information",
RunE: func(cmd *cobra.Command, _ []string) error {
endpoint, err := cmd.Root().PersistentFlags().GetString("endpoint")
endpoint, err := util.GetEndpoint(cmd)
if err != nil {
return err
}
Expand All @@ -31,5 +32,6 @@ func newWorkerCmd() *cobra.Command {
},
}

util.AddEndpointFlag(cmd)
return cmd
}
Loading

0 comments on commit 3bb0668

Please sign in to comment.