Skip to content

Commit

Permalink
Additional/unknown flags are warnings only now. Fixes #44
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Oct 10, 2023
1 parent 0ecac18 commit 087897a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
9 changes: 8 additions & 1 deletion configmap/loglevel_change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,20 @@ func TestDynamicLogLevel(t *testing.T) {
if err = os.Mkdir(pDir, 0o755); err != nil {
t.Fatalf("unable to make %v: %v", pDir, err)
}
fName := path.Join(pDir, "extra_flag")
if err = os.WriteFile(fName, []byte("ignored"), 0o644); err != nil {
t.Fatalf("unable to write %v: %v", fName, err)
}
var u *configmap.Updater
log.SetLogLevel(log.Debug)
if u, err = configmap.Setup(flag.CommandLine, pDir); err != nil {
t.Fatalf("unexpected error setting up config watch: %v", err)
}
defer u.Stop()
fName := path.Join(pDir, "loglevel")
if u.Warnings() != 1 {
t.Errorf("Expected exactly 1 warning (extra flag), got %d", u.Warnings())
}
fName = path.Join(pDir, "loglevel")
// Test also the new normalization (space trimmimg and captitalization)
if err = os.WriteFile(fName, []byte(" InFO\n\n"), 0o644); err != nil {
t.Fatalf("unable to write %v: %v", fName, err)
Expand Down
11 changes: 11 additions & 0 deletions configmap/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type Updater struct {
watcher *fsnotify.Watcher
flagSet *flag.FlagSet
done chan bool
warnings int // Count of unknown flags that have been logged.
}

// Setup is a combination/shortcut for New+Initialize+Start.
Expand Down Expand Up @@ -72,6 +73,7 @@ func New(flagSet *flag.FlagSet, dirPath string) (*Updater, error) {
watcher: watcher,
started: false,
done: nil,
warnings: 0,
}, nil
}

Expand Down Expand Up @@ -124,9 +126,13 @@ func (u *Updater) readAll(dynamicOnly bool) error {
continue
}
fullPath := path.Join(u.dirPath, f.Name())
log.S(log.Debug, "checking flag", log.Str("flag", f.Name()), log.Str("path", fullPath))
if err := u.readFlagFile(fullPath, dynamicOnly); err != nil {
if errors.Is(err, errFlagNotDynamic) && dynamicOnly {
// ignore
} else if errors.Is(err, errFlagNotFound) {
log.S(log.Warning, "config map for unknown flag", log.Str("flag", f.Name()), log.Str("path", fullPath))
u.warnings++
} else {
errorStrings = append(errorStrings, fmt.Sprintf("flag %v: %v", f.Name(), err.Error()))
}
Expand All @@ -139,6 +145,11 @@ func (u *Updater) readAll(dynamicOnly bool) error {
return nil
}

// Return the warnings count. Thread safety?
func (u *Updater) Warnings() int {
return u.warnings
}

func (u *Updater) readFlagFile(fullPath string, dynamicOnly bool) error {
flagName := path.Base(fullPath)
flag := u.flagSet.Lookup(flagName)
Expand Down

0 comments on commit 087897a

Please sign in to comment.