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