From 4bb59fe6e29e2392c65addb098462455c3d38137 Mon Sep 17 00:00:00 2001 From: Tomas <40318863+tomasmik@users.noreply.github.com> Date: Tue, 12 Nov 2024 14:19:39 +0200 Subject: [PATCH] Allow to target resources when running local-preview (#268) --- internal/cmd/stack/flags.go | 5 +++++ internal/cmd/stack/local_preview.go | 12 ++++++++++++ internal/cmd/stack/stack.go | 1 + 3 files changed, 18 insertions(+) diff --git a/internal/cmd/stack/flags.go b/internal/cmd/stack/flags.go index 4359214..4a3d7a7 100644 --- a/internal/cmd/stack/flags.go +++ b/internal/cmd/stack/flags.go @@ -32,6 +32,11 @@ var flagNoFindRepositoryRoot = &cli.BoolFlag{ Value: false, } +var flagTarget = &cli.StringSliceFlag{ + Name: "target", + Usage: "List of targets to use during local preview, read more here: https://spacelift.io/blog/terraform-target", +} + var flagProjectRootOnly = &cli.BoolFlag{ Name: "project-root-only", Usage: "Indicate if spacelift should only package files inside this stacks project_root", diff --git a/internal/cmd/stack/local_preview.go b/internal/cmd/stack/local_preview.go index e2a9908..47c2e72 100644 --- a/internal/cmd/stack/local_preview.go +++ b/internal/cmd/stack/local_preview.go @@ -21,6 +21,18 @@ func localPreview() cli.ActionFunc { return err } + if got := cliCtx.StringSlice(flagTarget.Name); len(got) > 0 { + var val string + for _, v := range got { + val = strings.Join([]string{val, "-target=" + v}, " ") + } + + envVars = append(envVars, EnvironmentVariable{ + Key: "TF_CLI_ARGS_plan", + Value: graphql.String(strings.TrimSpace(val)), + }) + } + stack, err := getStack(cliCtx) if err != nil { return err diff --git a/internal/cmd/stack/stack.go b/internal/cmd/stack/stack.go index cff7b78..540d017 100644 --- a/internal/cmd/stack/stack.go +++ b/internal/cmd/stack/stack.go @@ -162,6 +162,7 @@ func Command() *cli.Command { flagOverrideEnvVarsTF, flagDisregardGitignore, flagPrioritizeRun, + flagTarget, }, Action: localPreview(), Before: authenticated.Ensure,