Order all keys in config (#8541)

New changes

- return default values when sub-sys is
  not configured.
- state is hidden parameter now
- remove worm mode to be saved in config
This commit is contained in:
Harshavardhana
2019-11-20 15:10:24 -08:00
committed by GitHub
parent ca96560d56
commit 5ac4b517c9
51 changed files with 1436 additions and 642 deletions

View File

@@ -25,8 +25,39 @@ import (
"unicode"
)
// KVS each sub-system key, value
type KVS map[string]string
// KV - is a shorthand of each key value.
type KV struct {
Key string `json:"key"`
Value string `json:"value"`
}
// KVS - is a shorthand for some wrapper functions
// to operate on list of key values.
type KVS []KV
// Empty - return if kv is empty
func (kvs KVS) Empty() bool {
return len(kvs) == 0
}
// Get - returns the value of a key, if not found returns empty.
func (kvs KVS) Get(key string) string {
v, ok := kvs.Lookup(key)
if ok {
return v
}
return ""
}
// Lookup - lookup a key in a list of KVS
func (kvs KVS) Lookup(key string) (string, bool) {
for _, kv := range kvs {
if kv.Key == key {
return kv.Value, true
}
}
return "", false
}
// Targets sub-system targets
type Targets map[string]map[string]KVS
@@ -34,28 +65,25 @@ type Targets map[string]map[string]KVS
const (
stateKey = "state"
commentKey = "comment"
stateOn = "on"
stateOff = "off"
)
func (kvs KVS) String() string {
var s strings.Builder
for k, v := range kvs {
// Do not need to print if state is on
if k == stateKey && v == stateOn {
for _, kv := range kvs {
// Do not need to print state
if kv.Key == stateKey {
continue
}
if k == commentKey && v == "" {
if kv.Key == commentKey && kv.Value == "" {
continue
}
s.WriteString(k)
s.WriteString(kv.Key)
s.WriteString(KvSeparator)
spc := hasSpace(v)
spc := hasSpace(kv.Value)
if spc {
s.WriteString(KvDoubleQuote)
}
s.WriteString(v)
s.WriteString(kv.Value)
if spc {
s.WriteString(KvDoubleQuote)
}
@@ -140,14 +168,20 @@ func convertTargets(s string, targets Targets) error {
continue
}
if len(kv) == 1 && prevK != "" {
kvs[prevK] = strings.Join([]string{kvs[prevK], sanitizeValue(kv[0])}, KvSpaceSeparator)
kvs = append(kvs, KV{
Key: prevK,
Value: strings.Join([]string{kvs.Get(prevK), sanitizeValue(kv[0])}, KvSpaceSeparator),
})
continue
}
if len(kv) == 1 {
return fmt.Errorf("value for key '%s' cannot be empty", kv[0])
}
prevK = kv[0]
kvs[kv[0]] = sanitizeValue(kv[1])
kvs = append(kvs, KV{
Key: kv[0],
Value: sanitizeValue(kv[1]),
})
}
_, ok := targets[subSystemValue[0]]