Skip to content

Commit

Permalink
fix: refacto + gci
Browse files Browse the repository at this point in the history
Signed-off-by: ismael FALL <ismael.fall@epitech.eu>
  • Loading branch information
Doozers committed Mar 23, 2023
1 parent 3d0ac31 commit 2bc4491
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 103 deletions.
120 changes: 37 additions & 83 deletions cmd/depviz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,14 @@ var (
serverGitHubClientID = serverFlags.String("github-client-id", "", "GitHub client ID")
serverGitHubClientSecret = serverFlags.String("github-client-secret", "", "GitHub client secret")

runFlags = flag.NewFlagSet("run", flag.ExitOnError)
// runNoPull = runFlags.Bool("no-pull", false, "don't pull providers (graph only)")
runNoGraph = runFlags.Bool("no-graph", false, "don't generate graph (pull only)")
// runResync = runFlags.Bool("resync", false, "resync already synced content")
// runGitHubToken = runFlags.String("github-token", "", "GitHub token")
runNoPert = runFlags.Bool("no-pert", false, "disable PERT computing")
runFormat = runFlags.String("format", "dot", "output format")
runVertical = runFlags.Bool("vertical", false, "vertical mode")
runHidePRs = runFlags.Bool("hide-prs", false, "hide PRs")
runHideExternalDeps = runFlags.Bool("hide-external-deps", false, "hide dependencies outside of the specified targets")
runHideIsolated = runFlags.Bool("hide-isolated", false, "hide isolated tasks")
runShowClosed = runFlags.Bool("show-closed", false, "show closed tasks")
genFlags = flag.NewFlagSet("gen", flag.ExitOnError)
genNoGraph = genFlags.Bool("no-graph", false, "don't generate graph (pull only)")
genNoPert = genFlags.Bool("no-pert", false, "disable PERT computing")
genVertical = genFlags.Bool("vertical", false, "vertical mode")
genHidePRs = genFlags.Bool("hide-prs", false, "hide PRs")
genHideExternalDeps = genFlags.Bool("hide-external-deps", false, "hide dependencies outside of the specified targets")
genHideIsolated = genFlags.Bool("hide-isolated", false, "hide isolated tasks")
genShowClosed = genFlags.Bool("show-closed", false, "show closed tasks")

fetchFlags = flag.NewFlagSet("fetch", flag.ExitOnError)
fetchGitHubToken = fetchFlags.String("github-token", "", "GitHub token")
Expand Down Expand Up @@ -125,12 +121,6 @@ func Main(args []string) error {
// restore-json
},
Exec: func(context.Context, []string) error { return flag.ErrHelp },
}, {
Name: "run",
ShortHelp: "sync target urls and draw a graph",
ShortUsage: "run [flags] [url...]",
Exec: execRun,
FlagSet: runFlags,
}, {
Name: "server",
ShortHelp: "start a depviz server with depviz API",
Expand All @@ -144,7 +134,7 @@ func Main(args []string) error {
{Name: "json", Exec: execGenJSON, ShortHelp: "generate JSON output"},
{Name: "csv", Exec: execGenCSV, ShortHelp: "generate CSV output"},
},
FlagSet: runFlags,
FlagSet: genFlags,
}, {
Name: "fetch",
ShortHelp: "fetch data from providers",
Expand Down Expand Up @@ -263,34 +253,6 @@ func execStoreInfo(ctx context.Context, args []string) error {
return dvcore.StoreInfo(store)
}

func execRun(ctx context.Context, args []string) error {
if err := globalPreRun(); err != nil {
return err
}

store, err := storeFromArgs()
if err != nil {
return fmt.Errorf("init store: %w", err)
}

opts := dvcore.RunOpts{
Logger: logger,
Schema: schemaConfig,
Vertical: *runVertical,
NoPert: *runNoPert,
NoGraph: *runNoGraph,
// NoPull: *runNoPull,
Format: *runFormat,
// Resync: *runResync,
// GitHubToken: *runGitHubToken,
ShowClosed: *runShowClosed,
HideIsolated: *runHideIsolated,
HidePRs: *runHidePRs,
HideExternalDeps: *runHideExternalDeps,
}
return dvcore.Run(store, args, opts)
}

func execServer(ctx context.Context, args []string) error {
if err := globalPreRun(); err != nil {
return err
Expand Down Expand Up @@ -398,24 +360,20 @@ func execGenJSON(ctx context.Context, args []string) error {
return fmt.Errorf("init store: %w", err)
}

opts := dvcore.RunOpts{
Logger: logger,
Schema: schemaConfig,
Vertical: *runVertical,
NoPert: *runNoPert,
NoGraph: *runNoGraph,
// NoPull: *runNoPull,
Format: *runFormat,
// Resync: *runResync,
// GitHubToken: *runGitHubToken,
ShowClosed: *runShowClosed,
HideIsolated: *runHideIsolated,
HidePRs: *runHidePRs,
HideExternalDeps: *runHideExternalDeps,
}
opts.Format = "json"

return dvcore.Run(store, args, opts)
opts := dvcore.GenOpts{
Logger: logger,
Schema: schemaConfig,
Vertical: *genVertical,
NoPert: *genNoPert,
NoGraph: *genNoGraph,
ShowClosed: *genShowClosed,
HideIsolated: *genHideIsolated,
HidePRs: *genHidePRs,
HideExternalDeps: *genHideExternalDeps,
Format: "json",
}

return dvcore.Gen(store, args, opts)
}

func execGenCSV(ctx context.Context, args []string) error {
Expand All @@ -428,24 +386,20 @@ func execGenCSV(ctx context.Context, args []string) error {
return fmt.Errorf("init store: %w", err)
}

opts := dvcore.RunOpts{
Logger: logger,
Schema: schemaConfig,
Vertical: *runVertical,
NoPert: *runNoPert,
NoGraph: *runNoGraph,
// NoPull: *runNoPull,
Format: *runFormat,
// Resync: *runResync,
// GitHubToken: *runGitHubToken,
ShowClosed: *runShowClosed,
HideIsolated: *runHideIsolated,
HidePRs: *runHidePRs,
HideExternalDeps: *runHideExternalDeps,
}
opts.Format = "csv"

return dvcore.Run(store, args, opts)
opts := dvcore.GenOpts{
Logger: logger,
Schema: schemaConfig,
Vertical: *genVertical,
NoPert: *genNoPert,
NoGraph: *genNoGraph,
ShowClosed: *genShowClosed,
HideIsolated: *genHideIsolated,
HidePRs: *genHidePRs,
HideExternalDeps: *genHideExternalDeps,
Format: "csv",
}

return dvcore.Gen(store, args, opts)
}

func execFetch(ctx context.Context, args []string) error {
Expand Down
5 changes: 2 additions & 3 deletions internal/dvcore/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package dvcore
import (
"fmt"

"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/schema"
"go.uber.org/zap"
"moul.io/depviz/v3/internal/dvparser"
"moul.io/multipmuri"

"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/schema"
)

type FetchOpts struct {
Expand Down
30 changes: 13 additions & 17 deletions internal/dvcore/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"os"
"sync"

"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/schema"
"go.uber.org/zap"
yaml "gopkg.in/yaml.v2"
"moul.io/depviz/v3/internal/dvmodel"
Expand All @@ -17,13 +20,9 @@ import (
"moul.io/graphman"
"moul.io/graphman/viz"
"moul.io/multipmuri"

"github.com/cayleygraph/cayley"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/schema"
)

type RunOpts struct {
type GenOpts struct {
// global

// NoPull bool
Expand All @@ -50,11 +49,11 @@ type RunOpts struct {
HideExternalDeps bool
}

func Run(h *cayley.Handle, args []string, opts RunOpts) error {
func Gen(h *cayley.Handle, args []string, opts GenOpts) error {
if opts.Logger == nil {
opts.Logger = zap.NewNop()
}
opts.Logger.Debug("Run called", zap.Strings("args", args), zap.Any("opts", opts))
opts.Logger.Debug("Gen called", zap.Strings("args", args), zap.Any("opts", opts))

// FIXME: support the world

Expand Down Expand Up @@ -82,9 +81,9 @@ func Run(h *cayley.Handle, args []string, opts RunOpts) error {

switch opts.Format {
case "json":
return genJson(tasks)
return genJSON(tasks)
case "csv":
return genCsv(tasks)
return genCSV(tasks)
case "graphman-pert":
out, err := yaml.Marshal(pertConfig)
if err != nil {
Expand Down Expand Up @@ -245,7 +244,7 @@ func saveBatches(h *cayley.Handle, schema *schema.Config, batches []dvmodel.Batc
return nil
}

func graphmanPertConfig(tasks []dvmodel.Task, opts RunOpts) *graphman.PertConfig {
func graphmanPertConfig(tasks []dvmodel.Task, opts GenOpts) *graphman.PertConfig {
opts.Logger.Debug("graphTargets", zap.Int("tasks", len(tasks)), zap.Any("opts", opts))

// initialize graph config
Expand Down Expand Up @@ -297,7 +296,7 @@ func graphmanPertConfig(tasks []dvmodel.Task, opts RunOpts) *graphman.PertConfig
return &config
}

func genJson(tasks []dvmodel.Task) error {
func genJSON(tasks []dvmodel.Task) error {
out, err := json.MarshalIndent(tasks, "", " ")
if err != nil {
return err
Expand All @@ -306,14 +305,11 @@ func genJson(tasks []dvmodel.Task) error {
return nil
}

func genCsv(tasks []dvmodel.Task) error {
var csvTasks [][]string
func genCSV(tasks []dvmodel.Task) error {
csvTasks := make([][]string, len(tasks))
for _, task := range tasks {
csvTasks = append(csvTasks, task.MarshalCSV())
}
w := csv.NewWriter(os.Stdout)
if err := w.WriteAll(csvTasks); err != nil {
return err
}
return nil
return w.WriteAll(csvTasks)
}

0 comments on commit 2bc4491

Please sign in to comment.