Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow logging to be disabled #126

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion cmd/next.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ Environment Variables:

| Name | Description |
|------------|----------------------------------------------------------------|
| LOG_LEVEL | the level of logging when outputting to stderr (default: info) |
| LOG_LEVEL | the level of logging when printing to stderr (default: info) |
| NO_COLOR | switch to using an ASCII color profile within the terminal |
| NO_LOG | disable all log output |
| NSV_FORMAT | provide a go template for changing the default version format |
| NSV_PRETTY | pretty-print the output of the next semantic version in a |
| | given format. The format can be one of either full or compact. |
Expand Down Expand Up @@ -147,6 +148,10 @@ func printNext(vers []*nsv.Next, opts *Options) {
tags = append(tags, ver.Tag)
}

if !opts.NoLog {
fmt.Fprintln(opts.Err)
}

fmt.Fprint(opts.Out, strings.Join(tags, ","))

if opts.Show {
Expand Down
2 changes: 1 addition & 1 deletion cmd/next_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ feat: support pagination of search results`
gittest.InitRepository(t, gittest.WithLog(log))

var buf bytes.Buffer
cmd := nextCmd(&Options{Out: &buf, Logger: noopLogger})
cmd := nextCmd(&Options{Out: &buf, Err: io.Discard, Logger: noopLogger})
err := cmd.Execute()

require.NoError(t, err)
Expand Down
30 changes: 24 additions & 6 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ import (
"github.com/spf13/cobra"
)

var logLevels = []string{"debug", "info", "warn", "error", "fatal"}

type Options struct {
Err io.Writer `env:"-"`
Logger *log.Logger `env:"_"`
LogLevel string `env:"LOG_LEVEL"`
NoColor bool `env:"NO_COLOR"`
NoLog bool `env:"NO_LOG"`
Out io.Writer `env:"-"`
Paths []string `env:"-"`
Pretty string `env:"NSV_PRETTY"`
Expand All @@ -46,10 +49,11 @@ The power is at your fingertips.

Global Environment Variables:

| Name | Description |
|-----------|----------------------------------------------------------------|
| LOG_LEVEL | the level of logging when outputting to stderr (default: info) |
| NO_COLOR | switch to using an ASCII color profile within the terminal |`
| Name | Description |
|-----------|--------------------------------------------------------------|
| LOG_LEVEL | the level of logging when printing to stderr (default: info) |
| NO_COLOR | switch to using an ASCII color profile within the terminal |
| NO_LOG | disable all log output |`

type BuildDetails struct {
Version string `json:"version,omitempty"`
Expand Down Expand Up @@ -79,8 +83,15 @@ func Execute(out io.Writer, buildInfo BuildDetails) error {
lipgloss.SetColorProfile(termenv.Ascii)
}

var logw io.Writer

logw = opts.Err
if opts.NoLog {
logw = io.Discard
}

logLevel, _ := log.ParseLevel(opts.LogLevel)
opts.Logger = log.NewWithOptions(os.Stderr, log.Options{
opts.Logger = log.NewWithOptions(logw, log.Options{
Level: logLevel,
ReportCaller: false,
ReportTimestamp: false,
Expand All @@ -91,8 +102,11 @@ func Execute(out io.Writer, buildInfo BuildDetails) error {
}

flags := cmd.PersistentFlags()
flags.StringVar(&opts.LogLevel, "log-level", "info", "the level of logging when outputting to stderr")
flags.StringVar(&opts.LogLevel, "log-level", "info", "the level of logging when printing to stderr")
flags.BoolVar(&opts.NoColor, "no-color", false, "switch to using an ASCII color profile within the terminal")
flags.BoolVar(&opts.NoColor, "no-log", false, "disable all log output")

cmd.RegisterFlagCompletionFunc("log-level", logLevelFlagShellComp)

cmd.AddCommand(versionCmd(out, buildInfo),
manCmd(out),
Expand All @@ -105,6 +119,10 @@ func Execute(out io.Writer, buildInfo BuildDetails) error {
return cmd.Execute()
}

func logLevelFlagShellComp(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
return logLevels, cobra.ShellCompDirectiveDefault
}

func versionCmd(out io.Writer, buildInfo BuildDetails) *cobra.Command {
var short bool
cmd := &cobra.Command{
Expand Down
3 changes: 2 additions & 1 deletion cmd/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ Environment Variables:

| Name | Description |
|-----------------|----------------------------------------------------------------|
| LOG_LEVEL | the level of logging when outputting to stderr (default: info) |
| LOG_LEVEL | the level of logging when printing to stderr (default: info) |
| NO_COLOR | switch to using an ASCII color profile within the terminal |
| NO_LOG | disable all log output |
| NSV_FORMAT | provide a go template for changing the default version format |
| NSV_PRETTY | pretty-print the output of the next semantic version in a |
| | given format. The format can be one of either full or compact. |
Expand Down
6 changes: 3 additions & 3 deletions cmd/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestTagSkipsImpersonationIfGitConfigExists(t *testing.T) {
gittest.ConfigSet(t, "user.name", "poison-ivy", "user.email", "[email protected]")
gittest.CommitEmptyWithAuthor(t, "scarecrow", "[email protected]", "feat: capture metrics for populating dashboards")

cmd := tagCmd(&Options{Out: io.Discard, Logger: noopLogger})
cmd := tagCmd(&Options{Out: io.Discard, Err: io.Discard, Logger: noopLogger})
err := cmd.Execute()
require.NoError(t, err)

Expand All @@ -31,7 +31,7 @@ func TestTagSkipsImpersonationIfGitEnvVarsExist(t *testing.T) {
t.Setenv("GIT_COMMITTER_NAME", "joker")
t.Setenv("GIT_COMMITTER_EMAIL", "[email protected]")

cmd := tagCmd(&Options{Out: io.Discard, Logger: noopLogger})
cmd := tagCmd(&Options{Out: io.Discard, Err: io.Discard, Logger: noopLogger})
err := cmd.Execute()
require.NoError(t, err)

Expand All @@ -46,7 +46,7 @@ func TestTagWithTemplatedMessage(t *testing.T) {
(tag: 0.1.1) fix(ui): events are not being sorted as per user filters`
gittest.InitRepository(t, gittest.WithLog(log))

cmd := tagCmd(&Options{Out: io.Discard, Logger: noopLogger})
cmd := tagCmd(&Options{Out: io.Discard, Err: io.Discard, Logger: noopLogger})
cmd.SetArgs([]string{"--message", "chore: tagged {{.Tag}} from {{.PrevTag}}"})
err := cmd.Execute()
require.NoError(t, err)
Expand Down
Loading