diff --git a/internal/cmd/blueprint/blueprint.go b/internal/cmd/blueprint/blueprint.go index ded0e06..889161c 100644 --- a/internal/cmd/blueprint/blueprint.go +++ b/internal/cmd/blueprint/blueprint.go @@ -2,10 +2,11 @@ package blueprint import ( "fmt" + "math" + "github.com/spacelift-io/spacectl/internal/cmd" "github.com/spacelift-io/spacectl/internal/cmd/authenticated" "github.com/urfave/cli/v2" - "math" ) // Command encapsulates the blueprintNode command subtree. @@ -18,11 +19,11 @@ func Command() *cli.Command { Name: "list", Usage: "List the blueprints you have access to", Flags: []cli.Flag{ - flagShowLabels, + cmd.FlagShowLabels, cmd.FlagOutputFormat, cmd.FlagNoColor, - flagLimit, - flagSearch, + cmd.FlagLimit, + cmd.FlagSearch, }, Action: listBlueprints(), Before: cmd.PerformAllBefore( @@ -38,12 +39,12 @@ func Command() *cli.Command { } func validateLimit(cliCtx *cli.Context) error { - if cliCtx.IsSet(flagLimit.Name) { - if cliCtx.Uint(flagLimit.Name) == 0 { + if cliCtx.IsSet(cmd.FlagLimit.Name) { + if cliCtx.Uint(cmd.FlagLimit.Name) == 0 { return fmt.Errorf("limit must be greater than 0") } - if cliCtx.Uint(flagLimit.Name) >= math.MaxInt32 { + if cliCtx.Uint(cmd.FlagLimit.Name) >= math.MaxInt32 { return fmt.Errorf("limit must be less than %d", math.MaxInt32) } } @@ -52,8 +53,8 @@ func validateLimit(cliCtx *cli.Context) error { } func validateSearch(cliCtx *cli.Context) error { - if cliCtx.IsSet(flagSearch.Name) { - if cliCtx.String(flagSearch.Name) == "" { + if cliCtx.IsSet(cmd.FlagSearch.Name) { + if cliCtx.String(cmd.FlagSearch.Name) == "" { return fmt.Errorf("search must be non-empty") } diff --git a/internal/cmd/blueprint/flags.go b/internal/cmd/blueprint/flags.go index 033c47b..9745e5b 100644 --- a/internal/cmd/blueprint/flags.go +++ b/internal/cmd/blueprint/flags.go @@ -1,22 +1 @@ package blueprint - -import "github.com/urfave/cli/v2" - -var flagShowLabels = &cli.BoolFlag{ - Name: "show-labels", - Usage: "[Optional] Indicates that stack labels should be printed when outputting stack data in the table format", - Required: false, - Value: false, -} - -var flagLimit = &cli.UintFlag{ - Name: "limit", - Usage: "[Optional] Limit the number of items to return", - Required: false, -} - -var flagSearch = &cli.StringFlag{ - Name: "search", - Usage: "[Optional] Performs a full-text search.", - Required: false, -} diff --git a/internal/cmd/blueprint/list.go b/internal/cmd/blueprint/list.go index e6c0887..27485eb 100644 --- a/internal/cmd/blueprint/list.go +++ b/internal/cmd/blueprint/list.go @@ -3,6 +3,9 @@ package blueprint import ( "context" "fmt" + "slices" + "strings" + "github.com/pkg/errors" "github.com/shurcooL/graphql" "github.com/spacelift-io/spacectl/client/structs" @@ -10,8 +13,6 @@ import ( "github.com/spacelift-io/spacectl/internal/cmd" "github.com/spacelift-io/spacectl/internal/cmd/authenticated" "github.com/urfave/cli/v2" - "slices" - "strings" ) func listBlueprints() cli.ActionFunc { @@ -22,13 +23,13 @@ func listBlueprints() cli.ActionFunc { } var limit *uint - if cliCtx.IsSet(flagLimit.Name) { - limit = internal.Ptr(cliCtx.Uint(flagLimit.Name)) + if cliCtx.IsSet(cmd.FlagLimit.Name) { + limit = internal.Ptr(cliCtx.Uint(cmd.FlagLimit.Name)) } var search *string - if cliCtx.IsSet(flagSearch.Name) { - search = internal.Ptr(cliCtx.String(flagSearch.Name)) + if cliCtx.IsSet(cmd.FlagSearch.Name) { + search = internal.Ptr(cliCtx.String(cmd.FlagSearch.Name)) } switch outputFormat { @@ -98,7 +99,7 @@ func listBlueprintsTable( } columns := []string{"Name", "ID", "Description", "State", "Space", "Updated At"} - if ctx.Bool(flagShowLabels.Name) { + if ctx.Bool(cmd.FlagShowLabels.Name) { columns = append(columns, "Labels") } @@ -112,7 +113,7 @@ func listBlueprintsTable( b.Space.Name, cmd.HumanizeUnixSeconds(b.UpdatedAt), } - if ctx.Bool(flagShowLabels.Name) { + if ctx.Bool(cmd.FlagShowLabels.Name) { row = append(row, strings.Join(b.Labels, ", ")) } diff --git a/internal/cmd/flags.go b/internal/cmd/flags.go index a87174a..2ec7a31 100644 --- a/internal/cmd/flags.go +++ b/internal/cmd/flags.go @@ -20,3 +20,23 @@ var FlagNoColor = &cli.BoolFlag{ Name: "no-color", Usage: "Disables coloring for the console output. Automatically enabled when the output is not a terminal.", } + +// FlagLimit is a flag used for limiting the number of items to return. +var FlagLimit = &cli.UintFlag{ + Name: "limit", + Usage: "[Optional] Limit the number of items to return", +} + +// FlagSearch is a flag used for performing a full-text search. +var FlagSearch = &cli.StringFlag{ + Name: "search", + Usage: "[Optional] Performs a full-text search.", +} + +// FlagShowLabels is a flag used for indicating that labels should be printed when outputting data in the table format. +var FlagShowLabels = &cli.BoolFlag{ + Name: "show-labels", + Usage: "[Optional] Indicates that labels should be printed when outputting data in the table format", + Required: false, + Value: false, +} diff --git a/internal/cmd/profile/login_command.go b/internal/cmd/profile/login_command.go index e2e928b..12c9b26 100644 --- a/internal/cmd/profile/login_command.go +++ b/internal/cmd/profile/login_command.go @@ -100,7 +100,6 @@ func getCredentialsType(ctx *cli.Context) (session.CredentialsType, error) { return 0, err } - //nolint: gosec return session.CredentialsType(result + 1), nil } diff --git a/internal/cmd/stack/flags.go b/internal/cmd/stack/flags.go index 5a1926e..4359214 100644 --- a/internal/cmd/stack/flags.go +++ b/internal/cmd/stack/flags.go @@ -120,13 +120,6 @@ var flagSearchCount = &cli.IntFlag{ Value: 30, } -var flagShowLabels = &cli.BoolFlag{ - Name: "show-labels", - Usage: "[Optional] Indicates that stack labels should be printed when outputting stack data in the table format", - Required: false, - Value: false, -} - var flagOverrideEnvVarsTF = &cli.StringSliceFlag{ Name: "tf-env-var-override", Usage: "[Optional] Terraform environment variables injected into the run at runtime, they will be prefixed with TF_ by default, example: --tf-env-var-override 'foo=bar,bar=baz'", @@ -157,13 +150,3 @@ var flagInteractive = &cli.BoolFlag{ Aliases: []string{"i"}, Usage: "[Optional] Whether to run the command in interactive mode", } - -var flagLimit = &cli.UintFlag{ - Name: "limit", - Usage: "[Optional] Limit the number of items to return", -} - -var flagSearch = &cli.StringFlag{ - Name: "search", - Usage: "[Optional] Performs a full-text search.", -} diff --git a/internal/cmd/stack/list.go b/internal/cmd/stack/list.go index 9386038..d753625 100644 --- a/internal/cmd/stack/list.go +++ b/internal/cmd/stack/list.go @@ -22,25 +22,25 @@ func listStacks() cli.ActionFunc { } var limit *uint - if cliCtx.IsSet(flagLimit.Name) { - if cliCtx.Uint(flagLimit.Name) == 0 { + if cliCtx.IsSet(cmd.FlagLimit.Name) { + if cliCtx.Uint(cmd.FlagLimit.Name) == 0 { return fmt.Errorf("limit must be greater than 0") } - if cliCtx.Uint(flagLimit.Name) >= math.MaxInt32 { + if cliCtx.Uint(cmd.FlagLimit.Name) >= math.MaxInt32 { return fmt.Errorf("limit must be less than %d", math.MaxInt32) } - limit = internal.Ptr(cliCtx.Uint(flagLimit.Name)) + limit = internal.Ptr(cliCtx.Uint(cmd.FlagLimit.Name)) } var search *string - if cliCtx.IsSet(flagSearch.Name) { - if cliCtx.String(flagSearch.Name) == "" { + if cliCtx.IsSet(cmd.FlagSearch.Name) { + if cliCtx.String(cmd.FlagSearch.Name) == "" { return fmt.Errorf("search must be non-empty") } - search = internal.Ptr(cliCtx.String(flagSearch.Name)) + search = internal.Ptr(cliCtx.String(cmd.FlagSearch.Name)) } switch outputFormat { @@ -110,7 +110,7 @@ func listStacksTable( } columns := []string{"Name", "ID", "Commit", "Author", "State", "Worker Pool", "Locked By"} - if ctx.Bool(flagShowLabels.Name) { + if ctx.Bool(cmd.FlagShowLabels.Name) { columns = append(columns, "Labels") } @@ -125,7 +125,7 @@ func listStacksTable( s.WorkerPool.Name, s.LockedBy, } - if ctx.Bool(flagShowLabels.Name) { + if ctx.Bool(cmd.FlagShowLabels.Name) { row = append(row, strings.Join(s.Labels, ", ")) } diff --git a/internal/cmd/stack/stack.go b/internal/cmd/stack/stack.go index 775f241..cff7b78 100644 --- a/internal/cmd/stack/stack.go +++ b/internal/cmd/stack/stack.go @@ -137,11 +137,11 @@ func Command() *cli.Command { Name: "list", Usage: "List the stacks you have access to", Flags: []cli.Flag{ - flagShowLabels, + cmd.FlagShowLabels, cmd.FlagOutputFormat, cmd.FlagNoColor, - flagLimit, - flagSearch, + cmd.FlagLimit, + cmd.FlagSearch, }, Action: listStacks(), Before: cmd.PerformAllBefore(cmd.HandleNoColor, authenticated.Ensure), diff --git a/main.go b/main.go index 1897154..68d9099 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,11 @@ package main import ( - "github.com/spacelift-io/spacectl/internal/cmd/blueprint" "log" "os" "time" + "github.com/spacelift-io/spacectl/internal/cmd/blueprint" "github.com/spacelift-io/spacectl/internal/cmd/completion" "github.com/spacelift-io/spacectl/internal/cmd/module" "github.com/spacelift-io/spacectl/internal/cmd/profile"