Skip to content

Commit

Permalink
chore: Convert to log/slog
Browse files Browse the repository at this point in the history
The small amount of verbose logging in the exec command has been
converted to debug logging under log/slog. Accordingly, the `--verbose`
command line flag has been deprecated in favor of the more flexible
`--log-level` flag.
  • Loading branch information
bhavanki committed Aug 19, 2024
1 parent 22441a2 commit 768847a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 10 deletions.
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")

Check warning on line 102 in cmd/exec.go

View check run for this annotation

Codecov / codecov/patch

cmd/exec.go#L101-L102

Added lines #L101 - L102 were not covered by tests
}

var env environ.Environ
if strict {
if verbose {
fmt.Fprintf(os.Stderr, "chamber: strict mode engaged\n")
}
slog.Debug("chamber: strict mode engaged")

Check warning on line 107 in cmd/exec.go

View check run for this annotation

Codecov / codecov/patch

cmd/exec.go#L107

Added line #L107 was not covered by tests
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, ",")))

Check warning on line 132 in cmd/exec.go

View check run for this annotation

Codecov / codecov/patch

cmd/exec.go#L132

Added line #L132 was not covered by tests

return exec(command, commandArgs, env)
}
30 changes: 28 additions & 2 deletions 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 All @@ -24,7 +25,9 @@ var (
validTagKeyFormat = regexp.MustCompile(`^[A-Za-z0-9 +\-=\._:/@]{1,128}$`)
validTagValueFormat = regexp.MustCompile(`^[A-Za-z0-9 +\-=\._:/@]{1,256}$`)

// Deprecated: Use logLevel instead.
verbose bool
logLevel string
numRetries int
// Deprecated: Use retryMode instead.
minThrottleDelay time.Duration
Expand Down Expand Up @@ -72,19 +75,22 @@ var RootCmd = &cobra.Command{
Use: "chamber",
Short: "CLI for storing secrets",
SilenceUsage: true,
PersistentPreRun: prerun,
PersistentPreRunE: prerun,
PersistentPostRun: postrun,
}

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().MarkDeprecated("verbose", "use --log-level debug instead")
RootCmd.PersistentFlags().StringVar(&logLevel, "log-level", "", "Log level")
RootCmd.PersistentFlags().StringVarP(&backendFlag, "backend", "b", "ssm",
`Backend to use; AKA $CHAMBER_SECRET_BACKEND
null: no-op
Expand Down Expand Up @@ -236,7 +242,7 @@ func getSecretStore(ctx context.Context) (store.Store, error) {
return s, err
}

func prerun(cmd *cobra.Command, args []string) {
func prerun(cmd *cobra.Command, args []string) error {

Check warning on line 245 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L245

Added line #L245 was not covered by tests
if analyticsEnabled {
// set up analytics client
analyticsClient, _ = analytics.NewWithConfig(analyticsWriteKey, analytics.Config{
Expand All @@ -250,6 +256,26 @@ func prerun(cmd *cobra.Command, args []string) {
Set("chamber-version", chamberVersion),
})
}

var slogLeveler slog.Leveler
if verbose {
levelVar := &slog.LevelVar{}
levelVar.Set(slog.LevelDebug)
slogLeveler = levelVar

Check warning on line 264 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L260-L264

Added lines #L260 - L264 were not covered by tests
}
if logLevel != "" {
levelVar := &slog.LevelVar{}
err := levelVar.UnmarshalText([]byte(logLevel))
if err != nil {
return fmt.Errorf("Failed to parse log level: %w", err)

Check warning on line 270 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L266-L270

Added lines #L266 - L270 were not covered by tests
}
slogLeveler = levelVar

Check warning on line 272 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L272

Added line #L272 was not covered by tests
}
if slogLeveler != nil {
handler := slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slogLeveler})
slog.SetDefault(slog.New(handler))

Check warning on line 276 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L274-L276

Added lines #L274 - L276 were not covered by tests
}
return nil

Check warning on line 278 in cmd/root.go

View check run for this annotation

Codecov / codecov/patch

cmd/root.go#L278

Added line #L278 was not covered by tests
}

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

0 comments on commit 768847a

Please sign in to comment.