Skip to content

Commit

Permalink
Merge pull request #57 from hathora/support-nested-quotes-in-env
Browse files Browse the repository at this point in the history
Added support for more complex environmental variable values passed with --env
  • Loading branch information
gwprice115 authored Oct 18, 2024
2 parents f6f0566 + b3d690a commit d67c399
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
26 changes: 25 additions & 1 deletion internal/commands/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"strings"

"github.com/urfave/cli/v3"
"go.uber.org/zap"
Expand Down Expand Up @@ -259,7 +260,7 @@ var (
envVarsFlag = &cli.StringSliceFlag{
Name: "env",
Sources: cli.EnvVars(deploymentEnvVar("ENV")),
Usage: "`<KEY=VALUE>` formatted environment variables",
Usage: "`<KEY=VALUE>` formatted environment variables (use quotes for values with spaces or commas)",
Category: "Deployment:",
}

Expand Down Expand Up @@ -306,6 +307,7 @@ func parseContainerPorts(ports []string) ([]shared.ContainerPort, error) {
}

func parseEnvVars(envVars []string) ([]shared.DeploymentConfigV3Env, error) {
envVars = fixOverZealousCommaSplitting(envVars)
output := make([]shared.DeploymentConfigV3Env, 0, len(envVars))
for _, envVar := range envVars {
env, err := shorthand.ParseDeploymentEnvVar(envVar)
Expand All @@ -317,6 +319,28 @@ func parseEnvVars(envVars []string) ([]shared.DeploymentConfigV3Env, error) {
return output, nil
}

func fixOverZealousCommaSplitting(envVars []string) []string {
var fixedEnvVars []string
var currentPair string
for _, val := range envVars {
if strings.Contains(val, "=") && !strings.HasSuffix(currentPair, "=") {
// Start a new key-value pair
if currentPair != "" {
fixedEnvVars = append(fixedEnvVars, currentPair)
}
currentPair = val
} else {
// Append to the current key-value pair
currentPair += "," + val
}
}
if currentPair != "" {
fixedEnvVars = append(fixedEnvVars, currentPair)
}

return fixedEnvVars
}

var (
deploymentConfigKey = "commands.DeploymentConfig.DI"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/shorthand/env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func ParseDeploymentEnvVar(s string) (*shared.DeploymentConfigV3Env, error) {
return nil, fmt.Errorf("env var cannot be empty")
}

parts := strings.Split(s, "=")
parts := strings.SplitN(s, "=", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid env var format: %s", s)
}
Expand Down
13 changes: 8 additions & 5 deletions internal/shorthand/env_var_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ func Test_DeploymentEnvVarShorthand(t *testing.T) {
Value: "VALUE",
},
},
{
name: "name and value and extra",
input: "NAME=VALUE=EXTRA",
expectErr: true,
},
{
name: "key only",
input: "KEY",
Expand All @@ -48,6 +43,14 @@ func Test_DeploymentEnvVarShorthand(t *testing.T) {
input: "",
expectErr: true,
},
{
name: "nested flag",
input: `KEY=-SomeFlag="With Spaces,And Commas"`,
expect: &shared.DeploymentConfigV3Env{
Name: "KEY",
Value: `-SomeFlag="With Spaces,And Commas"`,
},
},
}

for _, tt := range tests {
Expand Down

0 comments on commit d67c399

Please sign in to comment.