...

Source file src/github.com/linkerd/linkerd2/cli/flag/flag.go

Documentation: github.com/linkerd/linkerd2/cli/flag

     1  package flag
     2  
     3  import (
     4  	"time"
     5  
     6  	charts "github.com/linkerd/linkerd2/pkg/charts/linkerd2"
     7  	"github.com/spf13/pflag"
     8  )
     9  
    10  type (
    11  	// Flag is an interface which describes a command line flag that affects the
    12  	// Helm Values used to render Helm charts.  This interface allows us to
    13  	// iterate over flags which have been set and apply their effects to the
    14  	// Values.
    15  	Flag interface {
    16  		Apply(values *charts.Values) error
    17  		IsSet() bool
    18  		Name() string
    19  	}
    20  
    21  	// UintFlag is a Flag with a uint typed value.
    22  	UintFlag struct {
    23  		name    string
    24  		Value   uint
    25  		flagSet *pflag.FlagSet
    26  		apply   func(values *charts.Values, value uint) error
    27  	}
    28  
    29  	// Int64Flag is a Flag with an int64 typed value.
    30  	Int64Flag struct {
    31  		name    string
    32  		Value   int64
    33  		flagSet *pflag.FlagSet
    34  		apply   func(values *charts.Values, value int64) error
    35  	}
    36  
    37  	// StringFlag is a Flag with a string typed value.
    38  	StringFlag struct {
    39  		name    string
    40  		Value   string
    41  		flagSet *pflag.FlagSet
    42  		apply   func(values *charts.Values, value string) error
    43  	}
    44  
    45  	// StringSliceFlag is a Flag with a []string typed value.
    46  	StringSliceFlag struct {
    47  		name    string
    48  		Value   []string
    49  		flagSet *pflag.FlagSet
    50  		apply   func(values *charts.Values, value []string) error
    51  	}
    52  
    53  	// BoolFlag is a Flag with a bool typed value.
    54  	BoolFlag struct {
    55  		name    string
    56  		Value   bool
    57  		flagSet *pflag.FlagSet
    58  		apply   func(values *charts.Values, value bool) error
    59  	}
    60  
    61  	// DurationFlag is a Flag with a time.Duration typed value.
    62  	DurationFlag struct {
    63  		name    string
    64  		Value   time.Duration
    65  		flagSet *pflag.FlagSet
    66  		apply   func(values *charts.Values, value time.Duration) error
    67  	}
    68  )
    69  
    70  // NewUintFlag creates a new uint typed Flag that executes the given function
    71  // when applied.  The flag is attached to the given FlagSet.
    72  func NewUintFlag(flagSet *pflag.FlagSet, name string, defaultValue uint, description string, apply func(values *charts.Values, value uint) error) *UintFlag {
    73  	flag := UintFlag{
    74  		name:    name,
    75  		flagSet: flagSet,
    76  		apply:   apply,
    77  	}
    78  	flagSet.UintVar(&flag.Value, name, defaultValue, description)
    79  	return &flag
    80  }
    81  
    82  // NewInt64Flag creates a new int64 typed Flag that executes the given function
    83  // when applied.  The flag is attached to the given FlagSet.
    84  func NewInt64Flag(flagSet *pflag.FlagSet, name string, defaultValue int64, description string, apply func(values *charts.Values, value int64) error) *Int64Flag {
    85  	flag := Int64Flag{
    86  		name:    name,
    87  		flagSet: flagSet,
    88  		apply:   apply,
    89  	}
    90  	flagSet.Int64Var(&flag.Value, name, defaultValue, description)
    91  	return &flag
    92  }
    93  
    94  // NewStringFlag creates a new string typed Flag that executes the given function
    95  // when applied.  The flag is attached to the given FlagSet.
    96  func NewStringFlag(flagSet *pflag.FlagSet, name string, defaultValue string, description string, apply func(values *charts.Values, value string) error) *StringFlag {
    97  	flag := StringFlag{
    98  		name:    name,
    99  		flagSet: flagSet,
   100  		apply:   apply,
   101  	}
   102  	flagSet.StringVar(&flag.Value, name, defaultValue, description)
   103  	return &flag
   104  }
   105  
   106  // NewStringSliceFlag creates a new []string typed Flag that executes the given function
   107  // when applied.  The flag is attached to the given FlagSet.
   108  func NewStringSliceFlag(flagSet *pflag.FlagSet, name string, defaultValue []string, description string, apply func(values *charts.Values, value []string) error) *StringSliceFlag {
   109  	flag := StringSliceFlag{
   110  		name:    name,
   111  		flagSet: flagSet,
   112  		apply:   apply,
   113  	}
   114  	flagSet.StringSliceVar(&flag.Value, name, defaultValue, description)
   115  	return &flag
   116  }
   117  
   118  // NewStringFlagP creates a new string typed Flag that executes the given function
   119  // when applied.  The flag is attached to the given FlagSet.
   120  func NewStringFlagP(flagSet *pflag.FlagSet, name string, short string, defaultValue string, description string, apply func(values *charts.Values, value string) error) *StringFlag {
   121  	flag := StringFlag{
   122  		name:    name,
   123  		flagSet: flagSet,
   124  		apply:   apply,
   125  	}
   126  	flagSet.StringVarP(&flag.Value, name, short, defaultValue, description)
   127  	return &flag
   128  }
   129  
   130  // NewBoolFlag creates a new bool typed Flag that executes the given function
   131  // when applied.  The flag is attached to the given FlagSet.
   132  func NewBoolFlag(flagSet *pflag.FlagSet, name string, defaultValue bool, description string, apply func(values *charts.Values, value bool) error) *BoolFlag {
   133  	flag := BoolFlag{
   134  		name:    name,
   135  		flagSet: flagSet,
   136  		apply:   apply,
   137  	}
   138  	flagSet.BoolVar(&flag.Value, name, defaultValue, description)
   139  	return &flag
   140  }
   141  
   142  // NewDurationFlag creates a new time.Duration typed Flag that executes the given function
   143  // when applied.  The flag is attached to the given FlagSet.
   144  func NewDurationFlag(flagSet *pflag.FlagSet, name string, defaultValue time.Duration, description string, apply func(values *charts.Values, value time.Duration) error) *DurationFlag {
   145  	flag := DurationFlag{
   146  		name:    name,
   147  		flagSet: flagSet,
   148  		apply:   apply,
   149  	}
   150  	flagSet.DurationVar(&flag.Value, name, defaultValue, description)
   151  	return &flag
   152  }
   153  
   154  // Apply executes the stored apply function on the given Values.
   155  func (flag *UintFlag) Apply(values *charts.Values) error {
   156  	return flag.apply(values, flag.Value)
   157  }
   158  
   159  // IsSet returns true if and only if the Flag has been explicitly set with a value.
   160  func (flag *UintFlag) IsSet() bool {
   161  	return flag.flagSet.Changed(flag.name)
   162  }
   163  
   164  // Name returns the name of the flag.
   165  func (flag *UintFlag) Name() string {
   166  	return flag.name
   167  }
   168  
   169  // Apply executes the stored apply function on the given Values.
   170  func (flag *Int64Flag) Apply(values *charts.Values) error {
   171  	return flag.apply(values, flag.Value)
   172  }
   173  
   174  // IsSet returns true if and only if the Flag has been explicitly set with a value.
   175  func (flag *Int64Flag) IsSet() bool {
   176  	return flag.flagSet.Changed(flag.name)
   177  }
   178  
   179  // Name returns the name of the flag.
   180  func (flag *Int64Flag) Name() string {
   181  	return flag.name
   182  }
   183  
   184  // Apply executes the stored apply function on the given Values.
   185  func (flag *StringFlag) Apply(values *charts.Values) error {
   186  	return flag.apply(values, flag.Value)
   187  }
   188  
   189  // Set sets the given value to the underlying Flag
   190  func (flag *StringFlag) Set(value string) error {
   191  	return flag.flagSet.Set(flag.name, value)
   192  }
   193  
   194  // IsSet returns true if and only if the Flag has been explicitly set with a value.
   195  func (flag *StringFlag) IsSet() bool {
   196  	return flag.flagSet.Changed(flag.name)
   197  }
   198  
   199  // Name returns the name of the flag.
   200  func (flag *StringFlag) Name() string {
   201  	return flag.name
   202  }
   203  
   204  // Apply executes the stored apply function on the given Values.
   205  func (flag *StringSliceFlag) Apply(values *charts.Values) error {
   206  	return flag.apply(values, flag.Value)
   207  }
   208  
   209  // IsSet returns true if and only if the Flag has been explicitly set with a value.
   210  func (flag *StringSliceFlag) IsSet() bool {
   211  	return flag.flagSet.Changed(flag.name)
   212  }
   213  
   214  // Name returns the name of the flag.
   215  func (flag *StringSliceFlag) Name() string {
   216  	return flag.name
   217  }
   218  
   219  // Apply executes the stored apply function on the given Values.
   220  func (flag *BoolFlag) Apply(values *charts.Values) error {
   221  	return flag.apply(values, flag.Value)
   222  }
   223  
   224  // IsSet returns true if and only if the Flag has been explicitly set with a value.
   225  func (flag *BoolFlag) IsSet() bool {
   226  	return flag.flagSet.Changed(flag.name)
   227  }
   228  
   229  // Name returns the name of the flag.
   230  func (flag *BoolFlag) Name() string {
   231  	return flag.name
   232  }
   233  
   234  // Apply executes the stored apply function on the given Values.
   235  func (flag *DurationFlag) Apply(values *charts.Values) error {
   236  	return flag.apply(values, flag.Value)
   237  }
   238  
   239  // IsSet returns true if and only if the Flag has been explicitly set with a value.
   240  func (flag *DurationFlag) IsSet() bool {
   241  	return flag.flagSet.Changed(flag.name)
   242  }
   243  
   244  // Name returns the name of the flag.
   245  func (flag *DurationFlag) Name() string {
   246  	return flag.name
   247  }
   248  
   249  // ApplySetFlags iterates through the given slice of Flags and applies the
   250  // effect of each set flag to the given Values.  Flags effects are applied
   251  // in the order they appear in the slice.
   252  func ApplySetFlags(values *charts.Values, flags []Flag) error {
   253  	for _, flag := range flags {
   254  		if flag.IsSet() {
   255  			err := flag.Apply(values)
   256  			if err != nil {
   257  				return err
   258  			}
   259  		}
   260  	}
   261  	return nil
   262  }
   263  

View as plain text