Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow to provide run ID instead of stack ID in some command #253

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
46 changes: 43 additions & 3 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 @@ -33,11 +36,25 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
stackID := cliCtx.String(flagStackID.Name)
stack, err := stackGetByID(cliCtx.Context, stackID)
if err != nil {
if errors.Is(err, errNoStackFound) {
mbialon marked this conversation as resolved.
Show resolved Hide resolved
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("failed to check if stack exists: %w", err)
}
if errors.Is(err, errNoStackFound) {
mbialon marked this conversation as resolved.
Show resolved Hide resolved
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 {
if errors.Is(err, errNoStackFound) {
mbialon marked this conversation as resolved.
Show resolved Hide resolved
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)
}

return nil, fmt.Errorf("failed to get stack by run id: %w", err)
}

return stack, nil
}

Expand Down Expand Up @@ -90,6 +107,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
Loading