diff options
author | Mike Crute <mike@crute.us> | 2022-01-09 23:05:11 -0800 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2022-01-09 23:05:11 -0800 |
commit | f4f23715cf22f06b7fb3b7663d054c20d220ce13 (patch) | |
tree | 332fbe93e34407e2e0c80f698054a3ad2d175376 | |
parent | ec381c0fe848d030c494ecbe960f04ebcb65daf7 (diff) | |
download | golib-cli/v0.2.1.tar.bz2 golib-cli/v0.2.1.tar.xz golib-cli/v0.2.1.zip |
cli: support required argscli/v0.2.1
-rw-r--r-- | cli/annotated_config.go | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/cli/annotated_config.go b/cli/annotated_config.go index feb9c6e..074f8ac 100644 --- a/cli/annotated_config.go +++ b/cli/annotated_config.go | |||
@@ -31,12 +31,18 @@ func GetConfig(cmd *cobra.Command, out interface{}) error { | |||
31 | continue | 31 | continue |
32 | } | 32 | } |
33 | 33 | ||
34 | // Pretty much only string and struct can be tested | ||
35 | req := tf.Tag.Get("flag-required") == "true" | ||
36 | |||
34 | switch f.Type().Kind() { | 37 | switch f.Type().Kind() { |
35 | case reflect.Bool: | 38 | case reflect.Bool: |
36 | v, _ := cmd.Flags().GetBool(name) | 39 | v, _ := cmd.Flags().GetBool(name) |
37 | f.Set(reflect.ValueOf(v)) | 40 | f.Set(reflect.ValueOf(v)) |
38 | case reflect.String: | 41 | case reflect.String: |
39 | v, _ := cmd.Flags().GetString(name) | 42 | v, _ := cmd.Flags().GetString(name) |
43 | if req && v == "" { | ||
44 | log.Fatalf("Flag %s is required but not provided", name) | ||
45 | } | ||
40 | f.Set(reflect.ValueOf(v)) | 46 | f.Set(reflect.ValueOf(v)) |
41 | case reflect.Int: | 47 | case reflect.Int: |
42 | v, _ := cmd.Flags().GetInt(name) | 48 | v, _ := cmd.Flags().GetInt(name) |
@@ -96,6 +102,9 @@ func GetConfig(cmd *cobra.Command, out interface{}) error { | |||
96 | case reflect.Struct: | 102 | case reflect.Struct: |
97 | if tf.Type.AssignableTo(reflect.TypeOf(VaultCredential{})) { // cli.VaultCredential | 103 | if tf.Type.AssignableTo(reflect.TypeOf(VaultCredential{})) { // cli.VaultCredential |
98 | v, _ := cmd.Flags().GetString(name) | 104 | v, _ := cmd.Flags().GetString(name) |
105 | if req && v == "" { | ||
106 | log.Fatalf("Flag %s is required but not provided", name) | ||
107 | } | ||
99 | vk, err := vault.GetVaultKey(v) | 108 | vk, err := vault.GetVaultKey(v) |
100 | if err != nil { | 109 | if err != nil { |
101 | return fmt.Errorf("Error getting %s from vault: %w", name, err) | 110 | return fmt.Errorf("Error getting %s from vault: %w", name, err) |