From 9b254b2f53ae1c7fb2c18e542542e2ad0b0f0b85 Mon Sep 17 00:00:00 2001 From: Vladislav Rassokhin Date: Fri, 12 Jul 2019 11:29:41 +0300 Subject: [PATCH] Simplify time.Duration parsing --- common/step_shutdown.go | 17 ++++------------- common/step_wait_for_ip.go | 22 +++++++--------------- iso/step_boot_command.go | 22 +++++++--------------- 3 files changed, 18 insertions(+), 43 deletions(-) diff --git a/common/step_shutdown.go b/common/step_shutdown.go index 86fee82c2..94a507900 100644 --- a/common/step_shutdown.go +++ b/common/step_shutdown.go @@ -12,27 +12,18 @@ import ( ) type ShutdownConfig struct { - Command string `mapstructure:"shutdown_command"` - RawTimeout string `mapstructure:"shutdown_timeout"` - - Timeout time.Duration + Command string `mapstructure:"shutdown_command"` + Timeout time.Duration `mapstructure:"shutdown_timeout"` } func (c *ShutdownConfig) Prepare() []error { var errs []error - if c.RawTimeout != "" { - timeout, err := time.ParseDuration(c.RawTimeout) - if err != nil { - errs = append(errs, fmt.Errorf("Failed parsing shutdown_timeout: %s", err)) - return errs - } - c.Timeout = timeout - } else { + if c.Timeout == 0 { c.Timeout = 5 * time.Minute } - return nil + return errs } type StepShutdown struct { diff --git a/common/step_wait_for_ip.go b/common/step_wait_for_ip.go index e81460df5..64a841538 100644 --- a/common/step_wait_for_ip.go +++ b/common/step_wait_for_ip.go @@ -11,9 +11,7 @@ import ( ) type WaitIpConfig struct { - SettleTimeout string `mapstructure:"ip_settle_timeout"` - - settleTimeout time.Duration + SettleTimeout time.Duration `mapstructure:"ip_settle_timeout"` } type StepWaitForIp struct { @@ -23,14 +21,8 @@ type StepWaitForIp struct { func (c *WaitIpConfig) Prepare() []error { var errs []error - if c.SettleTimeout == "" { - c.SettleTimeout = "5s" - } - - var err error - c.settleTimeout, err = time.ParseDuration(c.SettleTimeout) - if err != nil { - errs = append(errs, fmt.Errorf("failed parsing ip_settle_timeout: %s", err)) + if c.SettleTimeout == 0 { + c.SettleTimeout = 5 * time.Second } return errs @@ -71,11 +63,11 @@ func doGetIp(vm *driver.VirtualMachine, ctx context.Context, c *WaitIpConfig, er var prevIp = "" var stopTime time.Time var interval time.Duration - if c.settleTimeout.Seconds() >= 120 { + if c.SettleTimeout.Seconds() >= 120 { interval = 30 * time.Second - } else if c.settleTimeout.Seconds() >= 60 { + } else if c.SettleTimeout.Seconds() >= 60 { interval = 15 * time.Second - } else if c.settleTimeout.Seconds() >= 10 { + } else if c.SettleTimeout.Seconds() >= 10 { interval = 5 * time.Second } else { interval = 1 * time.Second @@ -93,7 +85,7 @@ loop: log.Printf("VM IP changed from %s to %s", prevIp, ip) } prevIp = ip - stopTime = time.Now().Add(c.settleTimeout) + stopTime = time.Now().Add(c.SettleTimeout) goto loop } else { log.Printf("VM IP is still the same: %s", prevIp) diff --git a/iso/step_boot_command.go b/iso/step_boot_command.go index 17a90192c..462806247 100644 --- a/iso/step_boot_command.go +++ b/iso/step_boot_command.go @@ -18,11 +18,9 @@ import ( ) type BootConfig struct { - BootCommand []string `mapstructure:"boot_command"` - RawBootWait string `mapstructure:"boot_wait"` // example: "1m30s"; default: "10s" - HTTPIP string `mapstructure:"http_ip"` - - bootWait time.Duration + BootCommand []string `mapstructure:"boot_command"` + BootWait time.Duration `mapstructure:"boot_wait"` // example: "1m30s"; default: "10s" + HTTPIP string `mapstructure:"http_ip"` } type bootCommandTemplateData struct { @@ -34,14 +32,8 @@ type bootCommandTemplateData struct { func (c *BootConfig) Prepare() []error { var errs []error - if c.RawBootWait == "" { - c.RawBootWait = "10s" - } - - var err error - c.bootWait, err = time.ParseDuration(c.RawBootWait) - if err != nil { - errs = append(errs, fmt.Errorf("failed parsing boot_wait: %s", err)) + if c.BootWait == 0 { + c.BootWait = 10 * time.Second } return errs @@ -98,8 +90,8 @@ func (s *StepBootCommand) Run(_ context.Context, state multistep.StateBag) multi return multistep.ActionContinue } - ui.Say(fmt.Sprintf("Waiting %s for boot...", s.Config.bootWait)) - wait := time.After(s.Config.bootWait) + ui.Say(fmt.Sprintf("Waiting %s for boot...", s.Config.BootWait)) + wait := time.After(s.Config.BootWait) WAITLOOP: for { select {