Skip to content

Commit

Permalink
fix: verbose mode in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
shyim committed May 7, 2024
1 parent 0334796 commit c09feaa
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"context"
"os"

"github.com/spf13/cobra"

Expand All @@ -26,6 +27,10 @@ var rootCmd = &cobra.Command{
}

func Execute(ctx context.Context) {
rootCmd.ParseFlags(os.Args)
verbose, _ := rootCmd.PersistentFlags().GetBool("verbose")

ctx = logging.WithLogger(ctx, logging.NewLogger(verbose))
accountApi.SetUserAgent("shopware-cli/" + version)

if err := rootCmd.ExecuteContext(ctx); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion internal/esbuild/esbuild_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

func getTestContext() context.Context {
logger := logging.NewLogger()
logger := logging.NewLogger(false)

return logging.WithLogger(context.TODO(), logger)
}
Expand Down
7 changes: 6 additions & 1 deletion logging/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logging

import (
"context"
"fmt"

Check failure on line 5 in logging/logger.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used (typecheck)

Check failure on line 5 in logging/logger.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used) (typecheck)

Check failure on line 5 in logging/logger.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used) (typecheck)

Check failure on line 5 in logging/logger.go

View workflow job for this annotation

GitHub Actions / lint

"fmt" imported and not used) (typecheck)

Check failure on line 5 in logging/logger.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest

"fmt" imported and not used

Check failure on line 5 in logging/logger.go

View workflow job for this annotation

GitHub Actions / run

"fmt" imported and not used

Check failure on line 5 in logging/logger.go

View workflow job for this annotation

GitHub Actions / macos-14

"fmt" imported and not used

Check failure on line 5 in logging/logger.go

View workflow job for this annotation

GitHub Actions / windows-latest

"fmt" imported and not used

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
Expand All @@ -15,7 +16,7 @@ const loggerKey = contextKey("logging")

var fallbackLogger *zap.SugaredLogger

func NewLogger() *zap.SugaredLogger {
func NewLogger(verbose bool) *zap.SugaredLogger {
loggerCfg := zap.NewDevelopmentConfig()
loggerCfg.EncoderConfig.MessageKey = "message"
loggerCfg.EncoderConfig.TimeKey = "timestamp"
Expand All @@ -26,6 +27,10 @@ func NewLogger() *zap.SugaredLogger {
loggerCfg.DisableCaller = true
loggerCfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder

if !verbose {
loggerCfg.Level = zap.NewAtomicLevelAt(zap.InfoLevel)
}

logger, err := loggerCfg.Build()
if err != nil {
logger = zap.NewNop()
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import (
"context"

"github.com/FriendsOfShopware/shopware-cli/cmd"
"github.com/FriendsOfShopware/shopware-cli/logging"
)

func main() {
logger := logging.NewLogger()
cmd.Execute(logging.WithLogger(context.Background(), logger))
cmd.Execute(context.Background())
}

0 comments on commit c09feaa

Please sign in to comment.