Skip to content

Commit

Permalink
feat: Add new command for prioriting a run and flag to prioritize loc…
Browse files Browse the repository at this point in the history
…al-preview
  • Loading branch information
0michalsokolowski0 committed Aug 26, 2024
1 parent deba5a0 commit 89a33d3
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ key.*
completions

*.csv
.idea
2 changes: 1 addition & 1 deletion internal/cmd/authenticated/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/stack/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions internal/cmd/stack/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
17 changes: 16 additions & 1 deletion internal/cmd/stack/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
52 changes: 52 additions & 0 deletions internal/cmd/stack/run_prioritize.go
Original file line number Diff line number Diff line change
@@ -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()
}
14 changes: 14 additions & 0 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func Command() *cli.Command {
flagOverrideEnvVars,
flagOverrideEnvVarsTF,
flagDisregardGitignore,
flagPrioritizeRun,
},
Action: localPreview(),
Before: authenticated.Ensure,
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 89a33d3

Please sign in to comment.