Skip to content

Commit

Permalink
Add a way to view stack dependencies (#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasmik authored Oct 4, 2023
1 parent 4e8ad84 commit 4b0a77d
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 0 deletions.
116 changes: 116 additions & 0 deletions internal/cmd/stack/dependencies.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package stack

import (
"fmt"

"github.com/pkg/errors"
"github.com/shurcooL/graphql"
"github.com/urfave/cli/v2"

"github.com/spacelift-io/spacectl/internal/cmd"
"github.com/spacelift-io/spacectl/internal/cmd/authenticated"
)

func dependenciesOn(cliCtx *cli.Context) error {
outputFormat, err := cmd.GetOutputFormat(cliCtx)
if err != nil {
return err
}

got, err := dependenciesListOneStack(cliCtx)
if err != nil {
return err
}

switch outputFormat {
case cmd.OutputFormatTable:
return cmd.OutputTable(got.dependsOnTableData(), true)
case cmd.OutputFormatJSON:
return cmd.OutputJSON(got.DependsOn)
}

return fmt.Errorf("unknown output format: %v", outputFormat)
}

func dependenciesOff(cliCtx *cli.Context) error {
outputFormat, err := cmd.GetOutputFormat(cliCtx)
if err != nil {
return err
}

got, err := dependenciesListOneStack(cliCtx)
if err != nil {
return err
}

switch outputFormat {
case cmd.OutputFormatTable:
return cmd.OutputTable(got.dependedOnByTableData(), true)
case cmd.OutputFormatJSON:
return cmd.OutputJSON(got.IsDependedOnBy)
}

return fmt.Errorf("unknown output format: %v", outputFormat)
}

func dependenciesListOneStack(cliCtx *cli.Context) (*stackWithDependencies, error) {
id, err := getStackID(cliCtx)
if err != nil {
return nil, err
}

var query struct {
Stack stackWithDependencies `graphql:"stack(id: $id)"`
}

variables := map[string]any{"id": graphql.ID(id)}
if err := authenticated.Client.Query(cliCtx.Context, &query, variables); err != nil {
return nil, errors.Wrap(err, "failed to query one stack")
}

return &query.Stack, nil
}

type stackWithDependencies struct {
ID string `graphql:"id" json:"id"`
Labels []string `graphql:"labels" json:"labels"`
Space string `graphql:"space" json:"space"`
Name string `graphql:"name" json:"name"`

DependsOn []stackDependency `graphql:"dependsOn" json:"dependsOn"`
IsDependedOnBy []stackDependency `graphql:"isDependedOnBy" json:"isDependedOnBy"`
}

type stackDependency struct {
Stack struct {
ID string `graphql:"id" json:"id"`
Name string `graphql:"name" json:"name"`
} `graphql:"stack" json:"stack"`

DependsOnStack struct {
ID string `graphql:"id" json:"id"`
Name string `graphql:"name" json:"name"`
} `graphql:"dependsOnStack" json:"dependsOnStack"`
}

func (s *stackWithDependencies) dependsOnTableData() [][]string {
columns := []string{"Name", "ID"}

tableData := [][]string{columns}
for _, dependency := range s.DependsOn {
tableData = append(tableData, []string{dependency.DependsOnStack.Name, dependency.DependsOnStack.ID})
}

return tableData
}

func (s *stackWithDependencies) dependedOnByTableData() [][]string {
columns := []string{"Name", "ID"}

tableData := [][]string{columns}
for _, dependency := range s.IsDependedOnBy {
tableData = append(tableData, []string{dependency.Stack.Name, dependency.Stack.ID})
}

return tableData
}
28 changes: 28 additions & 0 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,34 @@ func Command() *cli.Command {
},
},
},
{
Name: "dependencies",
Usage: "View stack dependencies",
Subcommands: []*cli.Command{
{
Name: "on",
Usage: "Get stacks which the provided that depends on",
Flags: []cli.Flag{
flagStackID,
cmd.FlagOutputFormat,
},
Action: dependenciesOn,
Before: authenticated.Ensure,
ArgsUsage: cmd.EmptyArgsUsage,
},
{
Name: "off",
Usage: "Get stacks that depend on the provided stack",
Flags: []cli.Flag{
flagStackID,
cmd.FlagOutputFormat,
},
Action: dependenciesOff,
Before: authenticated.Ensure,
ArgsUsage: cmd.EmptyArgsUsage,
},
},
},
},
}
}

0 comments on commit 4b0a77d

Please sign in to comment.