aboutsummaryrefslogtreecommitdiff
path: root/cli/annotated_config.go
blob: 8b89b3bed894b3cf5d1546c036acb0108d66e64d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package cli

import (
	"fmt"
	"log"
	"reflect"
	"strings"
	"time"

	"code.crute.us/mcrute/golib/vault"
	"github.com/spf13/cobra"
)

func MustGetConfig(cmd *cobra.Command, out interface{}) {
	if err := GetConfig(cmd, out); err != nil {
		log.Fatal(err)
	}
}

func GetConfig(cmd *cobra.Command, out interface{}) error {
	t := reflect.TypeOf(out).Elem()
	o := reflect.ValueOf(out).Elem()

	for i := 0; i < t.NumField(); i++ {
		tf := t.Field(i)
		f := o.FieldByName(tf.Name)

		// Fields with no name are not considered flags
		name := tf.Tag.Get("flag")
		if name == "" {
			continue
		}

		// Pretty much only string and struct can be tested
		req := tf.Tag.Get("flag-required") == "true"

		switch f.Type().Kind() {
		case reflect.Bool:
			v, _ := cmd.Flags().GetBool(name)
			f.Set(reflect.ValueOf(v))
		case reflect.String:
			v, _ := cmd.Flags().GetString(name)
			if req && v == "" {
				log.Fatalf("Flag %s is required but not provided", name)
			}
			f.Set(reflect.ValueOf(v))
		case reflect.Int:
			v, _ := cmd.Flags().GetInt(name)
			f.Set(reflect.ValueOf(v))
		case reflect.Int32:
			v, _ := cmd.Flags().GetInt32(name)
			f.Set(reflect.ValueOf(v))
		case reflect.Int64:
			if tf.Type.AssignableTo(reflect.TypeOf(time.Duration(0))) { // time.Duration
				v, _ := cmd.Flags().GetDuration(name)
				f.Set(reflect.ValueOf(v))
			} else {
				v, _ := cmd.Flags().GetInt64(name)
				f.Set(reflect.ValueOf(v))
			}
		case reflect.Uint:
			v, _ := cmd.Flags().GetUint(name)
			f.Set(reflect.ValueOf(v))
		case reflect.Uint32:
			v, _ := cmd.Flags().GetUint32(name)
			f.Set(reflect.ValueOf(v))
		case reflect.Uint64:
			v, _ := cmd.Flags().GetUint64(name)
			f.Set(reflect.ValueOf(v))
		case reflect.Float32:
			v, _ := cmd.Flags().GetFloat32(name)
			f.Set(reflect.ValueOf(v))
		case reflect.Float64:
			v, _ := cmd.Flags().GetFloat64(name)
			f.Set(reflect.ValueOf(v))
		case reflect.Slice:
			switch tf.Type.Elem().Kind() {
			case reflect.String:
				v, _ := cmd.Flags().GetStringSlice(name)
				f.Set(reflect.ValueOf(v))
			case reflect.Int:
				v, _ := cmd.Flags().GetIntSlice(name)
				f.Set(reflect.ValueOf(v))
			case reflect.Int32:
				v, _ := cmd.Flags().GetInt32Slice(name)
				f.Set(reflect.ValueOf(v))
			case reflect.Int64:
				v, _ := cmd.Flags().GetInt64Slice(name)
				f.Set(reflect.ValueOf(v))
			case reflect.Uint:
				v, _ := cmd.Flags().GetUintSlice(name)
				f.Set(reflect.ValueOf(v))
			case reflect.Float32:
				v, _ := cmd.Flags().GetFloat32Slice(name)
				f.Set(reflect.ValueOf(v))
			case reflect.Float64:
				v, _ := cmd.Flags().GetFloat64Slice(name)
				f.Set(reflect.ValueOf(v))
			default:
				return fmt.Errorf("type []%s is not supported for field %s", tf.Type.Elem(), tf.Name)
			}
		case reflect.Struct:
			if tf.Type.AssignableTo(reflect.TypeOf(VaultCredential{})) { // cli.VaultCredential
				v, _ := cmd.Flags().GetString(name)
				if req && v == "" {
					log.Fatalf("Flag %s is required but not provided", name)
				}
				vk, err := vault.GetVaultKey(v)
				if err != nil {
					return fmt.Errorf("Error getting %s from vault: %w", name, err)
				}
				f.Set(reflect.ValueOf(VaultCredential{v, vk.Username, vk.Password}))
			} else {
				return fmt.Errorf("type %s is not supported for field %s", tf.Type, tf.Name)
			}
		default:
			return fmt.Errorf("type %s is not supported for field %s", tf.Type, tf.Name)
		}
	}

	return nil
}

func inScope(desired, allowed string) bool {
	for _, i := range strings.Split(allowed, ",") {
		if strings.TrimSpace(i) == desired {
			return true
		}
	}
	return false
}

func AddFlags(cmd *cobra.Command, cfg interface{}, def interface{}, scope string) error {
	t := reflect.TypeOf(cfg).Elem()
	d := reflect.ValueOf(def).Elem()

	for i := 0; i < t.NumField(); i++ {
		f := t.Field(i)

		// Fields with no name are not considered flags
		name := f.Tag.Get("flag")
		if name == "" {
			continue
		}

		// Non-matching scopes should not bind here (note root is "")
		// Scopes can be a comma separated list
		if !inScope(scope, f.Tag.Get("flag-scope")) {
			continue
		}

		defV := d.FieldByName(f.Name).Interface()
		help := f.Tag.Get("flag-help")

		switch f.Type.Kind() {
		case reflect.Bool:
			cmd.PersistentFlags().Bool(name, defV.(bool), help)
		case reflect.String:
			cmd.PersistentFlags().String(name, defV.(string), help)
		case reflect.Int:
			cmd.PersistentFlags().Int(name, defV.(int), help)
		case reflect.Int32:
			cmd.PersistentFlags().Int32(name, defV.(int32), help)
		case reflect.Int64:
			if f.Type.AssignableTo(reflect.TypeOf(time.Duration(0))) { // time.Duration
				cmd.PersistentFlags().Duration(name, defV.(time.Duration), help)
			} else {
				cmd.PersistentFlags().Int64(name, defV.(int64), help)
			}
		case reflect.Uint:
			cmd.PersistentFlags().Uint(name, defV.(uint), help)
		case reflect.Uint32:
			cmd.PersistentFlags().Uint32(name, defV.(uint32), help)
		case reflect.Uint64:
			cmd.PersistentFlags().Uint64(name, defV.(uint64), help)
		case reflect.Float32:
			cmd.PersistentFlags().Float32(name, defV.(float32), help)
		case reflect.Float64:
			cmd.PersistentFlags().Float64(name, defV.(float64), help)
		case reflect.Slice:
			switch f.Type.Elem().Kind() {
			case reflect.String:
				cmd.PersistentFlags().StringSlice(name, defV.([]string), help)
			case reflect.Int:
				cmd.PersistentFlags().IntSlice(name, defV.([]int), help)
			case reflect.Int32:
				cmd.PersistentFlags().Int32Slice(name, defV.([]int32), help)
			case reflect.Int64:
				cmd.PersistentFlags().Int64Slice(name, defV.([]int64), help)
			case reflect.Uint:
				cmd.PersistentFlags().UintSlice(name, defV.([]uint), help)
			case reflect.Float32:
				cmd.PersistentFlags().Float32Slice(name, defV.([]float32), help)
			case reflect.Float64:
				cmd.PersistentFlags().Float64Slice(name, defV.([]float64), help)
			default:
				return fmt.Errorf("type []%s is not supported for field %s", f.Type.Elem(), f.Name)
			}
		case reflect.Struct:
			if f.Type.AssignableTo(reflect.TypeOf(VaultCredential{})) { // cli.VaultCredential
				cmd.PersistentFlags().String(name, defV.(VaultCredential).Path, help)
			} else {
				return fmt.Errorf("type %s is not supported for field %s", f.Type, f.Name)
			}
		default:
			return fmt.Errorf("type %s is not supported for field %s", f.Type, f.Name)
		}
	}

	return nil
}