Skip to content

Commit

Permalink
Add -timeout cli flag parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
x-cray authored and sethvargo committed Apr 29, 2016
1 parent 9af87ed commit e5328d7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 48 deletions.
100 changes: 54 additions & 46 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,12 @@ func (cli *CLI) parseFlags(args []string) (*Config, []string, bool, bool, error)
return nil
}), "splay", "")

flags.Var((funcDurationVar)(func(d time.Duration) error {
config.Timeout = d
config.set("timeout")
return nil
}), "timeout", "")

flags.Var((funcBoolVar)(func(b bool) error {
config.Upcase = b
config.set("upcase")
Expand Down Expand Up @@ -384,50 +390,52 @@ Usage: %s [options] <command>
Options:
-auth=<user[:pass]> Set the basic authentication username (and password)
-consul=<address> Sets the address of the Consul instance
-max-stale=<duration> Set the maximum staleness and allow stale queries to
Consul which will distribute work among all servers
instead of just the leader
-ssl Use SSL when connecting to Consul
-ssl-verify Verify certificates when connecting via SSL
-token=<token> Sets the Consul API token
-syslog Send the output to syslog instead of standard error
and standard out. The syslog facility defaults to
LOCAL0 and can be changed using a configuration file
-syslog-facility=<f> Set the facility where syslog should log. If this
attribute is supplied, the -syslog flag must also be
supplied.
-wait=<duration> Sets the 'minumum(:maximum)' amount of time to wait
before writing a triggering a restart
-retry=<duration> The amount of time to wait if Consul returns an
error when communicating with the API
-prefix A prefix to watch, multiple prefixes are merged from
left to right, with the right-most result taking
precedence, including any values specified with
-secret
-secret A secret path to watch in Vault, multiple prefixes
are merged from left to right, with the right-most
result taking precedence, including any values
specified with -prefix
-sanitize Replace invalid characters in keys to underscores
-splay The maximum time to wait before restarting the
program, from which a random value is chosen
-upcase Convert all environment variable keys to uppercase
-kill-signal The signal to send to kill the process
-config=<path> Sets the path to a configuration file on disk
-log-level=<level> Set the logging level - valid values are "debug",
"info", "warn" (default), and "err"
-pristine Only use variables retrieved from consul, do not inherit
existing environment variables
-once Do not run the process as a daemon
-version Print the version of this daemon
-auth=<user[:pass]> Set the basic authentication username (and password)
-consul=<address> Sets the address of the Consul instance
-max-stale=<duration> Set the maximum staleness and allow stale queries to
Consul which will distribute work among all servers
instead of just the leader
-retry=<duration> The amount of time to wait if Consul returns an
error when communicating with the API
-ssl Use SSL when connecting to Consul
-ssl-verify Verify certificates when connecting via SSL
-token=<token> Sets the Consul API token
-log-level=<level> Set the logging level - valid values are "debug",
"info", "warn" (default), and "err"
-syslog Send the output to syslog instead of standard error
and standard out. The syslog facility defaults to
LOCAL0 and can be changed using a configuration file
-syslog-facility=<f> Set the facility where syslog should log. If this
attribute is supplied, the -syslog flag must also be
supplied.
-wait=<duration[:duration]> Sets the 'minumum[:maximum]' amount of time to wait
before writing a triggering a restart
-splay=<duration> The maximum time to wait before sending kill signal
to the program, from which a random value is chosen
-timeout=<duration> Time to wait after sending kill signal to the
program before forcibly killing it
-prefix=<prefix> A prefix to watch, multiple prefixes are merged from
left to right, with the right-most result taking
precedence, including any values specified with
-secret
-secret=<prefix> A secret path to watch in Vault, multiple prefixes
are merged from left to right, with the right-most
result taking precedence, including any values
specified with -prefix
-sanitize Replace invalid characters in keys to underscores
-upcase Convert all environment variable keys to uppercase
-pristine Only use variables retrieved from Consul, do not
inherit existing environment variables
-kill-signal=<signal> The signal to send to kill the process. Defaults to
SIGTERM
-config=<path> Sets the path to a configuration file on disk
-once Do not run the process as a daemon
-version Print the version of this daemon
`
18 changes: 18 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,24 @@ func TestParseFlags_splay(t *testing.T) {
}
}

func TestParseFlags_timeout(t *testing.T) {
cli := NewCLI(ioutil.Discard, ioutil.Discard)
config, _, _, _, err := cli.parseFlags([]string{
"-timeout", "10s",
})
if err != nil {
t.Fatal(err)
}

expected := 10 * time.Second
if config.Timeout != expected {
t.Errorf("expected %v to be %v", config.Timeout, expected)
}
if !config.WasSet("timeout") {
t.Errorf("expected timeout to be set")
}
}

func TestParseFlags_upcase(t *testing.T) {
cli := NewCLI(ioutil.Discard, ioutil.Discard)
config, _, _, _, err := cli.parseFlags([]string{
Expand Down
2 changes: 1 addition & 1 deletion config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ func TestConfigFromPath_BadConfigs(t *testing.T) {

configPath := filepath.Join(configDir, "config")
err = ioutil.WriteFile(configPath, []byte(`
totally not a valid config
totally not a valid config!
`), 0644)
if err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (r *Runner) Signal(sig os.Signal) error {
}

// Run executes and manages the child process with the correct environment. The
// current enviornment is also copied into the child process environment.
// current environment is also copied into the child process environment.
func (r *Runner) Run() (<-chan int, error) {
r.Lock()
defer r.Unlock()
Expand Down

0 comments on commit e5328d7

Please sign in to comment.