aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Crute <mike@crute.us>2022-05-21 13:15:52 -0700
committerMike Crute <mike@crute.us>2022-05-21 13:15:52 -0700
commit64ccfb88e31207c0ad2090496613939403f50a7d (patch)
tree2080ae0b0c290613de32038ec791d5b39dd00e41
parent1c76cd697ee4564085b68212053a633fff5953a5 (diff)
downloadgolib-64ccfb88e31207c0ad2090496613939403f50a7d.tar.bz2
golib-64ccfb88e31207c0ad2090496613939403f50a7d.tar.xz
golib-64ccfb88e31207c0ad2090496613939403f50a7d.zip
cli: support Vault credentialscli/v0.2.2
-rw-r--r--cli/annotated_config.go18
-rw-r--r--cli/tolerant_pflag.go12
2 files changed, 21 insertions, 9 deletions
diff --git a/cli/annotated_config.go b/cli/annotated_config.go
index 074f8ac..8b89b3b 100644
--- a/cli/annotated_config.go
+++ b/cli/annotated_config.go
@@ -4,6 +4,7 @@ import (
4 "fmt" 4 "fmt"
5 "log" 5 "log"
6 "reflect" 6 "reflect"
7 "strings"
7 "time" 8 "time"
8 9
9 "code.crute.us/mcrute/golib/vault" 10 "code.crute.us/mcrute/golib/vault"
@@ -19,7 +20,6 @@ func MustGetConfig(cmd *cobra.Command, out interface{}) {
19func GetConfig(cmd *cobra.Command, out interface{}) error { 20func GetConfig(cmd *cobra.Command, out interface{}) error {
20 t := reflect.TypeOf(out).Elem() 21 t := reflect.TypeOf(out).Elem()
21 o := reflect.ValueOf(out).Elem() 22 o := reflect.ValueOf(out).Elem()
22 o.FieldByName("TemplateGlob").Set(reflect.ValueOf("test"))
23 23
24 for i := 0; i < t.NumField(); i++ { 24 for i := 0; i < t.NumField(); i++ {
25 tf := t.Field(i) 25 tf := t.Field(i)
@@ -109,7 +109,7 @@ func GetConfig(cmd *cobra.Command, out interface{}) error {
109 if err != nil { 109 if err != nil {
110 return fmt.Errorf("Error getting %s from vault: %w", name, err) 110 return fmt.Errorf("Error getting %s from vault: %w", name, err)
111 } 111 }
112 f.Set(reflect.ValueOf(VaultCredential{vk.Username, vk.Password})) 112 f.Set(reflect.ValueOf(VaultCredential{v, vk.Username, vk.Password}))
113 } else { 113 } else {
114 return fmt.Errorf("type %s is not supported for field %s", tf.Type, tf.Name) 114 return fmt.Errorf("type %s is not supported for field %s", tf.Type, tf.Name)
115 } 115 }
@@ -121,6 +121,15 @@ func GetConfig(cmd *cobra.Command, out interface{}) error {
121 return nil 121 return nil
122} 122}
123 123
124func inScope(desired, allowed string) bool {
125 for _, i := range strings.Split(allowed, ",") {
126 if strings.TrimSpace(i) == desired {
127 return true
128 }
129 }
130 return false
131}
132
124func AddFlags(cmd *cobra.Command, cfg interface{}, def interface{}, scope string) error { 133func AddFlags(cmd *cobra.Command, cfg interface{}, def interface{}, scope string) error {
125 t := reflect.TypeOf(cfg).Elem() 134 t := reflect.TypeOf(cfg).Elem()
126 d := reflect.ValueOf(def).Elem() 135 d := reflect.ValueOf(def).Elem()
@@ -135,7 +144,8 @@ func AddFlags(cmd *cobra.Command, cfg interface{}, def interface{}, scope string
135 } 144 }
136 145
137 // Non-matching scopes should not bind here (note root is "") 146 // Non-matching scopes should not bind here (note root is "")
138 if f.Tag.Get("flag-scope") != scope { 147 // Scopes can be a comma separated list
148 if !inScope(scope, f.Tag.Get("flag-scope")) {
139 continue 149 continue
140 } 150 }
141 151
@@ -188,7 +198,7 @@ func AddFlags(cmd *cobra.Command, cfg interface{}, def interface{}, scope string
188 } 198 }
189 case reflect.Struct: 199 case reflect.Struct:
190 if f.Type.AssignableTo(reflect.TypeOf(VaultCredential{})) { // cli.VaultCredential 200 if f.Type.AssignableTo(reflect.TypeOf(VaultCredential{})) { // cli.VaultCredential
191 cmd.PersistentFlags().String(name, "", help) 201 cmd.PersistentFlags().String(name, defV.(VaultCredential).Path, help)
192 } else { 202 } else {
193 return fmt.Errorf("type %s is not supported for field %s", f.Type, f.Name) 203 return fmt.Errorf("type %s is not supported for field %s", f.Type, f.Name)
194 } 204 }
diff --git a/cli/tolerant_pflag.go b/cli/tolerant_pflag.go
index b77939c..e9a1423 100644
--- a/cli/tolerant_pflag.go
+++ b/cli/tolerant_pflag.go
@@ -10,7 +10,7 @@ import (
10) 10)
11 11
12type VaultCredential struct { 12type VaultCredential struct {
13 Username, Password string 13 Path, Username, Password string
14} 14}
15 15
16type TolerantPflagSet struct { 16type TolerantPflagSet struct {
@@ -43,17 +43,19 @@ func (f *TolerantPflagSet) MayGetStringSlice(n string) []string {
43} 43}
44 44
45func (f *TolerantPflagSet) MayGetVaultCredential(n string) VaultCredential { 45func (f *TolerantPflagSet) MayGetVaultCredential(n string) VaultCredential {
46 vk, err := vault.GetVaultKey(f.MayGetString(n)) 46 p := f.MayGetString(n)
47 vk, err := vault.GetVaultKey(p)
47 if err != nil { 48 if err != nil {
48 return VaultCredential{} 49 return VaultCredential{}
49 } 50 }
50 return VaultCredential{vk.Username, vk.Password} 51 return VaultCredential{p, vk.Username, vk.Password}
51} 52}
52 53
53func (f *TolerantPflagSet) MustGetVaultCredential(n string) VaultCredential { 54func (f *TolerantPflagSet) MustGetVaultCredential(n string) VaultCredential {
54 vk, err := vault.GetVaultKey(f.MayGetString(n)) 55 p := f.MayGetString(n)
56 vk, err := vault.GetVaultKey(p)
55 if err != nil { 57 if err != nil {
56 log.Fatalf("Error getting %s from vault: %w", n, err) 58 log.Fatalf("Error getting %s from vault: %w", n, err)
57 } 59 }
58 return VaultCredential{vk.Username, vk.Password} 60 return VaultCredential{p, vk.Username, vk.Password}
59} 61}