Skip to content

Commit

Permalink
feature(blueprints): Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
0michalsokolowski0 committed Sep 20, 2024
1 parent f9e417a commit f1c18ad
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 69 deletions.
19 changes: 10 additions & 9 deletions internal/cmd/blueprint/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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(
Expand All @@ -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)
}
}
Expand All @@ -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")
}

Expand Down
21 changes: 0 additions & 21 deletions internal/cmd/blueprint/flags.go
Original file line number Diff line number Diff line change
@@ -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,
}
17 changes: 9 additions & 8 deletions internal/cmd/blueprint/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package blueprint
import (
"context"
"fmt"
"slices"
"strings"

"github.com/pkg/errors"
"github.com/shurcooL/graphql"
"github.com/spacelift-io/spacectl/client/structs"
"github.com/spacelift-io/spacectl/internal"
"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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
}

Expand All @@ -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, ", "))
}

Expand Down
20 changes: 20 additions & 0 deletions internal/cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
1 change: 0 additions & 1 deletion internal/cmd/profile/login_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func getCredentialsType(ctx *cli.Context) (session.CredentialsType, error) {
return 0, err
}

//nolint: gosec
return session.CredentialsType(result + 1), nil

Check failure on line 103 in internal/cmd/profile/login_command.go

View workflow job for this annotation

GitHub Actions / Lint the code

G115: integer overflow conversion int -> uint (gosec)

Check failure on line 103 in internal/cmd/profile/login_command.go

View workflow job for this annotation

GitHub Actions / CodeQL

G115: integer overflow conversion int -> uint (gosec)
}

Expand Down
17 changes: 0 additions & 17 deletions internal/cmd/stack/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down Expand Up @@ -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.",
}
18 changes: 9 additions & 9 deletions internal/cmd/stack/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
}

Expand All @@ -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, ", "))
}

Expand Down
6 changes: 3 additions & 3 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit f1c18ad

Please sign in to comment.