From a7371be7e7f183207a4c8137bf9afd5039e682a5 Mon Sep 17 00:00:00 2001 From: Manfred Touron Date: Wed, 5 Sep 2018 14:18:21 +0200 Subject: [PATCH] feat: add '--verbose' cli option + switch to zap --- Makefile | 2 +- db.go | 13 +++++++++---- fetch.go | 16 +++++++++++++--- logger.go | 7 +++++++ main.go | 24 ++++++++++++++++++++++-- render.go | 10 ++++++++-- 6 files changed, 60 insertions(+), 12 deletions(-) create mode 100644 logger.go diff --git a/Makefile b/Makefile index 697f04f79..3c31b8b87 100644 --- a/Makefile +++ b/Makefile @@ -5,4 +5,4 @@ install: .PHONY: update_examples update_examples: for dir in $(wildcard examples/*); do (cd $$dir && make); done - git commit examples -m "chore: update examples" + echo "now you can run 'git commit examples -m \"chore: update examples\"'" diff --git a/db.go b/db.go index 4b5d816ad..8ae46ec15 100644 --- a/db.go +++ b/db.go @@ -4,19 +4,24 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "os" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" + "go.uber.org/zap" ) type dbOptions struct { Path string `mapstructure:"db-path"` } +func (opts dbOptions) String() string { + out, _ := json.Marshal(opts) + return string(out) +} + func dbSetupFlags(flags *pflag.FlagSet, opts *dbOptions) { flags.StringVarP(&opts.Path, "db-path", "", "./depviz.db", "depviz database path") viper.BindPFlags(flags) @@ -46,7 +51,7 @@ func newDBDumpCommand() *cobra.Command { } func dbDump(opts *dbOptions) error { - log.Printf("dbDump(%v)", *opts) + logger().Debug("dbDump", zap.Stringer("opts", *opts)) issues, err := dbLoad(opts) if err != nil { return err @@ -60,13 +65,13 @@ func dbDump(opts *dbOptions) error { } func dbExists(opts *dbOptions) bool { - log.Printf("dbExists(%v)", *opts) + logger().Debug("dbExists", zap.Stringer("opts", *opts)) _, err := os.Stat(opts.Path) return err == nil } func dbLoad(opts *dbOptions) (Issues, error) { - log.Printf("dbLoad(%v)", *opts) + logger().Debug("dbLoad", zap.Stringer("opts", *opts)) var issues []*Issue content, err := ioutil.ReadFile(opts.Path) if err != nil { diff --git a/fetch.go b/fetch.go index cf0a54490..8c5d9e1ba 100644 --- a/fetch.go +++ b/fetch.go @@ -13,6 +13,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" + "go.uber.org/zap" "golang.org/x/oauth2" ) @@ -26,6 +27,11 @@ type fetchOptions struct { // includeExternalDeps bool } +func (opts fetchOptions) String() string { + out, _ := json.Marshal(opts) + return string(out) +} + func fetchSetupFlags(flags *pflag.FlagSet, opts *fetchOptions) { flags.StringSliceVarP(&opts.Repos, "repos", "r", []string{}, "list of repositories to aggregate issues from") // FIXME: get the default value dynamically from .git, if present flags.StringVarP(&opts.GithubToken, "github-token", "", "", "GitHub Token with 'issues' access") @@ -49,7 +55,7 @@ func newFetchCommand() *cobra.Command { } func fetch(opts *fetchOptions) error { - log.Printf("fetch(%v)", *opts) + logger().Debug("fetch", zap.Stringer("opts", *opts)) ctx := context.Background() ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: opts.GithubToken}) tc := oauth2.NewClient(ctx, ts) @@ -77,7 +83,11 @@ func fetch(opts *fetchOptions) error { return } total += len(issues) - log.Printf("repo:%s new-issues:%d total:%d", repo, len(issues), total) + logger().Debug("paginate", + zap.String("repo", repo), + zap.Int("new-issues", len(issues)), + zap.Int("total-issues", total), + ) out <- issues if resp.NextPage == 0 { break @@ -97,6 +107,6 @@ func fetch(opts *fetchOptions) error { if err != nil { return err } - log.Printf("GitHub API Rate limit: %s", rateLimits.GetCore().String()) + logger().Debug("github API rate limiting", zap.Stringer("limit", rateLimits.GetCore())) return errors.Wrap(ioutil.WriteFile(opts.DBOpts.Path, issuesJson, 0644), "failed to write db file") } diff --git a/logger.go b/logger.go new file mode 100644 index 000000000..acec18d09 --- /dev/null +++ b/logger.go @@ -0,0 +1,7 @@ +package main + +import "go.uber.org/zap" + +func logger() *zap.Logger { + return zap.L() +} diff --git a/main.go b/main.go index ecd4482c5..f5d3237ec 100644 --- a/main.go +++ b/main.go @@ -8,9 +8,12 @@ import ( "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/viper" + "go.uber.org/zap" + "go.uber.org/zap/zapcore" ) func main() { + defer logger().Sync() rootCmd := newRootCommand() if err := rootCmd.Execute(); err != nil { _, _ = fmt.Fprintf(os.Stderr, "%v\n", err) @@ -19,7 +22,7 @@ func main() { } var ( - //verbose bool + verbose bool cfgFile string ) @@ -27,9 +30,26 @@ func newRootCommand() *cobra.Command { cmd := &cobra.Command{ Use: "depviz", } - cmd.PersistentFlags().BoolP("help", "h", false, "Print usage") + cmd.PersistentFlags().BoolP("help", "h", false, "print usage") + cmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "verbose mode") cmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "", "config file (default is ./.depviz.yml)") cmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error { + // configure zap + config := zap.NewDevelopmentConfig() + if verbose { + config.Level.SetLevel(zapcore.DebugLevel) + } else { + config.Level.SetLevel(zapcore.InfoLevel) + } + config.DisableStacktrace = true + config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder + l, err := config.Build() + if err != nil { + return err + } + zap.ReplaceGlobals(l) + + // configure viper if cfgFile != "" { viper.SetConfigFile(cfgFile) } else { diff --git a/render.go b/render.go index 40ae9c043..b2c289340 100644 --- a/render.go +++ b/render.go @@ -1,15 +1,16 @@ package main import ( + "encoding/json" "fmt" "io" - "log" "os" "github.com/pkg/errors" "github.com/spf13/cobra" "github.com/spf13/pflag" "github.com/spf13/viper" + "go.uber.org/zap" ) type renderOptions struct { @@ -29,6 +30,11 @@ type renderOptions struct { //Preview bool } +func (opts renderOptions) String() string { + out, _ := json.Marshal(opts) + return string(out) +} + func renderSetupFlags(flags *pflag.FlagSet, opts *renderOptions) { flags.BoolVarP(&opts.ForceFetch, "fetch", "f", false, "force fetch before rendering") flags.StringVarP(&opts.RenderType, "type", "t", "roadmap", "graph type ('roadmap', 'orphans')") @@ -65,7 +71,7 @@ func newRenderCommand() *cobra.Command { } func render(opts *renderOptions) error { - log.Printf("render(%v)", *opts) + logger().Debug("render", zap.Stringer("opts", *opts)) if opts.ForceFetch || !dbExists(&opts.DBOpts) { if err := fetch(&opts.FetchOpts); err != nil { return err