Skip to content

Commit

Permalink
Allow to pass env vars for local preview (#145)
Browse files Browse the repository at this point in the history
* Allow to pass env vars for local preview
* comment fixes
  • Loading branch information
tomasmik authored May 23, 2023
1 parent dc3a144 commit b512ca8
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
10 changes: 10 additions & 0 deletions internal/cmd/stack/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,13 @@ var flagShowLabels = &cli.BoolFlag{
Required: false,
Value: false,
}

var flagOverrideEnvVarsTF = &cli.StringSliceFlag{
Name: "tf-env-var-override",
Usage: "[Optional] Terraform environment variables injected into the run at runtime, they will be prefixed with TF_ by default, example: --tf-env-var-override 'foo=bar,bar=baz'",
}

var flagOverrideEnvVars = &cli.StringSliceFlag{
Name: "env-var-override",
Usage: "[Optional] Environment variables injected into the run at runtime, example: --env-var-override 'foo=bar,bar=baz'",
}
58 changes: 55 additions & 3 deletions internal/cmd/stack/local_preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/mholt/archiver/v3"
"github.com/shurcooL/graphql"
Expand All @@ -16,10 +17,16 @@ import (

func localPreview() cli.ActionFunc {
return func(cliCtx *cli.Context) error {
envVars, err := parseEnvVariablesForLocalPreview(cliCtx)
if err != nil {
return err
}

stackID, err := getStackID(cliCtx)
if err != nil {
return err
}

ctx := context.Background()

var packagePath *string = nil
Expand Down Expand Up @@ -88,12 +95,13 @@ func localPreview() cli.ActionFunc {
var triggerMutation struct {
RunProposeLocalWorkspace struct {
ID string `graphql:"id"`
} `graphql:"runProposeLocalWorkspace(stack: $stack, workspace: $workspace)"`
} `graphql:"runProposeLocalWorkspace(stack: $stack, workspace: $workspace, environmentVarsOverrides: $environmentVarsOverrides)"`
}

triggerVariables := map[string]interface{}{
"stack": graphql.ID(stackID),
"workspace": graphql.ID(uploadMutation.UploadLocalWorkspace.ID),
"stack": graphql.ID(stackID),
"workspace": graphql.ID(uploadMutation.UploadLocalWorkspace.ID),
"environmentVarsOverrides": envVars,
}

var requestOpts []graphql.RequestOption
Expand Down Expand Up @@ -125,3 +133,47 @@ func localPreview() cli.ActionFunc {
return terminal.Error()
}
}

// EnvironmentVariable represents a key-value pair of environment variables
type EnvironmentVariable struct {
Key graphql.String `json:"key"`
Value graphql.String `json:"value"`
}

func parseEnvVariablesForLocalPreview(cliCtx *cli.Context) ([]EnvironmentVariable, error) {
envVars := make([]EnvironmentVariable, 0)

parseFn := func(ev string, mutateKey func(string) string) error {
parts := strings.Split(ev, "=")
if len(parts) != 2 {
return fmt.Errorf("invalid environment variable %q, must be in the form of KEY=VALUE", ev)
}

if mutateKey != nil {
parts[0] = mutateKey(parts[0])
}

envVars = append(envVars, EnvironmentVariable{
Key: graphql.String(parts[0]),
Value: graphql.String(parts[1]),
})

return nil
}

for _, ev := range cliCtx.StringSlice(flagOverrideEnvVars.Name) {
if err := parseFn(ev, nil); err != nil {
return nil, err
}
}

for _, ev := range cliCtx.StringSlice(flagOverrideEnvVarsTF.Name) {
if err := parseFn(ev, func(s string) string {
return strings.Join([]string{"TF_", s}, "")
}); err != nil {
return nil, err
}
}

return envVars, nil
}
2 changes: 2 additions & 0 deletions internal/cmd/stack/stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func Command() *cli.Command {
flagRunMetadata,
flagNoTail,
flagNoUpload,
flagOverrideEnvVars,
flagOverrideEnvVarsTF,
},
Action: localPreview(),
Before: authenticated.Ensure,
Expand Down

0 comments on commit b512ca8

Please sign in to comment.