diff --git a/internal/cmd/stack/stack.go b/internal/cmd/stack/stack.go index adf2c3d..775f241 100644 --- a/internal/cmd/stack/stack.go +++ b/internal/cmd/stack/stack.go @@ -244,6 +244,7 @@ func Command() *cli.Command { Usage: "Set current commit on the stack", Flags: []cli.Flag{ flagStackID, + flagRun, flagRequiredCommitSHA, }, Action: setCurrentCommit, @@ -259,6 +260,7 @@ func Command() *cli.Command { Usage: "Sets an environment variable.", Flags: []cli.Flag{ flagStackID, + flagRun, flagEnvironmentWriteOnly, }, Action: setVar, @@ -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, @@ -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, @@ -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, @@ -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, @@ -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, }, @@ -329,6 +336,7 @@ func Command() *cli.Command { Usage: "Open a stack in your browser", Flags: []cli.Flag{ flagStackID, + flagRun, flagIgnoreSubdir, flagCurrentBranch, flagSearchCount, @@ -405,6 +413,7 @@ func Command() *cli.Command { Usage: "Sets an environment variable.", Flags: []cli.Flag{ flagStackID, + flagRun, }, Action: resourcesList, Before: authenticated.Ensure, @@ -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, @@ -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, diff --git a/internal/cmd/stack/stack_selector.go b/internal/cmd/stack/stack_selector.go index 02c4f2d..0dc253a 100644 --- a/internal/cmd/stack/stack_selector.go +++ b/internal/cmd/stack/stack_selector.go @@ -18,6 +18,7 @@ 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) @@ -39,6 +40,16 @@ func getStack(cliCtx *cli.Context) (*stack, error) { 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 stack, nil + } else { + if cliCtx.IsSet(flagRun.Name) { + runID := cliCtx.String(flagRun.Name) + stack, err := stackGetByRunID(cliCtx.Context, runID) + if err == nil { + return stack, nil + } + + // TODO: Maybe we should log error here? + } } subdir, err := getGitRepositorySubdir() @@ -90,6 +101,25 @@ 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 { + StackByRunId struct { + stack + } `graphql:"stackByRunId(runId: $runId)"` + } + + variables := map[string]interface{}{ + "runId": graphql.ID(runID), + } + + err := authenticated.Client.Query(ctx, &query, variables) + if err != nil { + return nil, fmt.Errorf("failed to query GraphQL API when getting stack by run id: %w", err) + } + + return &query.StackByRunId.stack, nil +} + func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt bool) (*stack, error) { conditions := []structs.QueryPredicate{ {