(cmd interface{}, values ...interface{})
| 108 | } |
| 109 | |
| 110 | func setPositionalFlags(cmd interface{}, values ...interface{}) { |
| 111 | ptr := reflect.ValueOf(cmd) |
| 112 | if ptr.Kind() != reflect.Ptr { |
| 113 | Fail("need to pass a pointer to the command struct") |
| 114 | } |
| 115 | |
| 116 | val := ptr.Elem() |
| 117 | if val.Kind() != reflect.Struct { |
| 118 | Fail("need to pass a command struct") |
| 119 | } |
| 120 | |
| 121 | typ := val.Type() |
| 122 | for i := 0; i < typ.NumField(); i++ { |
| 123 | field := typ.Field(i) |
| 124 | |
| 125 | if tagValue, ok := field.Tag.Lookup("positional-args"); ok && tagValue == "yes" && field.Type.Kind() == reflect.Struct { |
| 126 | if len(values) != field.Type.NumField() { |
| 127 | Fail(fmt.Sprintf("%d values provided but positional args struct %s has %d fields", len(values), field.Name, field.Type.NumField())) |
| 128 | } |
| 129 | |
| 130 | for j := 0; j < field.Type.NumField(); j++ { |
| 131 | posField := field.Type.Field(j) |
| 132 | value := reflect.ValueOf(values[j]) |
| 133 | if value.Type().ConvertibleTo(posField.Type) { |
| 134 | val.Field(i).Field(j).Set(value.Convert(posField.Type)) |
| 135 | } else { |
| 136 | Fail(fmt.Sprintf( |
| 137 | "Could not set field '%s' type '%s' to '%v' type '%s'", |
| 138 | posField.Name, |
| 139 | posField.Type, |
| 140 | value.Interface(), |
| 141 | value.Type(), |
| 142 | )) |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | return |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | Fail(`Did not find a field with 'positional-args:"yes"' in the struct`) |
| 151 | } |
no test coverage detected