Allow DelKVS to delete specific sub-system fields. (#15354)

This commit is contained in:
Taran Pelkey 2022-07-22 17:48:23 -04:00 committed by GitHub
parent b0d70a0e5e
commit e83930333b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -773,35 +773,40 @@ func (c Config) GetKVS(s string, defaultKVS map[string]KVS) (Targets, error) {
// DelKVS - delete a specific key. // DelKVS - delete a specific key.
func (c Config) DelKVS(s string) error { func (c Config) DelKVS(s string) error {
if len(s) == 0 { subSys, inputs, tgt, err := GetSubSys(s)
return Errorf("input arguments cannot be empty") if err != nil {
} if !SubSystems.Contains(subSys) && len(inputs) == 1 {
inputs := strings.Fields(s) // Unknown sub-system found try to remove it anyways.
if len(inputs) > 1 { delete(c, subSys)
return Errorf("invalid number of arguments %s", s) return nil
}
subSystemValue := strings.SplitN(inputs[0], SubSystemSeparator, 2)
if len(subSystemValue) == 0 {
return Errorf("invalid number of arguments %s", s)
}
if !SubSystems.Contains(subSystemValue[0]) {
// Unknown sub-system found try to remove it anyways.
delete(c, subSystemValue[0])
return nil
}
tgt := Default
subSys := subSystemValue[0]
if len(subSystemValue) == 2 {
if len(subSystemValue[1]) == 0 {
return Errorf("sub-system target '%s' cannot be empty", s)
} }
tgt = subSystemValue[1] return err
} }
_, ok := c[subSys][tgt]
ck, ok := c[subSys][tgt]
if !ok { if !ok {
return Errorf("sub-system %s already deleted", s) return Errorf("sub-system %s:%s already deleted or does not exist", subSys, tgt)
}
if len(inputs) == 2 {
currKVS := ck.Clone()
defKVS := DefaultKVS[subSys]
for _, delKey := range strings.Fields(inputs[1]) {
_, ok := currKVS.Lookup(delKey)
if !ok {
return Errorf("key %s doesn't exist", delKey)
}
defVal, isDef := defKVS.Lookup(delKey)
if isDef {
currKVS.Set(delKey, defVal)
} else {
currKVS.Delete(delKey)
}
}
c[subSys][tgt] = currKVS
} else {
delete(c[subSys], tgt)
} }
delete(c[subSys], tgt)
return nil return nil
} }