github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/v7_suite_test.go (about) 1 package v7_test 2 3 import ( 4 "fmt" 5 "reflect" 6 "strings" 7 8 uuid "github.com/nu7hatch/gouuid" 9 . "github.com/onsi/ginkgo" 10 . "github.com/onsi/gomega" 11 log "github.com/sirupsen/logrus" 12 13 "testing" 14 ) 15 16 func TestV3(t *testing.T) { 17 RegisterFailHandler(Fail) 18 RunSpecs(t, "V7 Command Suite") 19 } 20 21 var _ = BeforeEach(func() { 22 log.SetLevel(log.PanicLevel) 23 }) 24 25 // RandomString provides a random string 26 func RandomString(prefix string) string { 27 guid, err := uuid.NewV4() 28 if err != nil { 29 panic(err) 30 } 31 32 return prefix + "-" + guid.String() 33 } 34 35 func setFlag(cmd interface{}, flag string, values ...interface{}) { 36 var value reflect.Value 37 switch len(values) { 38 case 0: 39 value = reflect.ValueOf(true) 40 case 1: 41 value = reflect.ValueOf(values[0]) 42 default: 43 Fail(fmt.Sprintf("cannot take more than one value for flag '%s'", flag)) 44 } 45 46 var key, trimmedFlag string 47 switch { 48 case strings.HasPrefix(flag, "--"): 49 trimmedFlag = strings.TrimPrefix(flag, "--") 50 key = "long" 51 case strings.HasPrefix(flag, "-"): 52 trimmedFlag = strings.TrimPrefix(flag, "-") 53 key = "short" 54 default: 55 Fail("flag must start with prefix '--' or '-'") 56 } 57 58 ptr := reflect.ValueOf(cmd) 59 if ptr.Kind() != reflect.Ptr { 60 Fail("need to pass a pointer to the command struct") 61 } 62 63 val := ptr.Elem() 64 if val.Kind() != reflect.Struct { 65 Fail("need to pass a command struct") 66 } 67 68 typ := val.Type() 69 for i := 0; i < typ.NumField(); i++ { 70 field := typ.Field(i) 71 72 if tagValue, ok := field.Tag.Lookup(key); ok { 73 if tagValue == trimmedFlag { 74 switch { 75 case value.Type().ConvertibleTo(field.Type): 76 val.Field(i).Set(value.Convert(field.Type)) 77 return 78 case reflect.PtrTo(field.Type).Implements(reflect.TypeOf((*interface{ UnmarshalFlag(string) error })(nil)).Elem()): 79 elem := reflect.New(field.Type) 80 e := elem.MethodByName("UnmarshalFlag").Call([]reflect.Value{value}) 81 if !e[0].IsNil() { 82 Fail(fmt.Sprintf( 83 "Could not set field '%s' type '%s' to '%v' type '%s' for flag '%s' because %s", 84 field.Name, 85 field.Type, 86 value.Interface(), 87 value.Type(), 88 flag, 89 e[0].Interface(), 90 )) 91 } 92 val.Field(i).Set(elem.Elem()) 93 return 94 default: 95 Fail(fmt.Sprintf( 96 "Could not set field '%s' type '%s' to '%v' type '%s' for flag '%s'", 97 field.Name, 98 field.Type, 99 value.Interface(), 100 value.Type(), 101 flag, 102 )) 103 } 104 } 105 } 106 } 107 108 Fail(fmt.Sprintf("could not find flag '%s' in command struct", flag)) 109 } 110 111 func setPositionalFlags(cmd interface{}, values ...interface{}) { 112 ptr := reflect.ValueOf(cmd) 113 if ptr.Kind() != reflect.Ptr { 114 Fail("need to pass a pointer to the command struct") 115 } 116 117 val := ptr.Elem() 118 if val.Kind() != reflect.Struct { 119 Fail("need to pass a command struct") 120 } 121 122 typ := val.Type() 123 for i := 0; i < typ.NumField(); i++ { 124 field := typ.Field(i) 125 126 if tagValue, ok := field.Tag.Lookup("positional-args"); ok && tagValue == "yes" && field.Type.Kind() == reflect.Struct { 127 if len(values) != field.Type.NumField() { 128 Fail(fmt.Sprintf("%d values provided but positional args struct %s has %d fields", len(values), field.Name, field.Type.NumField())) 129 } 130 131 for j := 0; j < field.Type.NumField(); j++ { 132 posField := field.Type.Field(j) 133 value := reflect.ValueOf(values[j]) 134 if value.Type().ConvertibleTo(posField.Type) { 135 val.Field(i).Field(j).Set(value.Convert(posField.Type)) 136 } else { 137 Fail(fmt.Sprintf( 138 "Could not set field '%s' type '%s' to '%v' type '%s'", 139 posField.Name, 140 posField.Type, 141 value.Interface(), 142 value.Type(), 143 )) 144 } 145 } 146 147 return 148 } 149 } 150 151 Fail(`Did not find a field with 'positional-args:"yes"' in the struct`) 152 }