From e83930333b6bdc4066ec6990d7fbb491f9cd4c94 Mon Sep 17 00:00:00 2001 From: Taran Pelkey <84724408+taran-p@users.noreply.github.com> Date: Fri, 22 Jul 2022 17:48:23 -0400 Subject: [PATCH] Allow DelKVS to delete specific sub-system fields. (#15354) --- internal/config/config.go | 55 +++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/internal/config/config.go b/internal/config/config.go index 52052cc34..932e316a0 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -773,35 +773,40 @@ func (c Config) GetKVS(s string, defaultKVS map[string]KVS) (Targets, error) { // DelKVS - delete a specific key. func (c Config) DelKVS(s string) error { - if len(s) == 0 { - return Errorf("input arguments cannot be empty") - } - inputs := strings.Fields(s) - if len(inputs) > 1 { - return Errorf("invalid number of arguments %s", s) - } - 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) + subSys, inputs, tgt, err := GetSubSys(s) + if err != nil { + if !SubSystems.Contains(subSys) && len(inputs) == 1 { + // Unknown sub-system found try to remove it anyways. + delete(c, subSys) + return nil } - tgt = subSystemValue[1] + return err } - _, ok := c[subSys][tgt] + + ck, ok := c[subSys][tgt] 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 }