Skip to content

Commit

Permalink
connectors status: --quiet flag & return error if failing tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejWilde committed Apr 20, 2020
1 parent 00db60c commit 1e69f0f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 8 deletions.
26 changes: 18 additions & 8 deletions internal/ctl/connectors/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type connectorsStatusCmdParams struct {
ClusterURL string
Connectors []string
Output string
Quiet bool
}

func connectorsStatusCmd() *cobra.Command {
Expand All @@ -36,6 +37,7 @@ func connectorsStatusCmd() *cobra.Command {
ctl.AddCommonConnectorsFlags(statusCmd, &params.ClusterURL)
ctl.AddConnectorNamesFlags(statusCmd, &params.Connectors)
ctl.AddOutputFlags(statusCmd, &params.Output)
ctl.AddQuietFlag(statusCmd, &params.Quiet)

return statusCmd
}
Expand Down Expand Up @@ -63,17 +65,25 @@ func doConnectorsStatus(_ *cobra.Command, params *connectorsStatusCmdParams) err
return errors.Wrap(err, "error getting connectors status")
}

switch params.Output {
case "json":
if err = printAsJSON(statusList); err != nil {
return errors.Wrap(err, "error printing connectors status as JSON")
if !params.Quiet {
switch params.Output {
case "json":
if err = printAsJSON(statusList); err != nil {
return errors.Wrap(err, "error printing connectors status as JSON")
}

case "table":
printAsTable(statusList)

default:
return fmt.Errorf("invalid output format specified: %s", params.Output)
}
}

case "table":
printAsTable(statusList)
failingConnectors, failingTasks := countFailing(statusList)

default:
return fmt.Errorf("invalid output format specified: %s", params.Output)
if failingConnectors != 0 || failingTasks != 0 {
return fmt.Errorf("%d connectors are failng, %d tasks are failing", failingConnectors, failingTasks)
}

return nil
Expand Down
45 changes: 45 additions & 0 deletions internal/ctl/connectors/status_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package connectors

import (
"testing"

"github.com/90poe/connectctl/pkg/client/connect"
"github.com/stretchr/testify/require"
)

func TestCountFailing(t *testing.T) {
statusList := []*connect.ConnectorStatus{
{
Connector: connect.ConnectorState{
State: "RUNNING",
},
Tasks: []connect.TaskState{
{
State: "RUNNING",
},
{
State: "FAILED",
},
},
},
{
Connector: connect.ConnectorState{
State: "FAILED",
},
Tasks: []connect.TaskState{
{
State: "FAILED",
},
{
State: "FAILED",
},
},
},
}

connectorsFailing, tasksFailing := countFailing(statusList)

require.Equal(t, 1, connectorsFailing)
require.Equal(t, 3, tasksFailing)

}
10 changes: 10 additions & 0 deletions internal/ctl/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ func AddOutputFlags(cmd *cobra.Command, output *string) {
BindStringVarP(cmd.Flags(), output, "json", "output", "o", "specify the output format (valid options: json, table)")
}

func AddQuietFlag(cmd *cobra.Command, quiet *bool) {
BindBoolVarP(cmd.Flags(), quiet, false, "quiet", "q", "disable output logging")
}

func AddDefinitionFilesFlags(cmd *cobra.Command, files *[]string, directory *string, env *string) {
BindStringArrayVarP(cmd.Flags(), files, []string{}, "files", "f", "the connector definitions files (Required if --directory or --env-var not specified)")
BindStringVarP(cmd.Flags(), directory, "", "directory", "d", "the directory containing the connector definitions files (Required if --file or --env-vars not specified)")
Expand Down Expand Up @@ -55,6 +59,12 @@ func BindBoolVar(f *pflag.FlagSet, p *bool, value bool, long, description string
viper.SetDefault(long, value)
}

func BindBoolVarP(f *pflag.FlagSet, p *bool, value bool, long, short, description string) {
f.BoolVarP(p, long, short, value, description)
_ = viper.BindPFlag(long, f.Lookup(long))
viper.SetDefault(long, value)
}

func BindStringVarP(f *pflag.FlagSet, p *string, value, long, short, description string) {
f.StringVarP(p, long, short, value, description)
_ = viper.BindPFlag(long, f.Lookup(long))
Expand Down

0 comments on commit 1e69f0f

Please sign in to comment.