Skip to content

Commit

Permalink
feat: add '--verbose' cli option + switch to zap
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Sep 5, 2018
1 parent 15c9b56 commit a7371be
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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\"'"
13 changes: 9 additions & 4 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down
16 changes: 13 additions & 3 deletions fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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")
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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")
}
7 changes: 7 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package main

import "go.uber.org/zap"

func logger() *zap.Logger {
return zap.L()
}
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -19,17 +22,34 @@ func main() {
}

var (
//verbose bool
verbose bool
cfgFile string
)

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 {
Expand Down
10 changes: 8 additions & 2 deletions render.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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')")
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a7371be

Please sign in to comment.