github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/arguments/flags.go (about)

     1  // Copyright (c) HashiCorp, Inc.
     2  // SPDX-License-Identifier: MPL-2.0
     3  
     4  package arguments
     5  
     6  import (
     7  	"flag"
     8  	"fmt"
     9  )
    10  
    11  // flagStringSlice is a flag.Value implementation which allows collecting
    12  // multiple instances of a single flag into a slice. This is used for flags
    13  // such as -target=aws_instance.foo and -var x=y.
    14  type flagStringSlice []string
    15  
    16  var _ flag.Value = (*flagStringSlice)(nil)
    17  
    18  func (v *flagStringSlice) String() string {
    19  	return ""
    20  }
    21  func (v *flagStringSlice) Set(raw string) error {
    22  	*v = append(*v, raw)
    23  
    24  	return nil
    25  }
    26  
    27  // flagNameValueSlice is a flag.Value implementation that appends raw flag
    28  // names and values to a slice. This is used to collect a sequence of flags
    29  // with possibly different names, preserving the overall order.
    30  //
    31  // FIXME: this is a copy of rawFlags from command/meta_config.go, with the
    32  // eventual aim of replacing it altogether by gathering variables in the
    33  // arguments package.
    34  type flagNameValueSlice struct {
    35  	flagName string
    36  	items    *[]FlagNameValue
    37  }
    38  
    39  var _ flag.Value = flagNameValueSlice{}
    40  
    41  func newFlagNameValueSlice(flagName string) flagNameValueSlice {
    42  	var items []FlagNameValue
    43  	return flagNameValueSlice{
    44  		flagName: flagName,
    45  		items:    &items,
    46  	}
    47  }
    48  
    49  func (f flagNameValueSlice) Empty() bool {
    50  	if f.items == nil {
    51  		return true
    52  	}
    53  	return len(*f.items) == 0
    54  }
    55  
    56  func (f flagNameValueSlice) AllItems() []FlagNameValue {
    57  	if f.items == nil {
    58  		return nil
    59  	}
    60  	return *f.items
    61  }
    62  
    63  func (f flagNameValueSlice) Alias(flagName string) flagNameValueSlice {
    64  	return flagNameValueSlice{
    65  		flagName: flagName,
    66  		items:    f.items,
    67  	}
    68  }
    69  
    70  func (f flagNameValueSlice) String() string {
    71  	return ""
    72  }
    73  
    74  func (f flagNameValueSlice) Set(str string) error {
    75  	*f.items = append(*f.items, FlagNameValue{
    76  		Name:  f.flagName,
    77  		Value: str,
    78  	})
    79  	return nil
    80  }
    81  
    82  type FlagNameValue struct {
    83  	Name  string
    84  	Value string
    85  }
    86  
    87  func (f FlagNameValue) String() string {
    88  	return fmt.Sprintf("%s=%q", f.Name, f.Value)
    89  }
    90  
    91  // FlagIsSet returns whether a flag is explicitly set in a set of flags
    92  func FlagIsSet(flags *flag.FlagSet, name string) bool {
    93  	isSet := false
    94  	flags.Visit(func(f *flag.Flag) {
    95  		if f.Name == name {
    96  			isSet = true
    97  		}
    98  	})
    99  	return isSet
   100  }