github.com/webonyx/up@v0.7.4-0.20180808230834-91b94e551323/internal/secret/secret.go (about) 1 package secret 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/apex/up" 8 ) 9 10 // delimiter. 11 const delim = "/" 12 13 // Format returns a normalized env var name. 14 func Format(app, stage, name string) string { 15 if stage == "" { 16 stage = "all" 17 } 18 19 return "/" + strings.Join([]string{ 20 "up", 21 app, 22 stage, 23 name, 24 }, delim) 25 } 26 27 // Parse returns the name stripped of prefix. 28 func Parse(s string) (app, stage, name string) { 29 p := strings.Split(s, delim) 30 app, stage, name = p[2], p[3], p[4] 31 return 32 } 33 34 // GroupByStage returns secrets mapped by stage. 35 func GroupByStage(secrets []*up.Secret) map[string][]*up.Secret { 36 m := make(map[string][]*up.Secret) 37 38 for _, s := range secrets { 39 m[s.Stage] = append(m[s.Stage], s) 40 } 41 42 return m 43 } 44 45 // FilterByApp returns secrets filtered by name. 46 func FilterByApp(secrets []*up.Secret, name string) (v []*up.Secret) { 47 for _, s := range secrets { 48 if s.App == name { 49 v = append(v, s) 50 } 51 } 52 return 53 } 54 55 // Env returns a slice of env variables. 56 func Env(secrets []*up.Secret) (env []string) { 57 for _, s := range secrets { 58 env = append(env, fmt.Sprintf("%s=%s", s.Name, s.Value)) 59 } 60 return 61 } 62 63 // String returns a string representation of the secret, 64 // using * as the wildcard when the secret is sensitive. 65 func String(s *up.Secret) string { 66 switch s.Type { 67 case "String": 68 return s.Value 69 case "SecureString": 70 return strings.Repeat("*", len(s.Value)) 71 default: 72 return "" 73 } 74 }