diff options
Diffstat (limited to 'client/go/script-utils/logfmt/plusminusflag.go')
-rw-r--r-- | client/go/script-utils/logfmt/plusminusflag.go | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/client/go/script-utils/logfmt/plusminusflag.go b/client/go/script-utils/logfmt/plusminusflag.go new file mode 100644 index 00000000000..1768cf0e7be --- /dev/null +++ b/client/go/script-utils/logfmt/plusminusflag.go @@ -0,0 +1,67 @@ +// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +// vespa logfmt command +// Author: arnej + +package logfmt + +import ( + "fmt" + "strings" +) + +// common code for showFlags and levelFlags + +type plusMinusFlag interface { + flags() map[string]bool + name() string + unchanged() bool +} + +func trimPrefix(value, prefix string) (newValue string, hadPrefix bool) { + hadPrefix = strings.HasPrefix(value, prefix) + if hadPrefix { + newValue = strings.TrimPrefix(value, prefix) + } else { + newValue = value + } + return +} + +func applyPlusMinus(val string, target plusMinusFlag) error { + minus := strings.HasPrefix(val, "-") + plus := strings.HasPrefix(val, "+") + val = strings.ReplaceAll(val, "-", ",-") + val = strings.ReplaceAll(val, "+", ",+") + if target.unchanged() { + // user wants to reset flags? + if minus == false && plus == false { + for k, _ := range target.flags() { + target.flags()[k] = false + } + } + } + changeTo := !minus + for _, k := range strings.Split(val, ",") { + if suppress, minus := trimPrefix(k, "-"); minus { + k = suppress + changeTo = false + } + if surface, plus := trimPrefix(k, "+"); plus { + k = surface + changeTo = true + } + if k == "" { + continue + } + if k == "all" { + for k, _ := range target.flags() { + target.flags()[k] = changeTo + } + } else if _, ok := target.flags()[k]; !ok { + return fmt.Errorf("not a valid %s flag: '%s'", target.name(), k) + } else { + target.flags()[k] = changeTo + } + } + return nil +} |