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
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
36 changes: 18 additions & 18 deletions internal/cmd/stack/stack_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +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, found, err := stackGetByID(cliCtx.Context, stackID)
stack, 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
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 !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 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, found, err := stackGetByRunID(cliCtx.Context, runID)
stack, err := stackGetByRunID(cliCtx.Context, runID)
if err != nil {
fmt.Printf("Failed to get stack by run id: %v\n", err)
} else {
if !found {
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 stack, nil
return nil, fmt.Errorf("failed to get stack by run id: %w", err)
}

return stack, nil
}

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

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

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

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

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

func stackGetByRunID(ctx context.Context, runID string) (*stack, bool, error) {
func stackGetByRunID(ctx context.Context, runID string) (*stack, error) {
var query struct {
RunStack struct {
stack
Expand All @@ -121,13 +121,13 @@ func stackGetByRunID(ctx context.Context, runID string) (*stack, bool, error) {
err := authenticated.Client.Query(ctx, &query, variables)
if err != nil {
if err.Error() == "not found" {
return nil, false, nil
return nil, errNoStackFound
}

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

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

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