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

chore: Convert to log/slog #519

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
13 changes: 5 additions & 8 deletions cmd/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"errors"
"fmt"
"log/slog"
"os"
"strings"

Expand Down Expand Up @@ -97,15 +98,13 @@ func execRun(cmd *cobra.Command, args []string) error {
return fmt.Errorf("Failed to get secret store: %w", err)
}

if pristine && verbose {
fmt.Fprintf(os.Stderr, "chamber: pristine mode engaged\n")
if pristine {
slog.Debug("chamber: pristine mode engaged")
}

var env environ.Environ
if strict {
if verbose {
fmt.Fprintf(os.Stderr, "chamber: strict mode engaged\n")
}
slog.Debug("chamber: strict mode engaged")
var err error
env = environ.Environ(os.Environ())
err = env.LoadStrict(cmd.Context(), secretStore, strictValue, pristine, services...)
Expand All @@ -130,9 +129,7 @@ func execRun(cmd *cobra.Command, args []string) error {
}
}

if verbose {
fmt.Fprintf(os.Stdout, "info: With environment %s\n", strings.Join(env, ","))
}
slog.Debug(fmt.Sprintf("info: With environment %s\n", strings.Join(env, ",")))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there any world where this is considered a breaking change? It looks like the comment in the help output for verbose before was wrong since partially we output to STDERR (only this line apparently was to STDOUT)

Do we need to keep that consistency for any reason?


return exec(command, commandArgs, env)
}
11 changes: 10 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"log/slog"
"os"
"regexp"
"strconv"
Expand Down Expand Up @@ -79,12 +80,13 @@ var RootCmd = &cobra.Command{
func init() {
RootCmd.PersistentFlags().IntVarP(&numRetries, "retries", "r", DefaultNumRetries, "For SSM or Secrets Manager, the number of retries we'll make before giving up; AKA $CHAMBER_RETRIES")
RootCmd.PersistentFlags().DurationVarP(&minThrottleDelay, "min-throttle-delay", "", 0, "DEPRECATED and no longer has any effect. Use retry-mode instead")
_ = RootCmd.PersistentFlags().MarkDeprecated("min-throttle-delay", "use --retry-mode instead")
RootCmd.PersistentFlags().StringVarP(&retryMode, "retry-mode", "", store.DefaultRetryMode.String(),
`For SSM, the model used to retry requests
`+aws.RetryModeStandard.String()+`
`+aws.RetryModeAdaptive.String(),
)
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "Print more information to STDOUT")
RootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "", false, "Print more information to STDERR")
RootCmd.PersistentFlags().StringVarP(&backendFlag, "backend", "b", "ssm",
`Backend to use; AKA $CHAMBER_SECRET_BACKEND
null: no-op
Expand Down Expand Up @@ -250,6 +252,13 @@ func prerun(cmd *cobra.Command, args []string) {
Set("chamber-version", chamberVersion),
})
}

if verbose {
levelVar := &slog.LevelVar{}
levelVar.Set(slog.LevelDebug)
handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: levelVar})
slog.SetDefault(slog.New(handler))
}
}

func postrun(cmd *cobra.Command, args []string) {
Expand Down
Loading