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..571c053 100644 --- a/internal/cmd/stack/local_preview.go +++ b/internal/cmd/stack/local_preview.go @@ -119,12 +119,27 @@ 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) { + var prioritizeMutation setRunPriorityMutation + variables := map[string]interface{}{ + "stackId": graphql.ID(stack.ID), + "runId": graphql.ID(triggerMutation.RunProposeLocalWorkspace.ID), + "prioritize": graphql.Boolean(true), + } + + if err = authenticated.Client.Mutate(ctx, &prioritizeMutation, variables); err != nil { + return err + } + + fmt.Print("The run has been successfully prioritized!\n") + } + linkToRun := authenticated.Client.URL( "/stack/%s/run/%s", stack.ID, diff --git a/internal/cmd/stack/run_prioritize.go b/internal/cmd/stack/run_prioritize.go new file mode 100644 index 0000000..97b6f1d --- /dev/null +++ b/internal/cmd/stack/run_prioritize.go @@ -0,0 +1,52 @@ +package stack + +import ( + "fmt" + "github.com/shurcooL/graphql" + "github.com/spacelift-io/spacectl/internal/cmd/authenticated" + "github.com/urfave/cli/v2" +) + +type setRunPriorityMutation struct { + SetRunPriority struct { + ID string `graphql:"id"` + } `graphql:"runPrioritizeSet(stack: $stackId, run: $runId, prioritize: $prioritize)"` +} + +func runPrioritize(cliCtx *cli.Context) error { + stackID, err := getStackID(cliCtx) + if err != nil { + return err + } + runID := cliCtx.String(flagRequiredRun.Name) + + var mutation setRunPriorityMutation + + variables := map[string]interface{}{ + "stackId": graphql.ID(stackID), + "runId": graphql.ID(runID), + "prioritize": graphql.Boolean(true), + } + + if err = authenticated.Client.Mutate(cliCtx.Context, &mutation, variables); 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() +} diff --git a/internal/cmd/stack/stack.go b/internal/cmd/stack/stack.go index d4c76fe..9fb53f6 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,19 @@ 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, + }, { Name: "run", Usage: "Manage a stack's runs",