forked from forj-oss/forjj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secrets_context.go
91 lines (80 loc) · 2.31 KB
/
secrets_context.go
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
package main
import (
"github.com/alecthomas/kingpin"
"github.com/forj-oss/forjj-modules/cli"
"github.com/forj-oss/forjj-modules/cli/interface"
"github.com/forj-oss/forjj-modules/cli/kingpinCli"
)
// Following deals with forjj-modules/cli at Parse time
// When some parameters needs to be retrieved at parsetime (Used in ParseContext)
// we need to let ParseContext get those values.
// So, we need to call flag/arg functions at init phase
//
// Ex:
type secretsContext struct {
params map[string]cli.ForjParam
cliContext clier.ParseContexter
}
func (s *secretsContext) init() {
s.params = make(map[string]cli.ForjParam)
}
// Create a cli flag from a kingpin flag (for forjj-modules/cli)
func (s *secretsContext) flag(name string, flag *kingpin.FlagClause) (cliFlag *kingpinCli.FlagClause) {
if s == nil {
return nil
}
cliFlag = kingpinCli.NewFlag(flag)
param := cli.NewForjFlag(cliFlag)
s.params[name] = param
return
}
// defineContext store the forjj-modules/cli context for GetStringValue
func (s *secretsContext) defineContext(context clier.ParseContexter) {
if s == nil {
return
}
s.cliContext = context
}
// getContextFlagValue Get Flag value from current cli context
func (s *secretsContext) getContextFlagValue(name string) (interface{}, bool) {
if s == nil {
return nil, false
}
param, found := s.params[name]
if !found {
return nil, false
}
return param.GetContextValue(s.cliContext)
}
// GetStringValue return value and status where the value were found.
//
// WARNING: Default status can be set only in cli load context phase (before parse)
// If we need to incorporate some data feed between real value and default value
// it must be done and saved during load context phase. (ie ParseContext() in cli_context.go)
func (s *secretsContext) GetStringValue(field string) (value string, found, isDefault bool, _ error) {
var param cli.ForjParam
param, found = s.params[field]
if !found {
return
}
var v interface{}
if !forj_app.cli.IsParsePhase() {
v, found = param.GetContextValue(s.cliContext)
if !found {
return
}
if fieldValue, ok := v.(string); ok {
value = fieldValue
} else if fieldDefault, ok := v.(*string); ok {
value = *fieldDefault
isDefault = true
}
} else {
found = param.IsFound()
if !found {
return
}
value = param.GetStringValue()
}
return
}