Skip to content

Commit

Permalink
Merge branch 'sethpollack-master'
Browse files Browse the repository at this point in the history
  • Loading branch information
danenania committed Nov 15, 2018
2 parents 7d19abf + 4fd5f6e commit 26425f6
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ echo "$SOME_VAR"
--cache cache encrypted config as a local backup (default is true when .env file exists, false otherwise)
--no-cache do NOT cache encrypted config as a local backup even when .env file exists
--cache-dir string cache directory (default is $HOME/.envkey/cache)
--dot-env-compatible change output to .env format
--env-file string ENVKEY-containing env file name (default ".env")
--pam-compatible change output format to be compatible with /etc/environment on Linux
-f, --force overwrite existing environment variables and/or other entries in .env file
Expand Down
6 changes: 4 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var shouldNotCache bool
var force bool
var printVersion bool
var pamCompatible bool
var dotEnvCompatible bool
var verboseOutput bool
var timeoutSeconds float64
var retries uint8
Expand Down Expand Up @@ -71,11 +72,11 @@ You can also pass an ENVKEY directly (not recommended for real workflows):

opts := fetch.FetchOptions{cacheEnabled, cacheDir, "envkey-source", version.Version, verboseOutput, timeoutSeconds, retries, retryBackoff}
if len(args) > 0 {
fmt.Println(shell.Source(args[0], force, opts, pamCompatible))
fmt.Println(shell.Source(args[0], force, opts, pamCompatible, dotEnvCompatible))
} else {
godotenv.Load(envFile)
envkey := os.Getenv("ENVKEY")
fmt.Println(shell.Source(envkey, force, opts, pamCompatible))
fmt.Println(shell.Source(envkey, force, opts, pamCompatible, dotEnvCompatible))
}
},
}
Expand All @@ -101,6 +102,7 @@ func init() {
RootCmd.Flags().Uint8Var(&retries, "retries", 3, "number of times to retry requests on failure")
RootCmd.Flags().Float64Var(&retryBackoff, "retryBackoff", 1, "retry backoff factor: {retryBackoff} * (2 ^ {retries - 1})")
RootCmd.Flags().BoolVar(&pamCompatible, "pam-compatible", false, "change output format to be compatible with /etc/environment on Linux")
RootCmd.Flags().BoolVar(&dotEnvCompatible, "dot-env-compatible", false, "change output to .env format")

// differences between bash syntax and the /etc/environment format, as parsed by PAM
// (https://github.com/linux-pam/linux-pam/blob/master/modules/pam_env/pam_env.c#L194)
Expand Down
6 changes: 4 additions & 2 deletions shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/envkey/envkey-fetch/fetch"
)

func Source(envkey string, force bool, options fetch.FetchOptions, pamCompatible bool) string {
func Source(envkey string, force bool, options fetch.FetchOptions, pamCompatible bool, dotEnvCompatible bool) string {
if envkey == "" {
return "echo 'error: ENVKEY missing.'; false"
}
Expand All @@ -36,7 +36,7 @@ func Source(envkey string, force bool, options fetch.FetchOptions, pamCompatible
}

var res string
if pamCompatible {
if pamCompatible || dotEnvCompatible {
res = ""
} else {
res = "export"
Expand Down Expand Up @@ -78,6 +78,8 @@ func Source(envkey string, force bool, options fetch.FetchOptions, pamCompatible
}
// Do not quote keys, but quote values.
res = res + "export " + key + "='" + val + "'"
} else if dotEnvCompatible {
res = res + key + "='" + val + "'" + "\n"
} else {
// Quote both keys and values.
res = res + " '" + key + "'='" + val + "'"
Expand Down
10 changes: 7 additions & 3 deletions shell/shell_test/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ const INVALID_ENVKEY = "Emzt4BE7C23QtsC7gb1z-3NvfNiG1Boy6XH2oinvalid-env-staging

func TestSource(t *testing.T) {
// Test valid
validRes := shell.Source(VALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, false)
validRes := shell.Source(VALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, false, false)
assert.Equal(t, "export 'TEST'='it' 'TEST_2'='works!' 'TEST_INJECTION'=''\"'\"'$(uname)' 'TEST_SINGLE_QUOTES'='this'\"'\"' is ok' 'TEST_SPACES'='it does work!' 'TEST_STRANGE_CHARS'='with quotes ` '\"'\"' \\\" bäh'", validRes)

// Test --pam-compatible
validRes2 := shell.Source(VALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, true)
validRes2 := shell.Source(VALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, true, false)
assert.Equal(t, "export TEST='it'\nexport TEST_2='works!'\nexport TEST_INJECTION=''$(uname)'\nexport TEST_SINGLE_QUOTES='this' is ok'\nexport TEST_SPACES='it does work!'\nexport TEST_STRANGE_CHARS='with quotes ` ' \\\" bäh'", validRes2)

// Test --dot-env-compatible
validRes3 := shell.Source(VALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, false, true)
assert.Equal(t, "TEST='it'\nTEST_2='works!'\nTEST_INJECTION=''\"'\"'$(uname)'\nTEST_SINGLE_QUOTES='this'\"'\"' is ok'\nTEST_SPACES='it does work!'\nTEST_STRANGE_CHARS='with quotes ` '\"'\"' \\\" bäh'\n", validRes3)

// Test invalid
invalidRes := shell.Source(INVALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, false)
invalidRes := shell.Source(INVALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, false, false)
assert.Equal(t, "echo 'error: ENVKEY invalid'; false", invalidRes)
}

0 comments on commit 26425f6

Please sign in to comment.