github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/cmd/juju/action/waitflag.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package action
     5  
     6  import (
     7  	"time"
     8  )
     9  
    10  // A gnuflag.Value for the --wait command line argument. If called
    11  // as a boolean  with no arguments, the forever flag is set to true.
    12  // If called  with an argument, d is set to the result of
    13  // time.ParseDuration().
    14  // eg:
    15  //   --wait
    16  //   --wait=10s
    17  type waitFlag struct {
    18  	forever bool
    19  	d       time.Duration
    20  }
    21  
    22  func (f *waitFlag) Set(s string) error {
    23  	if s == "true" {
    24  		f.forever = true
    25  		return nil
    26  	}
    27  	v, err := time.ParseDuration(s)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	f.d = v
    32  	return nil
    33  }
    34  
    35  func (f *waitFlag) String() string {
    36  	if f.forever {
    37  		return "true"
    38  	}
    39  	if f.d == 0 {
    40  		return ""
    41  	}
    42  	return f.d.String()
    43  }
    44  
    45  func (f *waitFlag) IsBoolFlag() bool {
    46  	return true
    47  }