forked from forj-oss/forjj
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getter.go
182 lines (156 loc) · 5.3 KB
/
getter.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
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
package main
import (
"fmt"
"github.com/forj-oss/forjj-modules/trace"
)
// Used to map between flags (cli) and Forjfile
type AppMapEntry struct {
cliFuncs map[string]func(name string) (string, bool, bool, error)
cli_obj string
cli_instance string
cli_field string
forj_section string
forj_instance string
forj_field string
}
func (a *Forj) AddMap(key, cli_obj, cli_instance, cli_field, forj_section, forj_instance, forj_field string) {
if a.appMapEntries == nil {
a.appMapEntries = make(map[string]AppMapEntry)
}
a.appMapEntries[key] = AppMapEntry{
cliFuncs: make(map[string]func(name string) (string, bool, bool, error)),
cli_obj: cli_obj,
cli_instance: cli_instance,
cli_field: cli_field,
forj_section: forj_section,
forj_instance: forj_instance,
forj_field: forj_field,
}
}
func (a *Forj) AddMapFunc(cliCmd, key string, getter func(name string) (string, bool, bool, error)) {
if v, found := a.appMapEntries[key]; found {
v.cliFuncs[cliCmd] = getter
a.appMapEntries[key] = v
}
}
// GetPrefs return a value found (or not) from different source of data
// 1: from cli, then exit if found
// 2: from Forjfile if found
// 3: Then cli default
func (a *Forj) GetPrefs(field string) (string, bool, error) {
var entry AppMapEntry
if e, found := a.appMapEntries[field]; !found {
return "", false, fmt.Errorf("Unable to get '%s' from Forjfile/cli mapping. Missing", field)
} else {
entry = e
}
var (
v string
found bool
isdefault bool
err error
)
if f, funcFound := entry.cliFuncs[a.contextAction]; funcFound {
v, found, isdefault, err = f(field)
} else {
v, found, isdefault, err = a.cli.GetStringValue(entry.cli_obj, entry.cli_instance, entry.cli_field)
}
if err != nil {
gotrace.Trace("Unable to get data from cli. %s", err)
err = nil
}
if found && !isdefault {
gotrace.Trace("Found Forjfile setting '%s' from cli : %s", entry.cli_field, v)
return v, found, err
}
if v2, found2, _ := a.f.GetString(entry.forj_section, entry.forj_instance, entry.forj_field); found2 {
gotrace.Trace("Found Forjfile setting '%s' from Forjfile : %s", entry.forj_field, v2)
return v2, found2, nil
}
if found {
gotrace.Trace("Found Forjfile setting '%s' from cli default: %s", entry.cli_field, v)
} else {
gotrace.Trace("Local setting '%s' not found from any of cli, Forjfile or cli default", field)
}
return v, found, err
}
// GetLocalPrefs return a value found (or not) from different source of data
// 1: from cli, then exit if found
// 2: from Workspace local settings if found
// Usually, LocalSetting is loaded from a Forjfile model and stored in the Workspace.
// 3: Then cli default
func (a *Forj) GetLocalPrefs(field string) (string, bool, error) {
var entry AppMapEntry
if e, found := a.appMapEntries[field]; !found {
return "", false, fmt.Errorf("Unable to get '%s' from Forjfile/cli mapping. Missing", field)
} else {
entry = e
}
var (
v string
found bool
isdefault bool
err error
)
if f, funcFound := entry.cliFuncs[a.contextAction]; funcFound {
v, found, isdefault, err = f(field)
} else {
v, found, isdefault, err = a.cli.GetStringValue(entry.cli_obj, entry.cli_instance, entry.cli_field)
}
if err != nil {
gotrace.Trace("Unable to get data from cli. %s", err)
err = nil
}
if found && !isdefault {
gotrace.Trace("Found Local setting '%s' from cli: %s", entry.forj_field, v)
return v, found, err
}
if v2, found2 := a.w.GetString(entry.forj_field); found2 {
gotrace.Trace("Found Local setting '%s' from workspace : %s", entry.forj_field, v2)
return v2, found2, nil
}
if found {
gotrace.Trace("Found Local setting '%s' from cli default: %s", entry.cli_field, v)
} else {
gotrace.Trace("Local setting '%s' not found from any of cli, Forjfile or cli default", entry.cli_field)
}
return v, found, err
}
// GetForgePrefs return a value found (or not) from Forjfile
func (a *Forj) GetForgePrefs(field string) (v string, found bool, _ error) {
var entry AppMapEntry
if e, found := a.appMapEntries[field]; !found {
return "", false, fmt.Errorf("Unable to get '%s' from Forjfile/cli mapping. Missing", field)
} else {
entry = e
}
v, found, _ = a.f.GetString(entry.forj_section, entry.forj_instance, entry.forj_field)
if found {
gotrace.Trace("Found Forjfile setting '%s' from Forjfile : %s", entry.forj_field, v)
} else {
gotrace.Trace("Forfile setting '%s' not found from Forjfile.", entry.forj_field)
}
return
}
// SetPrefsTo permits to store key/values to a known Forjfile data attached to a real file.
func (a *Forj) SetPrefsTo(dest, source, field, value string) error {
var entry AppMapEntry
if e, found := a.appMapEntries[field]; !found {
return fmt.Errorf("Unable to get '%s' from Forjfile/cli mapping. Missing", field)
} else {
entry = e
}
a.f.SetTo(dest, source, entry.forj_section, entry.forj_instance, entry.forj_field, value)
return nil
}
// SetPrefs uses global or merged data. As soon as a merge has been built, SetPrefs will save it in it.
func (a *Forj) SetPrefs(source, field, value string) error {
var entry AppMapEntry
if e, found := a.appMapEntries[field]; !found {
return fmt.Errorf("Unable to get '%s' from Forjfile/cli mapping. Missing", field)
} else {
entry = e
}
a.f.Set(source, entry.forj_section, entry.forj_instance, entry.forj_field, value)
return nil
}