diff --git a/.gitignore b/.gitignore index 4e5d868..166e791 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,4 @@ key.* completions *.csv +.idea diff --git a/internal/cmd/authenticated/client.go b/internal/cmd/authenticated/client.go index 8810ef9..3189c70 100644 --- a/internal/cmd/authenticated/client.go +++ b/internal/cmd/authenticated/client.go @@ -36,7 +36,7 @@ var Client client.Client // Ensure is a way of ensuring that the Client exists, and it meant to be used // as a Before action for commands that need it. -func Ensure(*cli.Context) error { +func Ensure(_ *cli.Context) error { ctx, httpClient := session.Defaults() if err := configureTLS(httpClient); err != nil { diff --git a/internal/cmd/stack/environment.go b/internal/cmd/stack/environment.go index 2c80f82..b352eef 100644 --- a/internal/cmd/stack/environment.go +++ b/internal/cmd/stack/environment.go @@ -207,7 +207,7 @@ func (e *configElement) toConfigElementOutput(contextName *string) (listEnvEleme if err != nil { message := fmt.Sprintf("failed to decode base64-encoded file with id %s", e.ID) - return listEnvElementOutput{}, errors.Wrapf(err, message) + return listEnvElementOutput{}, errors.Wrap(err, message) } stringValue := string(result) diff --git a/internal/cmd/stack/flags.go b/internal/cmd/stack/flags.go index b2b5439..55cc426 100644 --- a/internal/cmd/stack/flags.go +++ b/internal/cmd/stack/flags.go @@ -147,6 +147,11 @@ var flagResources = &cli.StringSliceFlag{ Usage: "[Optional] A comma separeted list of resources to be used when applying, example: 'aws_instance.foo'", } +var flagPrioritizeRun = &cli.BoolFlag{ + Name: "prioritize-run", + Usage: "[Optional] Indicate whether to prioritize the run", +} + var flagInteractive = &cli.BoolFlag{ Name: "interactive", Aliases: []string{"i"}, diff --git a/internal/cmd/stack/local_preview.go b/internal/cmd/stack/local_preview.go index 183fc2b..5bfa226 100644 --- a/internal/cmd/stack/local_preview.go +++ b/internal/cmd/stack/local_preview.go @@ -119,12 +119,22 @@ func localPreview() cli.ActionFunc { requestOpts = append(requestOpts, graphql.WithHeader(internal.UserProvidedRunMetadataHeader, cliCtx.String(flagRunMetadata.Name))) } - if err := authenticated.Client.Mutate(ctx, &triggerMutation, triggerVariables, requestOpts...); err != nil { + if err = authenticated.Client.Mutate(ctx, &triggerMutation, triggerVariables, requestOpts...); err != nil { return err } fmt.Println("You have successfully created a local preview run!") + if cliCtx.Bool(flagPrioritizeRun.Name) { + _, err = setRunPriority(cliCtx, stack.ID, triggerMutation.RunProposeLocalWorkspace.ID, true) + if err != nil { + fmt.Printf("Failed to prioritize the run due to err: %v\n", err) + fmt.Println("Resolve the issue and prioritize the run manually") + } else { + fmt.Println("The run has been successfully prioritized!") + } + } + linkToRun := authenticated.Client.URL( "/stack/%s/run/%s", stack.ID, diff --git a/internal/cmd/stack/run_deprioritize.go b/internal/cmd/stack/run_deprioritize.go new file mode 100644 index 0000000..83bbb27 --- /dev/null +++ b/internal/cmd/stack/run_deprioritize.go @@ -0,0 +1,39 @@ +package stack + +import ( + "fmt" + + "github.com/spacelift-io/spacectl/internal/cmd/authenticated" + "github.com/urfave/cli/v2" +) + +func runDeprioritize(cliCtx *cli.Context) error { + stackID, err := getStackID(cliCtx) + if err != nil { + return err + } + runID := cliCtx.String(flagRequiredRun.Name) + + mutation, err := setRunPriority(cliCtx, stackID, runID, false) + if err != nil { + return err + } + + fmt.Printf("Run ID %q has been successfully deprioritized\n", runID) + fmt.Println("The live run can be visited at", authenticated.Client.URL( + "/stack/%s/run/%s", + stackID, + mutation.SetRunPriority.ID, + )) + + if !cliCtx.Bool(flagTail.Name) { + return nil + } + + terminal, err := runLogsWithAction(cliCtx.Context, stackID, mutation.SetRunPriority.ID, nil) + if err != nil { + return err + } + + return terminal.Error() +} diff --git a/internal/cmd/stack/run_prioritize.go b/internal/cmd/stack/run_prioritize.go new file mode 100644 index 0000000..f516091 --- /dev/null +++ b/internal/cmd/stack/run_prioritize.go @@ -0,0 +1,62 @@ +package stack + +import ( + "fmt" + + "github.com/shurcooL/graphql" + "github.com/spacelift-io/spacectl/internal/cmd/authenticated" + "github.com/urfave/cli/v2" +) + +func runPrioritize(cliCtx *cli.Context) error { + stackID, err := getStackID(cliCtx) + if err != nil { + return err + } + runID := cliCtx.String(flagRequiredRun.Name) + + mutation, err := setRunPriority(cliCtx, stackID, runID, true) + if err != nil { + return err + } + + fmt.Printf("Run ID %q has been successfully prioritized\n", runID) + fmt.Println("The live run can be visited at", authenticated.Client.URL( + "/stack/%s/run/%s", + stackID, + mutation.SetRunPriority.ID, + )) + + if !cliCtx.Bool(flagTail.Name) { + return nil + } + + terminal, err := runLogsWithAction(cliCtx.Context, stackID, mutation.SetRunPriority.ID, nil) + if err != nil { + return err + } + + return terminal.Error() +} + +type setRunPriorityMutation struct { + SetRunPriority struct { + ID string `graphql:"id"` + } `graphql:"runPrioritizeSet(stack: $stackId, run: $runId, prioritize: $prioritize)"` +} + +func setRunPriority(cliCtx *cli.Context, stackID, runID string, prioritize bool) (setRunPriorityMutation, error) { + var mutation setRunPriorityMutation + + variables := map[string]interface{}{ + "stackId": graphql.ID(stackID), + "runId": graphql.ID(runID), + "prioritize": graphql.Boolean(prioritize), + } + + if err := authenticated.Client.Mutate(cliCtx.Context, &mutation, variables); err != nil { + return setRunPriorityMutation{}, err + } + + return mutation, nil +} diff --git a/internal/cmd/stack/stack.go b/internal/cmd/stack/stack.go index d4c76fe..f6d63ab 100644 --- a/internal/cmd/stack/stack.go +++ b/internal/cmd/stack/stack.go @@ -160,6 +160,7 @@ func Command() *cli.Command { flagOverrideEnvVars, flagOverrideEnvVarsTF, flagDisregardGitignore, + flagPrioritizeRun, }, Action: localPreview(), Before: authenticated.Ensure, @@ -192,6 +193,32 @@ func Command() *cli.Command { Before: authenticated.Ensure, ArgsUsage: cmd.EmptyArgsUsage, }, + { + Category: "Run management", + Name: "prioritize", + Usage: "Prioritize a run", + Flags: []cli.Flag{ + flagStackID, + flagRequiredRun, + flagTail, + }, + Action: runPrioritize, + Before: authenticated.Ensure, + ArgsUsage: cmd.EmptyArgsUsage, + }, + { + Category: "Run management", + Name: "deprioritize", + Usage: "Deprioritize a run", + Flags: []cli.Flag{ + flagStackID, + flagRequiredRun, + flagTail, + }, + Action: runDeprioritize, + Before: authenticated.Ensure, + ArgsUsage: cmd.EmptyArgsUsage, + }, { Name: "run", Usage: "Manage a stack's runs",