github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/helper/flags/autopilot_flags.go (about)

     1  package flags
     2  
     3  // These flag type implementations are provided to maintain autopilot command
     4  // backwards compatibility.
     5  
     6  import (
     7  	"fmt"
     8  	"math/bits"
     9  	"strconv"
    10  	"time"
    11  )
    12  
    13  // BoolValue provides a flag value that's aware if it has been set.
    14  type BoolValue struct {
    15  	v *bool
    16  }
    17  
    18  // Merge will overlay this value if it has been set.
    19  func (b *BoolValue) Merge(onto *bool) {
    20  	if b.v != nil {
    21  		*onto = *(b.v)
    22  	}
    23  }
    24  
    25  // Set implements the flag.Value interface.
    26  func (b *BoolValue) Set(v string) error {
    27  	if b.v == nil {
    28  		b.v = new(bool)
    29  	}
    30  	var err error
    31  	*(b.v), err = strconv.ParseBool(v)
    32  	return err
    33  }
    34  
    35  // String implements the flag.Value interface.
    36  func (b *BoolValue) String() string {
    37  	var current bool
    38  	if b.v != nil {
    39  		current = *(b.v)
    40  	}
    41  	return fmt.Sprintf("%v", current)
    42  }
    43  
    44  // DurationValue provides a flag value that's aware if it has been set.
    45  type DurationValue struct {
    46  	v *time.Duration
    47  }
    48  
    49  // Merge will overlay this value if it has been set.
    50  func (d *DurationValue) Merge(onto *time.Duration) {
    51  	if d.v != nil {
    52  		*onto = *(d.v)
    53  	}
    54  }
    55  
    56  // Set implements the flag.Value interface.
    57  func (d *DurationValue) Set(v string) error {
    58  	if d.v == nil {
    59  		d.v = new(time.Duration)
    60  	}
    61  	var err error
    62  	*(d.v), err = time.ParseDuration(v)
    63  	return err
    64  }
    65  
    66  // String implements the flag.Value interface.
    67  func (d *DurationValue) String() string {
    68  	var current time.Duration
    69  	if d.v != nil {
    70  		current = *(d.v)
    71  	}
    72  	return current.String()
    73  }
    74  
    75  // UintValue provides a flag value that's aware if it has been set.
    76  type UintValue struct {
    77  	v *uint
    78  }
    79  
    80  // Merge will overlay this value if it has been set.
    81  func (u *UintValue) Merge(onto *uint) {
    82  	if u.v != nil {
    83  		*onto = *(u.v)
    84  	}
    85  }
    86  
    87  // Set implements the flag.Value interface.
    88  func (u *UintValue) Set(v string) error {
    89  	if u.v == nil {
    90  		u.v = new(uint)
    91  	}
    92  
    93  	parsed, err := strconv.ParseUint(v, 0, bits.UintSize)
    94  	*(u.v) = (uint)(parsed)
    95  	return err
    96  }
    97  
    98  // String implements the flag.Value interface.
    99  func (u *UintValue) String() string {
   100  	var current uint
   101  	if u.v != nil {
   102  		current = *(u.v)
   103  	}
   104  	return fmt.Sprintf("%v", current)
   105  }