Skip to content

Commit

Permalink
add flag for .env format
Browse files Browse the repository at this point in the history
  • Loading branch information
Seth Pollack committed Nov 13, 2018
1 parent 5532103 commit c54db78
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 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

0 comments on commit c54db78

Please sign in to comment.