From 2f65b4123ca25cc6e16084f698bf96a7b3e6586f Mon Sep 17 00:00:00 2001 From: Kuba Martin Date: Mon, 19 Jun 2023 14:35:43 +0200 Subject: [PATCH] Add support for external run dependencies. (#146) * Add support for run external dependencies. Signed-off-by: Jakub Martin * Refactor graphql schema. Signed-off-by: Jakub Martin * Rename command. Signed-off-by: Jakub Martin * Fixes. Signed-off-by: Jakub Martin * Fix lint. Signed-off-by: Jakub Martin * Fix lint Signed-off-by: Jakub Martin * Fix lint? Signed-off-by: Jakub Martin --------- Signed-off-by: Jakub Martin --- go.mod | 1 + go.sum | 1 + internal/cmd/run_external_dependency/flags.go | 15 +++++++ .../run_external_dependency/mark_completed.go | 40 +++++++++++++++++++ .../run_external_dependency.go | 30 ++++++++++++++ main.go | 2 + 6 files changed, 89 insertions(+) create mode 100644 internal/cmd/run_external_dependency/flags.go create mode 100644 internal/cmd/run_external_dependency/mark_completed.go create mode 100644 internal/cmd/run_external_dependency/run_external_dependency.go diff --git a/go.mod b/go.mod index 67dfd0c..7352e79 100644 --- a/go.mod +++ b/go.mod @@ -20,6 +20,7 @@ require ( github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06 github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 github.com/urfave/cli/v2 v2.25.1 + golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 golang.org/x/net v0.8.0 golang.org/x/oauth2 v0.6.0 golang.org/x/sync v0.1.0 diff --git a/go.sum b/go.sum index 9b558b5..6b2355e 100644 --- a/go.sum +++ b/go.sum @@ -211,6 +211,7 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y golang.org/x/crypto v0.7.0 h1:AvwMYaRytfdeVt3u6mLaxYtErKYjxA2OXjJ1HHq6t3A= golang.org/x/crypto v0.7.0/go.mod h1:pYwdfH91IfpZVANVyUOhSIPZaFoJGxTFbZhFTx+dXZU= golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561 h1:MDc5xs78ZrZr3HMQugiXOAkSZtfTpbJLDr/lwfgO53E= +golang.org/x/exp v0.0.0-20220909182711-5c715a9e8561/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= diff --git a/internal/cmd/run_external_dependency/flags.go b/internal/cmd/run_external_dependency/flags.go new file mode 100644 index 0000000..9f6cae2 --- /dev/null +++ b/internal/cmd/run_external_dependency/flags.go @@ -0,0 +1,15 @@ +package runexternaldependency + +import "github.com/urfave/cli/v2" + +var flagRunExternalDependencyID = &cli.StringFlag{ + Name: "id", + Usage: "[Required] ID of the external dependency", + Required: true, +} + +var flagStatus = &cli.StringFlag{ + Name: "status", + Usage: "[Required] Status of the external dependency (one of: 'finished', 'failed')", + Required: true, +} diff --git a/internal/cmd/run_external_dependency/mark_completed.go b/internal/cmd/run_external_dependency/mark_completed.go new file mode 100644 index 0000000..a0a08f2 --- /dev/null +++ b/internal/cmd/run_external_dependency/mark_completed.go @@ -0,0 +1,40 @@ +package runexternaldependency + +import ( + "fmt" + "strings" + + "github.com/urfave/cli/v2" + "golang.org/x/exp/slices" + + "github.com/spacelift-io/spacectl/internal/cmd/authenticated" +) + +func markRunExternalDependencyAsCompleted(cliCtx *cli.Context) error { + externalDependencyID := cliCtx.String(flagRunExternalDependencyID.Name) + status := cliCtx.String(flagStatus.Name) + + var mutation struct { + RunExternalDependencyMarkAsFinished struct { + Phantom bool `graphql:"phantom"` + } `graphql:"runExternalDependencyMarkAsCompleted(dependency: $dependency, status: $status)"` + } + + if !slices.Contains([]string{"finished", "failed"}, status) { + return fmt.Errorf("status must be one of: finished, failed") + } + + type RunExternalDependencyStatus string + variables := map[string]interface{}{ + "dependency": externalDependencyID, + "status": RunExternalDependencyStatus(strings.ToUpper(status)), + } + + if err := authenticated.Client.Mutate(cliCtx.Context, &mutation, variables); err != nil { + return err + } + + fmt.Printf("Marked external dependency as %s\n", status) + + return nil +} diff --git a/internal/cmd/run_external_dependency/run_external_dependency.go b/internal/cmd/run_external_dependency/run_external_dependency.go new file mode 100644 index 0000000..b35be30 --- /dev/null +++ b/internal/cmd/run_external_dependency/run_external_dependency.go @@ -0,0 +1,30 @@ +package runexternaldependency + +import ( + "github.com/urfave/cli/v2" + + "github.com/spacelift-io/spacectl/internal/cmd" + "github.com/spacelift-io/spacectl/internal/cmd/authenticated" +) + +// Command encapsulates the run external dependency command subtree. +func Command() *cli.Command { + return &cli.Command{ + Name: "run-external-dependency", + Usage: "Manage Spacelift Run external dependencies", + Subcommands: []*cli.Command{ + { + Category: "Run external dependency management", + Name: "mark-completed", + Usage: "Mark Run external dependency as completed", + Flags: []cli.Flag{ + flagRunExternalDependencyID, + flagStatus, + }, + Action: markRunExternalDependencyAsCompleted, + Before: authenticated.Ensure, + ArgsUsage: cmd.EmptyArgsUsage, + }, + }, + } +} diff --git a/main.go b/main.go index e04e2be..82cb9db 100644 --- a/main.go +++ b/main.go @@ -10,6 +10,7 @@ import ( "github.com/spacelift-io/spacectl/internal/cmd/module" "github.com/spacelift-io/spacectl/internal/cmd/profile" "github.com/spacelift-io/spacectl/internal/cmd/provider" + "github.com/spacelift-io/spacectl/internal/cmd/run_external_dependency" "github.com/spacelift-io/spacectl/internal/cmd/stack" versioncmd "github.com/spacelift-io/spacectl/internal/cmd/version" "github.com/spacelift-io/spacectl/internal/cmd/whoami" @@ -33,6 +34,7 @@ func main() { module.Command(), profile.Command(), provider.Command(), + runexternaldependency.Command(), stack.Command(), whoami.Command(), versioncmd.Command(version),