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 2 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
54 changes: 47 additions & 7 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 @@ -31,14 +34,28 @@ func getStackID(cliCtx *cli.Context) (string, error) {
func getStack(cliCtx *cli.Context) (*stack, error) {
if cliCtx.IsSet(flagStackID.Name) {
stackID := cliCtx.String(flagStackID.Name)
stack, err := stackGetByID(cliCtx.Context, stackID)
stack, found, err := stackGetByID(cliCtx.Context, stackID)
if err != nil {
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

if !found {
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, found, err := stackGetByRunID(cliCtx.Context, runID)
Copy link
Contributor

@tomasmik tomasmik Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the double logic (using bool here and not using it the other function)? just return the same error as we do in the previous method maybe?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if err != nil {
fmt.Printf("Failed to get stack by run id: %v\n", err)
mbialon marked this conversation as resolved.
Show resolved Hide resolved
} else {
if !found {
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 stack, nil
}
}

subdir, err := getGitRepositorySubdir()
Expand Down Expand Up @@ -67,7 +84,7 @@ func getStack(cliCtx *cli.Context) (*stack, error) {
return got, nil
}

func stackGetByID(ctx context.Context, stackID string) (*stack, error) {
func stackGetByID(ctx context.Context, stackID string) (*stack, bool, error) {
var query struct {
Stack struct {
stack
Expand All @@ -80,14 +97,37 @@ func stackGetByID(ctx context.Context, stackID string) (*stack, error) {

err := authenticated.Client.Query(ctx, &query, variables)
if err != nil {
return nil, fmt.Errorf("failed to query GraphQL API when checking if a stack exists: %w", err)
return nil, false, fmt.Errorf("failed to query GraphQL API when checking if a stack exists: %w", err)
}

if query.Stack.ID != stackID {
return nil, errNoStackFound
return nil, false, nil
}

return &query.Stack.stack, true, nil
}

func stackGetByRunID(ctx context.Context, runID string) (*stack, bool, 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, false, nil
}

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

return &query.Stack.stack, nil
return &query.RunStack.stack, true, nil
}

func findAndSelectStack(ctx context.Context, p *stackSearchParams, forcePrompt bool) (*stack, error) {
Expand Down
Loading