Skip to content

Commit

Permalink
automatic retries with backoff for improved reliability
Browse files Browse the repository at this point in the history
  • Loading branch information
danenania committed Nov 9, 2018
1 parent 0ed640d commit 3b8a5da
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
22 changes: 12 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,18 @@ echo "$SOME_VAR"
### Flags

```text
--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)
--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
-h, --help help for envkey-source
-v, --version prints the version
--verbose print verbose output (default is false)
--timeout float timeout in seconds for http requests (default 2)
--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)
--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
-h, --help help for envkey-source
-v, --version prints the version
--verbose print verbose output (default is false)
--timeout float timeout in seconds for http requests (default 2)
--retries uint8 number of times to retry requests on failure (default 3)
--retryBackoff float retry backoff factor: {retryBackoff} * (2 ^ {retries - 1}) (default 1)
```

### Errors
Expand Down
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ var printVersion bool
var pamCompatible bool
var verboseOutput bool
var timeoutSeconds float64
var retries uint8
var retryBackoff float64

// RootCmd represents the base command when called without any subcommands
var RootCmd = &cobra.Command{
Expand Down Expand Up @@ -67,7 +69,7 @@ You can also pass an ENVKEY directly (not recommended for real workflows):
}
}

opts := fetch.FetchOptions{cacheEnabled, cacheDir, "envkey-source", version.Version, verboseOutput, timeoutSeconds}
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))
} else {
Expand Down Expand Up @@ -96,7 +98,8 @@ func init() {
RootCmd.Flags().StringVar(&envFile, "env-file", ".env", "ENVKEY-containing env file name")
RootCmd.Flags().BoolVar(&verboseOutput, "verbose", false, "print verbose output (default is false)")
RootCmd.Flags().Float64Var(&timeoutSeconds, "timeout", 10.0, "timeout in seconds for http requests")

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")

// differences between bash syntax and the /etc/environment format, as parsed by PAM
Expand Down
6 changes: 3 additions & 3 deletions shell/shell_test/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ 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}, false)
validRes := shell.Source(VALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, 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}, true)
validRes2 := shell.Source(VALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, true)
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 invalid
invalidRes := shell.Source(INVALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0}, false)
invalidRes := shell.Source(INVALID_ENVKEY, true, fetch.FetchOptions{false, "", "envkey-source", version.Version, false, 2.0, 1, 0.1}, false)
assert.Equal(t, "echo 'error: ENVKEY invalid'; false", invalidRes)
}
2 changes: 1 addition & 1 deletion version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.2.2
1.2.4

0 comments on commit 3b8a5da

Please sign in to comment.