Skip to content

Commit

Permalink
Merge pull request #46 from moul/dev/moul/refactor-cli
Browse files Browse the repository at this point in the history
wip: refactor cli
  • Loading branch information
moul authored Sep 13, 2018
2 parents ac380bb + d159003 commit d5b035c
Show file tree
Hide file tree
Showing 11 changed files with 546 additions and 478 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ _inspired by this discussion: [jbenet/random-ideas#37](https://github.com/jbenet
$ export GITHUB_TOKEN=xxxx

# render and display the roadmap
$ depviz render --repos=moul/depviz | dot -Tpng > depviz-roadmap.png
$ depviz run moul/depviz | dot -Tpng > depviz-roadmap.png
$ open depviz-roadmap.png

# render and display the orphans
$ depviz render --repos=moul/depviz -t orphans | dot -Tpng > depviz-orphans.png
$ depviz run moul/depviz --show-orphans | dot -Tpng > depviz-orphans.png
$ open depviz-orphans.png
```

Expand All @@ -69,7 +69,7 @@ $ open depviz-orphans.png
```console
# install imgcat
$ go get github.com/olivere/iterm2-imagetools/cmd/imgcat
$ depviz render | dot -Tpng | imgcat
$ depviz run https://github.com/moul/depviz/issues/42 | dot -Tpng | imgcat
```

![](https://raw.githubusercontent.com/moul/depviz/master/examples/imgcat.png)
Expand Down
File renamed without changes.
40 changes: 23 additions & 17 deletions fetch.go → cmd_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"io/ioutil"
"log"
"os"
"sync"

"github.com/google/go-github/github"
Expand All @@ -19,53 +18,60 @@ import (
"golang.org/x/oauth2"
)

type fetchOptions struct {
type pullOptions struct {
// db
DBOpts dbOptions

// fetch
// pull
Repos []string
GithubToken string `mapstructure:"github-token"`
GitlabToken string `mapstructure:"gitlab-token"`
// includeExternalDeps bool

Targets []string
}

func (opts fetchOptions) String() string {
func (opts pullOptions) 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
func pullSetupFlags(flags *pflag.FlagSet, opts *pullOptions) {
flags.StringVarP(&opts.GithubToken, "github-token", "", "", "GitHub Token with 'issues' access")
flags.StringVarP(&opts.GitlabToken, "gitlab-token", "", "", "GitLab Token with 'issues' access")
viper.BindPFlags(flags)
}

func newFetchCommand() *cobra.Command {
opts := &fetchOptions{}
func newPullCommand() *cobra.Command {
opts := &pullOptions{}
cmd := &cobra.Command{
Use: "fetch",
Use: "pull",
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.Unmarshal(opts); err != nil {
return err
}
return fetch(opts)
opts.Targets = args
return pull(opts)
},
}
fetchSetupFlags(cmd.Flags(), opts)
pullSetupFlags(cmd.Flags(), opts)
dbSetupFlags(cmd.Flags(), &opts.DBOpts)
return cmd
}

func fetch(opts *fetchOptions) error {
logger().Debug("fetch", zap.Stringer("opts", *opts))
func pull(opts *pullOptions) error {
logger().Debug("pull", zap.Stringer("opts", *opts))

var (
wg sync.WaitGroup
allIssues []*Issue
out = make(chan []*Issue, 100)
)
wg.Add(len(opts.Repos))
for _, repoURL := range opts.Repos {

repos := getReposFromTargets(opts.Targets)

wg.Add(len(repos))
for _, repoURL := range repos {
repo := NewRepo(repoURL)
switch repo.Provider() {
case GitHubProvider:
Expand Down Expand Up @@ -107,7 +113,7 @@ func fetch(opts *fetchOptions) error {
}(repo)
case GitLabProvider:
go func(repo Repo) {
client := gitlab.NewClient(nil, os.Getenv("GITLAB_TOKEN"))
client := gitlab.NewClient(nil, opts.GitlabToken)
client.SetBaseURL(fmt.Sprintf("%s/api/v4", repo.SiteURL()))

//projectID := url.QueryEscape(repo.RepoPath())
Expand All @@ -123,7 +129,7 @@ func fetch(opts *fetchOptions) error {
for {
issues, resp, err := client.Issues.ListProjectIssues(projectID, opts)
if err != nil {
logger().Error("failed to fetch issues", zap.Error(err))
logger().Error("failed to pull issues", zap.Error(err))
return
}
total += len(issues)
Expand Down
66 changes: 33 additions & 33 deletions render.go → cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,67 +13,69 @@ import (
"go.uber.org/zap"
)

type renderOptions struct {
// fetch
FetchOpts fetchOptions
NoFetch bool
type runOptions struct {
// pull
PullOpts pullOptions
NoPull bool

// db
DBOpts dbOptions

// render
RenderType string
// run
ShowClosed bool `mapstructure:"show-closed"`
ShowOrphans bool
EpicLabel string
Destination string

Targets []string
//Preview bool
}

func (opts renderOptions) String() string {
func (opts runOptions) String() string {
out, _ := json.Marshal(opts)
return string(out)
}

func renderSetupFlags(flags *pflag.FlagSet, opts *renderOptions) {
flags.BoolVarP(&opts.NoFetch, "no-fetch", "f", false, "do not fetch new issues before rendering")
flags.StringVarP(&opts.RenderType, "type", "t", "roadmap", "graph type ('roadmap', 'orphans')")
func runSetupFlags(flags *pflag.FlagSet, opts *runOptions) {
flags.BoolVarP(&opts.NoPull, "no-pull", "f", false, "do not pull new issues before runing")
flags.BoolVarP(&opts.ShowClosed, "show-closed", "", false, "show closed issues")
flags.BoolVarP(&opts.ShowOrphans, "show-orphans", "", false, "show issues not linked to an epic")
flags.StringVarP(&opts.EpicLabel, "epic-label", "", "", "label used for epics (empty means issues with dependencies but without dependants)")
flags.StringVarP(&opts.EpicLabel, "epic-label", "", "epic", "label used for epics (empty means issues with dependencies but without dependants)")
flags.StringVarP(&opts.Destination, "destination", "", "-", "destination ('-' for stdout)")
//flags.BoolVarP(&opts.Preview, "preview", "p", false, "preview result")
viper.BindPFlags(flags)
}

func newRenderCommand() *cobra.Command {
opts := &renderOptions{}
func newRunCommand() *cobra.Command {
opts := &runOptions{}
cmd := &cobra.Command{
Use: "render",
Use: "run",
RunE: func(cmd *cobra.Command, args []string) error {
if err := viper.Unmarshal(opts); err != nil {
return err
}
if err := viper.Unmarshal(&opts.FetchOpts); err != nil {
if err := viper.Unmarshal(&opts.PullOpts); err != nil {
return err
}
if err := viper.Unmarshal(&opts.DBOpts); err != nil {
return err
}
opts.FetchOpts.DBOpts = opts.DBOpts
return render(opts)
opts.PullOpts.DBOpts = opts.DBOpts
opts.PullOpts.Targets = args
opts.Targets = args
return run(opts)
},
}
renderSetupFlags(cmd.Flags(), opts)
fetchSetupFlags(cmd.Flags(), &opts.FetchOpts)
runSetupFlags(cmd.Flags(), opts)
pullSetupFlags(cmd.Flags(), &opts.PullOpts)
dbSetupFlags(cmd.Flags(), &opts.DBOpts)
return cmd
}

func render(opts *renderOptions) error {
logger().Debug("render", zap.Stringer("opts", *opts))
if !opts.NoFetch || !dbExists(&opts.DBOpts) {
if err := fetch(&opts.FetchOpts); err != nil {
func run(opts *runOptions) error {
logger().Debug("run", zap.Stringer("opts", *opts))
if !opts.NoPull || !dbExists(&opts.DBOpts) {
if err := pull(&opts.PullOpts); err != nil {
return err
}
}
Expand All @@ -87,18 +89,16 @@ func render(opts *renderOptions) error {
return errors.Wrap(err, "failed to prepare issues")
}

var out string
switch opts.RenderType {
case "roadmap":
out, err = roadmapGraph(issues, opts)
case "orphans":
out, err = orphansGraph(issues, opts)
default:
err = fmt.Errorf("unknown graph type: %q", opts.RenderType)
issues.processEpicLinks()
if !opts.ShowClosed {
issues.HideClosed()
}
if err != nil {
return errors.Wrap(err, "failed to render graph")
if !opts.ShowOrphans && issues.HasNonOrphans() {
issues.HideOrphans()
}
issues.processEpicLinks()

out, err := graphviz(issues, opts)

var dest io.WriteCloser
switch opts.Destination {
Expand Down
3 changes: 0 additions & 3 deletions examples/depviz/.depviz.yml

This file was deleted.

6 changes: 2 additions & 4 deletions examples/depviz/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
.PHONY: run
run:
depviz render -f | dot -Tsvg > depviz.svg
depviz render -t orphans -f | dot -Tsvg > orphans.svg
cat orphans.svg | grep -s text || rm orphans.svg
depviz run --show-closed moul/depviz | dot -Tsvg > depviz.svg

.PHONY: preview
preview:
depviz render -f | dot -Tpng | imgcat
depviz run | dot -Tpng | imgcat
Loading

0 comments on commit d5b035c

Please sign in to comment.