Skip to content

Commit

Permalink
feat: Allow to provide run ID instead of stack ID in some command (#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
0michalsokolowski0 authored Sep 13, 2024
1 parent f73c926 commit 860bc9f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/security.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
uses: actions/checkout@v4

- name: Run GoSec Security Scanner
uses: securego/gosec@master
uses: securego/gosec@v2.21.0
with:
args: "-severity=medium -no-fail -fmt sarif -out gosec-results.sarif ./..."

Expand Down
11 changes: 11 additions & 0 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func Command() *cli.Command {
Usage: "Set current commit on the stack",
Flags: []cli.Flag{
flagStackID,
flagRun,
flagRequiredCommitSHA,
},
Action: setCurrentCommit,
Expand All @@ -259,6 +260,7 @@ func Command() *cli.Command {
Usage: "Sets an environment variable.",
Flags: []cli.Flag{
flagStackID,
flagRun,
flagEnvironmentWriteOnly,
},
Action: setVar,
Expand All @@ -270,6 +272,7 @@ func Command() *cli.Command {
Usage: "Lists all the environment variables and mounted files for a stack.",
Flags: []cli.Flag{
flagStackID,
flagRun,
cmd.FlagOutputFormat,
},
Action: (&listEnvCommand{}).listEnv,
Expand All @@ -280,6 +283,7 @@ func Command() *cli.Command {
Usage: "Mount a file from existing file or STDIN.",
Flags: []cli.Flag{
flagStackID,
flagRun,
flagEnvironmentWriteOnly,
},
Action: mountFile,
Expand All @@ -291,6 +295,7 @@ func Command() *cli.Command {
Usage: "Deletes an environment variable or mounted file.",
Flags: []cli.Flag{
flagStackID,
flagRun,
},
Action: deleteEnvironment,
Before: authenticated.Ensure,
Expand All @@ -303,6 +308,7 @@ func Command() *cli.Command {
Usage: "Shows current outputs for a specific stack. Does not show the value of sensitive outputs.",
Flags: []cli.Flag{
flagStackID,
flagRun,
flagOutputID,
cmd.FlagOutputFormat,
cmd.FlagNoColor,
Expand All @@ -316,6 +322,7 @@ func Command() *cli.Command {
Usage: "Shows detailed information about a specific stack",
Flags: []cli.Flag{
flagStackID,
flagRun,
cmd.FlagOutputFormat,
cmd.FlagNoColor,
},
Expand All @@ -329,6 +336,7 @@ func Command() *cli.Command {
Usage: "Open a stack in your browser",
Flags: []cli.Flag{
flagStackID,
flagRun,
flagIgnoreSubdir,
flagCurrentBranch,
flagSearchCount,
Expand Down Expand Up @@ -405,6 +413,7 @@ func Command() *cli.Command {
Usage: "Sets an environment variable.",
Flags: []cli.Flag{
flagStackID,
flagRun,
},
Action: resourcesList,
Before: authenticated.Ensure,
Expand All @@ -421,6 +430,7 @@ func Command() *cli.Command {
Usage: "Get stacks which the provided that depends on",
Flags: []cli.Flag{
flagStackID,
flagRun,
cmd.FlagOutputFormat,
},
Action: dependenciesOn,
Expand All @@ -432,6 +442,7 @@ func Command() *cli.Command {
Usage: "Get stacks that depend on the provided stack",
Flags: []cli.Flag{
flagStackID,
flagRun,
cmd.FlagOutputFormat,
},
Action: dependenciesOff,
Expand Down
42 changes: 40 additions & 2 deletions internal/cmd/stack/stack_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ import (
"github.com/urfave/cli/v2"
)

var errNoStackFound = errors.New("no stack found")
var (
errNoStackFound = errors.New("no stack found")
)

// getStackID will try to retrieve a stack ID from multiple sources.
// It will do so in the following order:
// 1. Check the --id flag, if set, use that value.
// 2. Check the --run flag, if set, try to get the stack associated with the run.
// 2. Check the current directory to determine repository and subdirectory and search for a stack.
func getStackID(cliCtx *cli.Context) (string, error) {
stack, err := getStack(cliCtx)
Expand All @@ -32,12 +35,24 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
if cliCtx.IsSet(flagStackID.Name) {
stackID := cliCtx.String(flagStackID.Name)
stack, err := stackGetByID(cliCtx.Context, stackID)
if errors.Is(err, errNoStackFound) {
return nil, fmt.Errorf("stack with id %q could not be found. Please check that the stack exists and that you have access to it. To list available stacks run: spacectl stack list", stackID)
}
if err != nil {
return nil, fmt.Errorf("failed to check if stack exists: %w", err)
}

return stack, nil
} else if cliCtx.IsSet(flagRun.Name) {
runID := cliCtx.String(flagRun.Name)
stack, err := stackGetByRunID(cliCtx.Context, runID)
if errors.Is(err, errNoStackFound) {
return nil, fmt.Errorf("stack with id %q could not be found. Please check that the stack exists and that you have access to it. To list available stacks run: spacectl stack list", stackID)
return nil, fmt.Errorf("run with id %q was not found. Please check that the run exists and that you have access to it. To list available stacks run: spacectl stack run list", runID)
}
if err != nil {
return nil, fmt.Errorf("failed to get stack by run id: %w", err)
}

return stack, nil
}

Expand Down Expand Up @@ -90,6 +105,29 @@ func stackGetByID(ctx context.Context, stackID string) (*stack, error) {
return &query.Stack.stack, nil
}

func stackGetByRunID(ctx context.Context, runID string) (*stack, error) {
var query struct {
RunStack struct {
stack
} `graphql:"runStack(runId: $runId)"`
}

variables := map[string]interface{}{
"runId": graphql.ID(runID),
}

err := authenticated.Client.Query(ctx, &query, variables)
if err != nil {
if err.Error() == "not found" {
return nil, errNoStackFound
}

return nil, fmt.Errorf("failed to query GraphQL API when getting stack by run id: %w", err)
}

return &query.RunStack.stack, nil
}

func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt bool) (*stack, error) {
conditions := []structs.QueryPredicate{
{
Expand Down

0 comments on commit 860bc9f

Please sign in to comment.