Skip to content

Commit

Permalink
feat: Add new commands for prioritizing/deprioritizing a run and flag…
Browse files Browse the repository at this point in the history
… to prioritize local-preview (#243)
  • Loading branch information
0michalsokolowski0 authored Aug 27, 2024
1 parent deba5a0 commit 146bf07
Show file tree
Hide file tree
Showing 8 changed files with 147 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
12 changes: 11 additions & 1 deletion internal/cmd/stack/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
39 changes: 39 additions & 0 deletions internal/cmd/stack/run_deprioritize.go
Original file line number Diff line number Diff line change
@@ -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()
}
62 changes: 62 additions & 0 deletions internal/cmd/stack/run_prioritize.go
Original file line number Diff line number Diff line change
@@ -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
}
27 changes: 27 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,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",
Expand Down

0 comments on commit 146bf07

Please sign in to comment.