From 62b1c2bf9dee8b734f7d3d0aa3a1ad7644750626 Mon Sep 17 00:00:00 2001 From: TJ Leonard Date: Fri, 14 Aug 2020 15:15:43 -0400 Subject: [PATCH 1/2] add name flag for describe connection --- cmd/describe.go | 44 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 39 insertions(+), 5 deletions(-) diff --git a/cmd/describe.go b/cmd/describe.go index e1ab5db71..6ef39458a 100644 --- a/cmd/describe.go +++ b/cmd/describe.go @@ -18,7 +18,8 @@ package cmd import ( "context" "fmt" - "strconv" + "github.com/meroxa/meroxa-go" + "os" "time" "github.com/spf13/cobra" @@ -40,6 +41,9 @@ var describeResourceCmd = &cobra.Command{ name := args[0] c, err := client() + if err != nil { + fmt.Println("Error: ", err) + } ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() @@ -56,23 +60,50 @@ var describeResourceCmd = &cobra.Command{ var describeConnectionCmd = &cobra.Command{ Use: "connection", Short: "describe connection", + PreRun: func(cmd *cobra.Command, args []string) { + if cmd.Flags().NFlag() > 1 { + fmt.Println("INFO: Only 1 flag can be set") + err := cmd.Usage() + if err != nil { + fmt.Println("Error: ", err) + } + os.Exit(1) + } + }, Run: func(cmd *cobra.Command, args []string) { + var ( + err error + conn *meroxa.Connector + ) + c, err := client() - intID, err := strconv.Atoi(args[0]) + + intID, err := cmd.Flags().GetInt("id") if err != nil { fmt.Println("Error: ", err) + return + } + + name, err := cmd.Flags().GetString("name") + if err != nil { + fmt.Println("Error: ", err) + return } ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() - res, err := c.GetConnection(ctx, intID) + if intID == -1 && name != "" { + conn, err = c.GetConnectionByName(ctx, name) + } else { + conn, err = c.GetConnection(ctx, intID) + } if err != nil { fmt.Println("Error: ", err) + return } - - prettyPrint("connection", res) + prettyPrint("connection", conn) }, } @@ -90,6 +121,9 @@ func init() { // Subcommands describeCmd.AddCommand(describeResourceCmd) describeCmd.AddCommand(describeConnectionCmd) + describeConnectionCmd.Flags().IntP("id", "i", -1, "connection id") + describeConnectionCmd.Flags().StringP("name", "n", "", "connection name") + describeCmd.AddCommand(describeFunctionCmd) // Here you will define your flags and configuration settings. From 591b00434c769d58c4c7cff60f1199cfaa8ae190 Mon Sep 17 00:00:00 2001 From: TJ Leonard Date: Fri, 14 Aug 2020 17:53:53 -0400 Subject: [PATCH 2/2] refactor describe connection to be by name only --- cmd/describe.go | 39 ++++++++------------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/cmd/describe.go b/cmd/describe.go index 6ef39458a..c3212d23b 100644 --- a/cmd/describe.go +++ b/cmd/describe.go @@ -17,9 +17,9 @@ package cmd import ( "context" + "errors" "fmt" "github.com/meroxa/meroxa-go" - "os" "time" "github.com/spf13/cobra" @@ -58,47 +58,27 @@ var describeResourceCmd = &cobra.Command{ } var describeConnectionCmd = &cobra.Command{ - Use: "connection", + Use: "connection [name]", Short: "describe connection", - PreRun: func(cmd *cobra.Command, args []string) { - if cmd.Flags().NFlag() > 1 { - fmt.Println("INFO: Only 1 flag can be set") - err := cmd.Usage() - if err != nil { - fmt.Println("Error: ", err) - } - os.Exit(1) + Args: func(cmd *cobra.Command, args []string) error { + if len(args) < 1 { + return errors.New("requires a connection name") } + return nil }, Run: func(cmd *cobra.Command, args []string) { var ( err error conn *meroxa.Connector ) - + name := args[0] c, err := client() - intID, err := cmd.Flags().GetInt("id") - if err != nil { - fmt.Println("Error: ", err) - return - } - - name, err := cmd.Flags().GetString("name") - if err != nil { - fmt.Println("Error: ", err) - return - } - ctx := context.Background() ctx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() - if intID == -1 && name != "" { - conn, err = c.GetConnectionByName(ctx, name) - } else { - conn, err = c.GetConnection(ctx, intID) - } + conn, err = c.GetConnectionByName(ctx, name) if err != nil { fmt.Println("Error: ", err) return @@ -121,9 +101,6 @@ func init() { // Subcommands describeCmd.AddCommand(describeResourceCmd) describeCmd.AddCommand(describeConnectionCmd) - describeConnectionCmd.Flags().IntP("id", "i", -1, "connection id") - describeConnectionCmd.Flags().StringP("name", "n", "", "connection name") - describeCmd.AddCommand(describeFunctionCmd) // Here you will define your flags and configuration settings.