Skip to content

Commit

Permalink
Initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
0michalsokolowski0 committed Aug 30, 2024
1 parent f73c926 commit 810f764
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
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
30 changes: 30 additions & 0 deletions internal/cmd/stack/stack_selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {

Check failure on line 43 in internal/cmd/stack/stack_selector.go

View workflow job for this annotation

GitHub Actions / Lint the code

elseif: can replace 'else {if cond {}}' with 'else if cond {}' (gocritic)
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()
Expand Down Expand Up @@ -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{
{
Expand Down

0 comments on commit 810f764

Please sign in to comment.