Skip to content

Commit

Permalink
fix: show valid log levels in help
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Jul 10, 2024
1 parent 06ce27d commit d8fb3ca
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
7 changes: 6 additions & 1 deletion internal/app/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"

"github.com/hashicorp/terraform/command/cliconfig"
"github.com/leg100/pug/internal/logging"
Expand Down Expand Up @@ -53,11 +54,15 @@ func parse(stderr io.Writer, args []string) (config, error) {
fs.StringEnumVar(&cfg.FirstPage, 'f', "first-page", "The first page to open on startup.", "modules", "workspaces", "runs", "tasks", "logs")
fs.BoolVar(&cfg.Debug, 'd', "debug", "Log bubbletea messages to messages.log")
fs.BoolVar(&cfg.version, 'v', "version", "Print version.")
fs.StringEnumVar(&cfg.loggingOptions.Level, 'l', "log-level", "Logging level.", "info", "debug", "error", "warn")
_ = fs.String('c', "config", defaultConfigFile, "Path to config file.")

fs.BoolVar(&cfg.DisableReloadAfterApply, 0, "disable-reload-after-apply", "Disable automatic reload of state following an apply.")

{
usage := fmt.Sprintf("Logging level (valid: %s).", strings.Join(logging.ValidLevels(), ","))
fs.StringEnumVar(&cfg.loggingOptions.Level, 'l', "log-level", usage, logging.ValidLevels()...)
}

// Plugin cache is enabled not via pug flags but via terraform config
tfcfg, _ := cliconfig.LoadConfig()
cfg.PluginCache = (tfcfg.PluginCacheDir != "")
Expand Down
11 changes: 11 additions & 0 deletions internal/app/config_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app

import (
"bytes"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -177,3 +178,13 @@ func TestConfig(t *testing.T) {
})
}
}

func TestHelpFlag(t *testing.T) {
got := new(bytes.Buffer)
_, err := parse(got, []string{"--help"})
// Help flag should return error
require.Error(t, err)

want := "-l, --log-level STRING Logging level (valid: info,debug,error,warn). (default: info)"
assert.Contains(t, got.String(), want)
}
32 changes: 28 additions & 4 deletions internal/logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,40 @@ package logging
import (
"io"
"log/slog"
"slices"

"github.com/leg100/pug/internal/pubsub"
"github.com/leg100/pug/internal/resource"
"golang.org/x/exp/maps"
)

const DefaultLevel = "info"

var levels = map[string]slog.Level{
"debug": slog.LevelDebug,
"info": slog.LevelInfo,
"warn": slog.LevelWarn,
"error": slog.LevelError,
"debug": slog.LevelDebug,
DefaultLevel: slog.LevelInfo,
"warn": slog.LevelWarn,
"error": slog.LevelError,
}

// ValidLevels returns valid strings for choosing a log level. Returns the
// default log level first.
func ValidLevels() []string {
keys := maps.Keys(levels)
slices.SortFunc(keys, func(a, b string) int {
if a == DefaultLevel {
return -1
}
if b == DefaultLevel {
return 1
}
// Sort remaining in alphabetical order.
if a < b {
return -1
}
return 1
})
return keys
}

// NewLogger constructs Logger, a slog wrapper with additional functionality.
Expand Down

0 comments on commit d8fb3ca

Please sign in to comment.