From f33d4c6291c12e1076b9e4abf0771858de3d33a0 Mon Sep 17 00:00:00 2001 From: franklin Date: Mon, 20 Mar 2023 11:44:56 +0100 Subject: [PATCH] feat: implement posh changs --- arbitary/lint/command.go | 113 +++++++-------- arbitary/open/command.go | 65 ++++----- derailed/k9s/command.go | 56 +++----- dreadl0ck/zeus/command.go | 2 - etcd-io/etcd/command.go | 28 ++-- foomo/gocontentful/command.go | 46 +++--- foomo/gotsrpc/command.go | 49 +++---- foomo/squadron/command.go | 216 +++++++++++++++-------------- go.mod | 2 +- go.sum | 4 +- golang/command.go | 81 +++++------ google/gcloud/command.go | 30 ++-- helm/helm/command.go | 158 ++++++++++----------- jlesquembre/kubeprompt/command.go | 35 ++--- jondot/hygen/command.go | 42 +++--- mjmlio/mjml/command.go | 48 +++---- onepassword/command.go | 44 +++--- pivotal/licensefinder/command.go | 33 ++--- stern/stern/command.go | 88 ++++++------ webdriverio/webdriverio/command.go | 82 +++++------ yarnpkg/yarn/command.go | 47 +++---- 21 files changed, 545 insertions(+), 724 deletions(-) diff --git a/arbitary/lint/command.go b/arbitary/lint/command.go index 1c5c55a..aad0a89 100644 --- a/arbitary/lint/command.go +++ b/arbitary/lint/command.go @@ -23,7 +23,7 @@ type ( l log.Logger name string cache cache.Namespace - commandTree *tree.Root + commandTree tree.Root } CommandOption func(command *Command) ) @@ -40,15 +40,15 @@ func CommandWithName(v string) CommandOption { func CommandWithGo() CommandOption { return func(o *Command) { - o.commandTree.Nodes = append(o.commandTree.Nodes, &tree.Node{ + o.commandTree.Node().Nodes = append(o.commandTree.Node().Nodes, &tree.Node{ Name: "go", - Description: "run golangci-lint", + Description: "Run golangci-lint", Args: tree.Args{o.pathArg("go.mod")}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("fix", false, "run quick fix") - fs.String("timeout", "1m", "max excution timeout") - fs.String("out-format", "github-actions", "output format") - fs.Int("concurrency", 1, "num of concurrent processes") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("fix", false, "run quick fix") + fs.Default().String("timeout", "1m", "max excution timeout") + fs.Default().String("out-format", "github-actions", "output format") + fs.Default().Int("concurrency", 1, "num of concurrent processes") return nil }, Execute: func(ctx context.Context, r *readline.Readline) error { @@ -58,7 +58,6 @@ func CommandWithGo() CommandOption { if out, err := shell.New(ctx, o.l, "golangci-lint", "run"). Args("--path-prefix", dir). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Dir(dir). Output(); err != nil { @@ -73,12 +72,12 @@ func CommandWithGo() CommandOption { func CommandWithTSC() CommandOption { return func(o *Command) { - o.commandTree.Nodes = append(o.commandTree.Nodes, &tree.Node{ + o.commandTree.Node().Nodes = append(o.commandTree.Node().Nodes, &tree.Node{ Name: "tsc", - Description: "run tsc", + Description: "Run tsc", Args: tree.Args{o.pathArg("tsconfig.json")}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("fix", false, "run quick fix") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("fix", false, "run quick fix") return nil }, Execute: func(ctx context.Context, r *readline.Readline) error { @@ -88,7 +87,6 @@ func CommandWithTSC() CommandOption { if out, err := shell.New(ctx, o.l, "tsc", "--noEmit"). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Dir(dir). Output(); err != nil { @@ -103,12 +101,12 @@ func CommandWithTSC() CommandOption { func CommandWithHelm() CommandOption { return func(o *Command) { - o.commandTree.Nodes = append(o.commandTree.Nodes, &tree.Node{ + o.commandTree.Node().Nodes = append(o.commandTree.Node().Nodes, &tree.Node{ Name: "helm", - Description: "run helm lint", + Description: "Run helm lint", Args: tree.Args{o.pathArg("Chart.yaml")}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("fix", false, "run quick fix") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("fix", false, "run quick fix") return nil }, Execute: func(ctx context.Context, r *readline.Readline) error { @@ -117,7 +115,6 @@ func CommandWithHelm() CommandOption { o.l.Info("└ " + dir) if out, err := shell.New(ctx, o.l, "helm", "lint", dir). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Output(); err != nil { return errors.Wrap(err, string(out)) @@ -131,13 +128,13 @@ func CommandWithHelm() CommandOption { func CommandWithESLint() CommandOption { return func(o *Command) { - o.commandTree.Nodes = append(o.commandTree.Nodes, &tree.Node{ + o.commandTree.Node().Nodes = append(o.commandTree.Node().Nodes, &tree.Node{ Name: "eslint", - Description: "run eslint", + Description: "Run eslint", Args: tree.Args{o.pathArg("package.json")}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("fix", false, "run quick fix") - fs.Bool("cache", false, "use cache") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("fix", false, "run quick fix") + fs.Default().Bool("cache", false, "use cache") return nil }, Execute: func(ctx context.Context, r *readline.Readline) error { @@ -146,7 +143,6 @@ func CommandWithESLint() CommandOption { o.l.Info("└ " + dir) if out, err := shell.New(ctx, o.l, "eslint", "--quiet", "."). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Dir(dir). Output(); err != nil { @@ -161,13 +157,12 @@ func CommandWithESLint() CommandOption { func CommandWithGherkin() CommandOption { return func(o *Command) { - o.commandTree.Nodes = append(o.commandTree.Nodes, &tree.Node{ + o.commandTree.Node().Nodes = append(o.commandTree.Node().Nodes, &tree.Node{ Name: "gherkin", - Description: "run gherkin lint", + Description: "Run gherkin lint", Args: tree.Args{o.pathArg("wdio.conf.ts")}, - - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("fix", false, "run quick fix") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("fix", false, "run quick fix") return nil }, Execute: func(ctx context.Context, r *readline.Readline) error { @@ -177,7 +172,6 @@ func CommandWithGherkin() CommandOption { if out, err := shell.New(ctx, o.l, "gherkin-lint", dir). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Output(); err != nil { return errors.Wrap(err, string(out)) @@ -191,12 +185,12 @@ func CommandWithGherkin() CommandOption { func CommandWithTerraform() CommandOption { return func(o *Command) { - o.commandTree.Nodes = append(o.commandTree.Nodes, &tree.Node{ + o.commandTree.Node().Nodes = append(o.commandTree.Node().Nodes, &tree.Node{ Name: "terraform", - Description: "run tflint lint", + Description: "Run tflint lint", Args: tree.Args{o.pathArg("main.tf")}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("fix", false, "run quick fix") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("fix", false, "run quick fix") return nil }, Execute: func(ctx context.Context, r *readline.Readline) error { @@ -207,7 +201,6 @@ func CommandWithTerraform() CommandOption { if out, err := shell.New(ctx, o.l, "tflint"). Dir(dir). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Output(); err != nil { return errors.Wrap(err, string(out)) @@ -221,16 +214,16 @@ func CommandWithTerraform() CommandOption { func CommandWithTerrascan() CommandOption { return func(o *Command) { - o.commandTree.Nodes = append(o.commandTree.Nodes, &tree.Node{ + o.commandTree.Node().Nodes = append(o.commandTree.Node().Nodes, &tree.Node{ Name: "terrascan", - Description: "run terrascan", + Description: "Run terrascan", Nodes: tree.Nodes{ { Name: "helm", - Description: "run terrascan helm", + Description: "Run terrascan helm", Args: tree.Args{o.pathArg("Chart.yaml")}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("fix", false, "run quick fix") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("fix", false, "run quick fix") return nil }, Execute: func(ctx context.Context, r *readline.Readline) error { @@ -241,7 +234,6 @@ func CommandWithTerrascan() CommandOption { Args("--iac-dir", dir). Args("--iac-type", "docker"). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Output(); err != nil { return errors.Wrap(err, string(out)) @@ -252,10 +244,10 @@ func CommandWithTerrascan() CommandOption { }, { Name: "terraform", - Description: "run terrascan terraform", + Description: "Run terrascan terraform", Args: tree.Args{o.pathArg("main.tf")}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("fix", false, "run quick fix") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("fix", false, "run quick fix") return nil }, Execute: func(ctx context.Context, r *readline.Readline) error { @@ -266,7 +258,6 @@ func CommandWithTerrascan() CommandOption { Args("--iac-dir", dir). Args("--iac-type", "docker"). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Output(); err != nil { return errors.Wrap(err, string(out)) @@ -277,10 +268,10 @@ func CommandWithTerrascan() CommandOption { }, { Name: "docker", - Description: "run terrascan docker", + Description: "Run terrascan docker", Args: tree.Args{o.pathArg("Dockerfile")}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("fix", false, "run quick fix") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("fix", false, "run quick fix") return nil }, Execute: func(ctx context.Context, r *readline.Readline) error { @@ -291,7 +282,6 @@ func CommandWithTerrascan() CommandOption { Args("--iac-dir", dir). Args("--iac-type", "docker"). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Output(); err != nil { return errors.Wrap(err, string(out)) @@ -314,16 +304,16 @@ func NewCommand(l log.Logger, c cache.Cache, opts ...CommandOption) *Command { l: l.Named("lint"), name: "lint", cache: c.Get("lint"), - commandTree: &tree.Root{ - Description: "lint your code", - }, + commandTree: tree.New(&tree.Node{ + Description: "Lint your code", + }), } for _, opt := range opts { if opt != nil { opt(inst) } } - inst.commandTree.Name = inst.name + inst.commandTree.Node().Name = inst.name return inst } @@ -333,11 +323,11 @@ func NewCommand(l log.Logger, c cache.Cache, opts ...CommandOption) *Command { // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -349,14 +339,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Lint your code. - -Usage: - lint [linter] - -Examples: - lint go -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -382,7 +365,7 @@ func (c *Command) pathArg(filename string) *tree.Arg { return &tree.Arg{ Name: "path", Optional: true, - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(c.paths(ctx, filename)) }, } diff --git a/arbitary/open/command.go b/arbitary/open/command.go index 457c01c..8080d49 100644 --- a/arbitary/open/command.go +++ b/arbitary/open/command.go @@ -21,7 +21,7 @@ type ( cfg Config name string configKey string - commandTree *tree.Root + commandTree tree.Root } CommandOption func(*Command) ) @@ -62,37 +62,35 @@ func NewCommand(l log.Logger, op *onepassword.OnePassword, opts ...CommandOption return nil, err } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: inst.name, Description: "Open an external url", - Node: &tree.Node{ - Args: tree.Args{ - { - Name: "router", - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { - var ret []goprompt.Suggest - for s, router := range inst.cfg { - ret = append(ret, goprompt.Suggest{Text: s, Description: router.Description}) - } - return ret - }, - }, - { - Name: "route", - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { - var ret []goprompt.Suggest - if value, ok := inst.cfg[r.Args().At(0)]; ok { - for s, route := range value.Routes { - ret = append(ret, goprompt.Suggest{Text: s, Description: route.Description}) - } - } - return ret - }, + Args: tree.Args{ + { + Name: "router", + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { + var ret []goprompt.Suggest + for s, router := range inst.cfg { + ret = append(ret, goprompt.Suggest{Text: s, Description: router.Description}) + } + return ret + }, + }, + { + Name: "route", + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { + var ret []goprompt.Suggest + if value, ok := inst.cfg[r.Args().At(0)]; ok { + for s, route := range value.Routes { + ret = append(ret, goprompt.Suggest{Text: s, Description: route.Description}) + } + } + return ret }, }, - Execute: inst.execute, }, - } + Execute: inst.execute, + }) return inst, nil } @@ -102,11 +100,11 @@ func NewCommand(l log.Logger, op *onepassword.OnePassword, opts ...CommandOption // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -135,14 +133,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Connect to a open. - -Usage: - open [router] [route] - -Examples: - open grafana home -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ diff --git a/derailed/k9s/command.go b/derailed/k9s/command.go index 6558a3b..3d001e0 100644 --- a/derailed/k9s/command.go +++ b/derailed/k9s/command.go @@ -19,7 +19,7 @@ type ( l log.Logger kubectl *kubectl.Kubectl squadron *squadron.Squadron - commandTree *tree.Root + commandTree tree.Root namespaceFn NamespaceFn } NamespaceFn func(cluster, fleet, squadron string) string @@ -58,28 +58,25 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl, squadron *squadron.Squad opt(inst) } } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: "k9s", - Description: "open the k9s dashboard", - Node: &tree.Node{ - Args: tree.Args{ - { - Name: "cluster", - Suggest: inst.completeClusters, - }, - { - Name: "fleet", - Suggest: inst.completeFleets, - }, - { - Name: "squadron", - Suggest: inst.completeSquadrons, - }, + Description: "Open the k9s dashboard", + Args: tree.Args{ + { + Name: "cluster", + Suggest: inst.completeClusters, + }, + { + Name: "fleet", + Suggest: inst.completeFleets, + }, + { + Name: "squadron", + Suggest: inst.completeSquadrons, }, - Execute: inst.execute, }, - } - + Execute: inst.execute, + }) return inst } @@ -88,11 +85,11 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl, squadron *squadron.Squad // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -104,14 +101,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Open the k9s dashboard. - -Usage: - k9s [cluster] [namespace] - -Examples: - k9s example-cluster my-namespace -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -126,18 +116,18 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { Run() } -func (c *Command) completeClusters(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { +func (c *Command) completeClusters(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(c.kubectl.Clusters()) } -func (c *Command) completeFleets(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { +func (c *Command) completeFleets(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { if cluster, ok := c.squadron.Cluster(r.Args().At(0)); ok { return suggests.List(cluster.Fleets) } return nil } -func (c *Command) completeSquadrons(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { +func (c *Command) completeSquadrons(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { if value, err := c.squadron.List(); err != nil { c.l.Debug(err.Error()) return nil diff --git a/dreadl0ck/zeus/command.go b/dreadl0ck/zeus/command.go index 4e5b5db..81f9a65 100644 --- a/dreadl0ck/zeus/command.go +++ b/dreadl0ck/zeus/command.go @@ -77,14 +77,12 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { c.l.Info("bootstrapping a new zeus:", dir) return shell.New(ctx, c.l, "zeus", "bootstrap"). Args(args...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Dir(path.Join(dir, "..")). Run() } else { return shell.New(ctx, c.l, "zeus", "-C", path.Dir(dir)). Args(args...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Run() } diff --git a/etcd-io/etcd/command.go b/etcd-io/etcd/command.go index c900e15..eb80391 100644 --- a/etcd-io/etcd/command.go +++ b/etcd-io/etcd/command.go @@ -21,7 +21,7 @@ import ( type Command struct { l log.Logger etcd *ETCD - commandTree *tree.Root + commandTree tree.Root } // ------------------------------------------------------------------------------------------------ @@ -36,7 +36,7 @@ func NewCommand(l log.Logger, etcd *ETCD, opts ...Option) *Command { pathArg := &tree.Arg{ Name: "path", - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []prompt2.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []prompt2.Suggest { if value, ok := inst.etcd.cfg.Cluster(r.Args().At(0)); ok { return suggests.List(value.Paths) } @@ -44,9 +44,9 @@ func NewCommand(l log.Logger, etcd *ETCD, opts ...Option) *Command { }, } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: "etcd", - Description: "read and write to etcd", + Description: "Read and write to etcd", Nodes: tree.Nodes{ { Name: "cluster", @@ -73,7 +73,7 @@ func NewCommand(l log.Logger, etcd *ETCD, opts ...Option) *Command { }, }, }, - } + }) return inst } @@ -83,11 +83,11 @@ func NewCommand(l log.Logger, etcd *ETCD, opts ...Option) *Command { // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -99,19 +99,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Read and write to etcd. - -Usage: - etcd [cluster] [cmd] - -Available commands: - get [path] Prints the value - edit [path] Edit the given value - -Examples: - etcd example-cluster get config.yaml - etcd example-cluster edit config.yaml -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ diff --git a/foomo/gocontentful/command.go b/foomo/gocontentful/command.go index 065193b..d7d1e3f 100644 --- a/foomo/gocontentful/command.go +++ b/foomo/gocontentful/command.go @@ -25,7 +25,7 @@ type ( op *onepassword.OnePassword name string cache cache.Namespace - commandTree *tree.Root + commandTree tree.Root } CommandOption func(*Command) ) @@ -56,26 +56,24 @@ func NewCommand(l log.Logger, cache cache.Cache, op *onepassword.OnePassword, op opt(inst) } } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: inst.name, - Description: "run gocontentful", - Node: &tree.Node{ - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("debug", false, "show debug output") - return nil - }, - Args: tree.Args{ - { - Name: "path", - Optional: true, - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { - return suggests.List(inst.paths(ctx)) - }, + Description: "Run gocontentful", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("debug", false, "show debug output") + return nil + }, + Args: tree.Args{ + { + Name: "path", + Optional: true, + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { + return suggests.List(inst.paths(ctx)) }, }, - Execute: inst.execute, }, - } + Execute: inst.execute, + }) return inst } @@ -85,11 +83,11 @@ func NewCommand(l log.Logger, cache cache.Cache, op *onepassword.OnePassword, op // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -116,14 +114,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Generate gocontentful files. - -Usage: - gocontentful - -Examples: - gocontentful ./path/gocontentful.yml -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -157,7 +148,6 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { if err := shell.New(ctx, c.l, "gocontentful", "-spaceid", cfg.SpaceID, "-cmakey", cfg.CMAKey, "-contenttypes", strings.Join(cfg.ContentTypes, ","), dir). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Run(); err != nil { return err diff --git a/foomo/gotsrpc/command.go b/foomo/gotsrpc/command.go index 63274de..6814d59 100644 --- a/foomo/gotsrpc/command.go +++ b/foomo/gotsrpc/command.go @@ -19,7 +19,7 @@ import ( type Command struct { l log.Logger cache cache.Namespace - commandTree *tree.Root + commandTree tree.Root } // ------------------------------------------------------------------------------------------------ @@ -31,23 +31,21 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { l: l.Named("gotsrpc"), cache: cache.Get("gotsrpc"), } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: "gotsrpc", - Description: "run gotsrpc", - Node: &tree.Node{ - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("debug", false, "show debug output") - return nil - }, - Args: tree.Args{ - { - Name: "path", - Suggest: inst.completePaths, - }, - }, - Execute: inst.execute, + Description: "Run gotsrpc", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("debug", false, "show debug output") + return nil }, - } + Args: tree.Args{ + { + Name: "path", + Suggest: inst.completePaths, + }, + }, + Execute: inst.execute, + }) return inst } @@ -57,11 +55,11 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -88,18 +86,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Generate gotsrpc files. - -Usage: - gotsrpc - -Available options: - debug - skipgotsrpc - -Examples: - gotsrpc ./path/gotsrpc.yml -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -131,7 +118,7 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { return nil } -func (c *Command) completePaths(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { +func (c *Command) completePaths(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(c.paths(ctx)) } diff --git a/foomo/squadron/command.go b/foomo/squadron/command.go index 4042135..138b297 100644 --- a/foomo/squadron/command.go +++ b/foomo/squadron/command.go @@ -30,7 +30,7 @@ type ( slackChannelID string kubectl *kubectl.Kubectl squadron *Squadron - commandTree *tree.Root + commandTree tree.Root namespaceFn NamespaceFn } NamespaceFn func(cluster, fleet, squadron string) string @@ -88,7 +88,7 @@ func NewCommand(l log.Logger, squadron *Squadron, kubectl *kubectl.Kubectl, op * Name: "unit", Repeat: true, Optional: true, - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { if value, err := inst.squadron.ListUnits(ctx, r.Args().At(2), r.Args().At(0), r.Args().At(1), true); err != nil { return nil } else { @@ -96,10 +96,15 @@ func NewCommand(l log.Logger, squadron *Squadron, kubectl *kubectl.Kubectl, op * } }, } - commonFlags := func(fs *readline.FlagSet) { - fs.Bool("no-override", false, "ignore override files") - fs.Bool("verbose", inst.l.IsLevel(log.LevelDebug), "set verbose level") - fs.Bool("debug", inst.l.IsLevel(log.LevelTrace), "set debug level") + slackFlag := func(fs *readline.FlagSets) { + if inst.slack != nil { + fs.Internal().Bool("slack", false, "send slack notification") + } + } + commonFlags := func(fs *readline.FlagSets) { + fs.Internal().Bool("no-override", false, "ignore override files") + fs.Default().Bool("verbose", inst.l.IsLevel(log.LevelDebug), "set verbose level") + fs.Default().Bool("debug", inst.l.IsLevel(log.LevelTrace), "set debug level") } clusterValues := func(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -112,9 +117,9 @@ func NewCommand(l log.Logger, squadron *Squadron, kubectl *kubectl.Kubectl, op * return suggests.List(ret) } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: "squadron", - Description: "manage your squadron", + Description: "Manage your squadron", Nodes: tree.Nodes{ { Name: "cluster", @@ -143,78 +148,73 @@ func NewCommand(l log.Logger, squadron *Squadron, kubectl *kubectl.Kubectl, op * Nodes: tree.Nodes{ { Name: "up", - Description: "installs a squadron chart", + Description: "Installs a squadron chart", Args: tree.Args{unitsArg}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + slackFlag(fs) commonFlags(fs) - fs.Bool("diff", false, "show diff") - fs.Bool("push", false, "push image") - fs.Bool("build", false, "build image") - fs.String("tag", "", "image tag") - fs.Int64("parallel", 0, "number of parallel processes") - if inst.slack != nil { - fs.Bool("slack", false, "send slack notification") - } - return nil - }, - PassThroughFlags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("create-namespace", false, "create namespace if not exist") + fs.Default().Bool("diff", false, "show diff") + fs.Default().Bool("push", false, "push image") + fs.Default().Bool("build", false, "build image") + fs.Internal().Int64("parallel", 0, "number of parallel processes") + fs.Internal().String("tag", "", "image tag") + fs.Internal().Bool("create-namespace", false, "create namespace if not exist") return nil }, Execute: inst.up, }, { Name: "list", - Description: "list squadron units", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "List squadron units", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { commonFlags(fs) - fs.Bool("prefix-squadron", false, "prefix unit names with squadron") + fs.Default().Bool("prefix-squadron", false, "prefix unit names with squadron") return nil }, Execute: inst.list, }, { Name: "down", - Description: "uninstalls the squadron chart", + Description: "Uninstalls the squadron chart", Args: tree.Args{unitsArg}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { commonFlags(fs) - if inst.slack != nil { - fs.Bool("slack", false, "send slack notification") - } + slackFlag(fs) return nil }, Execute: inst.down, }, { Name: "push", - Description: "push squadron units", + Description: "Push squadron units", Args: tree.Args{unitsArg}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { commonFlags(fs) - fs.Bool("build", false, "build image") + fs.Default().Bool("build", false, "build image") + fs.Internal().String("tag", "", "image tag") + fs.Internal().Int64("parallel", 0, "number of parallel processes") return nil }, Execute: inst.push, }, { Name: "build", - Description: "build or rebuild squadron units", + Description: "Build or rebuild squadron units", Args: tree.Args{unitsArg}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { commonFlags(fs) - fs.Bool("push", false, "push image") - fs.String("tag", "", "image tag") - fs.Int64("parallel", 0, "number of parallel processes") + fs.Default().Bool("push", false, "push image") + fs.Internal().String("tag", "", "image tag") + fs.Internal().Int64("parallel", 0, "number of parallel processes") return nil }, Execute: inst.build, }, { Name: "status", - Description: "display the status of the units", + Description: "Display the status of the units", Args: tree.Args{unitsArg}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { commonFlags(fs) return nil }, @@ -222,34 +222,32 @@ func NewCommand(l log.Logger, squadron *Squadron, kubectl *kubectl.Kubectl, op * }, { Name: "config", - Description: "view generated squadron config", + Description: "View generated squadron config", Args: tree.Args{unitsArg}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { commonFlags(fs) - fs.Bool("no-render", false, "push image") + fs.Default().Bool("no-render", false, "push image") return nil }, Execute: inst.config, }, { Name: "rollback", - Description: "roll back the squadron chart", + Description: "Roll back the squadron chart", Args: tree.Args{unitsArg}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { commonFlags(fs) - fs.String("revision", "", "revision number to rollback to") - if inst.slack != nil { - fs.Bool("slack", false, "send slack notification") - } + fs.Default().String("revision", "", "revision number to rollback to") + slackFlag(fs) return nil }, Execute: inst.rollback, }, { Name: "generate", - Description: "generate and view the squadron chart", + Description: "Generate and view the squadron chart", Args: tree.Args{unitsArg}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { commonFlags(fs) return nil }, @@ -257,10 +255,11 @@ func NewCommand(l log.Logger, squadron *Squadron, kubectl *kubectl.Kubectl, op * }, { Name: "template", - Description: "render chart templates locally and display the output", + Description: "Render chart templates locally and display the output", Args: tree.Args{unitsArg}, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { commonFlags(fs) + fs.Internal().String("tag", "", "image tag") return nil }, Execute: inst.template, @@ -272,7 +271,7 @@ func NewCommand(l log.Logger, squadron *Squadron, kubectl *kubectl.Kubectl, op * }, }, }, - } + }) return inst } @@ -282,11 +281,11 @@ func NewCommand(l log.Logger, squadron *Squadron, kubectl *kubectl.Kubectl, op * // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -298,18 +297,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Manage your squadron. - -Usage: - squadron [cluster] [fleet] [squadron] [cmd] - -Available Commands: - up - down - -Examples: - k9s example-cluster my-namespace -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -357,54 +345,68 @@ func (c *Command) template(ctx context.Context, r *readline.Readline) error { } func (c *Command) execute(ctx context.Context, r *readline.Readline) error { - flags := r.Flags() + var env []string + var squadrons []string + ifs := r.FlagSets().Internal() + passFlags := []string{"--"} cluster, fleet, squadron, cmd, units := r.Args()[0], r.Args()[1], r.Args()[2], r.Args()[3], r.Args()[4:] - if c.op != nil { - if ok, _ := c.op.Session(); !ok { - c.l.Info("missing 1password session, please login") - if err := c.op.SignIn(ctx); err != nil { + // retrieve flags + tag, _ := ifs.GetString("tag") + noOverride := log.MustGet(ifs.GetBool("no-override"))(c.l) + + { // handle 1password + if c.op != nil { + if ok, _ := c.op.Session(); !ok { + c.l.Info("missing 1password session, please login") + if err := c.op.SignIn(ctx); err != nil { + return err + } + } + } + } + + { // handle pass through flags + if slices.Contains([]string{"up", "template"}, cmd) { + passFlags = append(passFlags, "--set", fmt.Sprintf("fleet=%v", fleet)) + } + if slices.Contains([]string{"up"}, cmd) { + if log.MustGet(ifs.GetBool("create-namespace"))(c.l) { + passFlags = append(passFlags, "--create-namespace") + } + } + } + + { // handle env + env = append(env, fmt.Sprintf("FLEET=%s", fleet)) + if tag != "" { + env = append(env, fmt.Sprintf("TAG=%q", tag)) + } + env = append(env, c.kubectl.Cluster(cluster).Env()) + } + + { // handle squadrons + if squadron == All { + if value, err := c.squadron.List(); err == nil { + squadrons = value + } + } else { + squadrons = []string{squadron} + } + } + + { // handle slack + if ok, _ := ifs.GetBool("slack"); ok { + if err := c.notify(ctx, cmd, cluster, fleet, squadron, tag, units); err != nil { return err } } } - passFlags := r.PassThroughFlags() - if slices.Contains([]string{"up", "template"}, cmd) { - if len(passFlags) == 0 { - passFlags = append(passFlags, "--") - } - passFlags = append(passFlags, "--set", fmt.Sprintf("fleet=%v", fleet)) - } - - var env []string - env = append(env, fmt.Sprintf("FLEET=%s", fleet)) - if value := r.FlagSet().GetString("tag"); value != "" { - env = append(env, fmt.Sprintf("TAG=%q", value)) - flags = flags.Splice(flags.IndexOf("--tag"), 2) - } - env = append(env, c.kubectl.Cluster(cluster).Env()) - - var squadrons []string - if squadron == All { - if value, err := c.squadron.List(); err == nil { - squadrons = value - } - } else { - squadrons = []string{squadron} - } - - if c.slack != nil && r.FlagSet().GetBool("slack") { - flags = flags.Splice(flags.IndexOf("--slack"), 1) - if err := c.notify(ctx, cmd, cluster, fleet, squadron, r.FlagSet().GetString("tag"), units); err != nil { - return err - } - } - for _, s := range squadrons { env := append(env, fmt.Sprintf("SQUADRON=%s", s)) - flags := flags - files := strings.Join(c.squadron.GetFiles(s, cluster, fleet, !r.FlagSet().GetBool("no-override")), ",") + flags := r.FlagSets().Default().Args() + files := strings.Join(c.squadron.GetFiles(s, cluster, fleet, !noOverride), ",") if slices.Contains([]string{"up", "down", "rollback", "status", "template"}, cmd) { flags = append(flags, "--namespace", c.namespaceFn(cluster, fleet, s)) } diff --git a/go.mod b/go.mod index 1a2469b..2a32a25 100644 --- a/go.mod +++ b/go.mod @@ -8,7 +8,7 @@ require ( github.com/1Password/connect-sdk-go v1.5.0 github.com/c-bata/go-prompt v0.2.6 github.com/cloudrecipes/packagejson v1.0.0 - github.com/foomo/posh v0.3.1-0.20230220152511-db05bf278428 + github.com/foomo/posh v0.3.1-0.20230320104124-535c54a232e7 github.com/joho/godotenv v1.5.1 github.com/pkg/errors v0.9.1 github.com/pterm/pterm v0.12.54 diff --git a/go.sum b/go.sum index 46d0baa..d82e59d 100644 --- a/go.sum +++ b/go.sum @@ -86,8 +86,8 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k= -github.com/foomo/posh v0.3.1-0.20230220152511-db05bf278428 h1:uIpD2sUh6FDnmuo1CI+737irVZiCKK80wNNb5AItEac= -github.com/foomo/posh v0.3.1-0.20230220152511-db05bf278428/go.mod h1:/vQBAKq672FEktZN3KbWc7Ofnhxz4r+I9oU+9/t14pI= +github.com/foomo/posh v0.3.1-0.20230320104124-535c54a232e7 h1:CE0FLumvmm5EzGLIbA7x15vklA5RmYYIfEN7bieRQBM= +github.com/foomo/posh v0.3.1-0.20230320104124-535c54a232e7/go.mod h1:/vQBAKq672FEktZN3KbWc7Ofnhxz4r+I9oU+9/t14pI= github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE= github.com/franklinkim/go-prompt v0.2.7-0.20210427061716-a8f4995d7aa5 h1:kXNtle4AoQnngdm+gwt4ku6Llbzw3EFHgZYpL618JaI= github.com/franklinkim/go-prompt v0.2.7-0.20210427061716-a8f4995d7aa5/go.mod h1:+syUfnvYJUO5A+6QMQYXAyzkxHMNlj9dH2LIeQfBSjc= diff --git a/golang/command.go b/golang/command.go index f304708..1c7d196 100644 --- a/golang/command.go +++ b/golang/command.go @@ -21,7 +21,7 @@ import ( type Command struct { l log.Logger cache cache.Namespace - commandTree *tree.Root + commandTree tree.Root } // ------------------------------------------------------------------------------------------------ @@ -37,7 +37,7 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { pathModArg := &tree.Arg{ Name: "path", Optional: true, - Suggest: func(ctx context.Context, p *tree.Root, r *readline.Readline) []prompt2.Suggest { + Suggest: func(ctx context.Context, p tree.Root, r *readline.Readline) []prompt2.Suggest { return inst.completePaths(ctx, "go.mod", true) }, } @@ -45,24 +45,24 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { pathGenerateArg := &tree.Arg{ Name: "path", Optional: true, - Suggest: func(ctx context.Context, p *tree.Root, r *readline.Readline) []prompt2.Suggest { + Suggest: func(ctx context.Context, p tree.Root, r *readline.Readline) []prompt2.Suggest { return inst.completePaths(ctx, "generate.go", false) }, } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: "go", - Description: "go related tasks", + Description: "Go related tasks", Nodes: tree.Nodes{ { Name: "mod", - Description: "run go mod commands", + Description: "Run go mod commands", Nodes: tree.Nodes{ { Name: "tidy", - Description: "run go mod tidy", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Int("parallel", 0, "number of parallel processes") + Description: "Run go mod tidy", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Internal().Int("parallel", 0, "number of parallel processes") return nil }, Args: []*tree.Arg{pathModArg}, @@ -70,9 +70,9 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { }, { Name: "download", - Description: "run go mod download", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Int("parallel", 0, "number of parallel processes") + Description: "Run go mod download", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Internal().Int("parallel", 0, "number of parallel processes") return nil }, Args: []*tree.Arg{pathModArg}, @@ -80,9 +80,9 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { }, { Name: "outdated", - Description: "show go mod outdated", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Int("parallel", 0, "number of parallel processes") + Description: "Show go mod outdated", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Internal().Int("parallel", 0, "number of parallel processes") return nil }, Args: []*tree.Arg{pathModArg}, @@ -92,16 +92,16 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { }, { Name: "work", - Description: "manage go.work file", + Description: "Manage go.work file", Nodes: tree.Nodes{ { Name: "init", - Description: "generate go.work file", + Description: "Generate go.work file", Execute: inst.workInit, }, { Name: "use", - Description: "add go.work entry", + Description: "Add go.work entry", Args: []*tree.Arg{ { Name: "path", @@ -114,9 +114,9 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { }, { Name: "generate", - Description: "run go mod commands", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Int("parallel", 0, "number of parallel processes") + Description: "Run go mod commands", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Internal().Int("parallel", 0, "number of parallel processes") return nil }, Args: []*tree.Arg{pathGenerateArg}, @@ -124,9 +124,9 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { }, { Name: "test", - Description: "run go test", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Int("parallel", 0, "number of parallel processes") + Description: "Run go test", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Internal().Int("parallel", 0, "number of parallel processes") return nil }, Args: []*tree.Arg{pathModArg}, @@ -134,16 +134,16 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { }, { Name: "build", - Description: "run go build", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Int("parallel", 0, "number of parallel processes") + Description: "Run go build", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Internal().Int("parallel", 0, "number of parallel processes") return nil }, Args: []*tree.Arg{pathModArg}, Execute: inst.build, }, }, - } + }) return inst } @@ -152,11 +152,11 @@ func NewCommand(l log.Logger, cache cache.Cache) *Command { // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -168,22 +168,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Looks for go.mod files and runs the given command. - -Usage: - go [command] - -Available commands: - mod [command] run go mod - generate run go generate - test run go test - build run go build - -SubCommands mod: - tidy - download - outdated -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -207,7 +192,6 @@ func (c *Command) build(ctx context.Context, r *readline.Readline) error { return shell.New(ctx, c.l, "go", "build", "-v", "./...", // TODO select test ). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Dir(value). Run() @@ -233,7 +217,6 @@ func (c *Command) test(ctx context.Context, r *readline.Readline) error { return shell.New(ctx, c.l, "go", "test", "-v", "./...", // TODO select test ). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Dir(value). Run() @@ -380,7 +363,7 @@ func (c *Command) paths(ctx context.Context, filename string, dir bool) []string func (c *Command) wg(ctx context.Context, r *readline.Readline) (context.Context, *errgroup.Group) { wg, ctx := errgroup.WithContext(ctx) - if value, err := r.FlagSet().GetInt("parallel"); err == nil && value != 0 { + if value, _ := r.FlagSets().Internal().GetInt("parallel"); value != 0 { wg.SetLimit(value) } else { wg.SetLimit(1) diff --git a/google/gcloud/command.go b/google/gcloud/command.go index 9200922..a89ae96 100644 --- a/google/gcloud/command.go +++ b/google/gcloud/command.go @@ -26,7 +26,7 @@ type ( op *onepassword.OnePassword gcloud *GCloud kubectl *kubectl.Kubectl - commandTree *tree.Root + commandTree tree.Root clusterNameFn ClusterNameFn } ClusterNameFn func(name string, cluster Cluster) string @@ -74,12 +74,10 @@ func NewCommand(l log.Logger, gcloud *GCloud, kubectl *kubectl.Kubectl, opts ... opt(inst) } } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: inst.name, Description: "Run google cloud sdk commands", - Node: &tree.Node{ - Execute: inst.execute, - }, + Execute: inst.execute, Nodes: tree.Nodes{ { Name: "login", @@ -88,7 +86,7 @@ func NewCommand(l log.Logger, gcloud *GCloud, kubectl *kubectl.Kubectl, opts ... { Name: "account", Optional: true, - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(inst.gcloud.cfg.AccountNames()) }, }, @@ -106,7 +104,7 @@ func NewCommand(l log.Logger, gcloud *GCloud, kubectl *kubectl.Kubectl, opts ... Args: tree.Args{ { Name: "cluster", - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(inst.gcloud.cfg.ClusterNames()) }, }, @@ -114,7 +112,7 @@ func NewCommand(l log.Logger, gcloud *GCloud, kubectl *kubectl.Kubectl, opts ... Execute: inst.containerClustersGetCredentials, }, }, - } + }) return inst } @@ -124,11 +122,11 @@ func NewCommand(l log.Logger, gcloud *GCloud, kubectl *kubectl.Kubectl, opts ... // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -140,16 +138,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Execute google-cloud-sdk commands. - -Usage: - gcloud [cmd] - -Available commands: - login Login into your google cloud account (optional service account) - docker Configure docker access - kubeconfig [cluster] Retrieve kube config for the given cluster -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -160,7 +149,6 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { return shell.New(ctx, c.l, "gcloud"). Args(r.Args()...). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Run() } diff --git a/helm/helm/command.go b/helm/helm/command.go index 15e3808..cc7f356 100644 --- a/helm/helm/command.go +++ b/helm/helm/command.go @@ -18,7 +18,7 @@ type ( l log.Logger name string kubectl *kubectl.Kubectl - commandTree *tree.Root + commandTree tree.Root } CommandOption func(*Command) ) @@ -43,33 +43,33 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { name: "helm", kubectl: kubectl, } - allFlags := func(fs *readline.FlagSet) { - fs.Bool("help", false, "help for helm") - fs.Bool("debug", false, "enable verbose output") - fs.String("namespace", "", "namespace scope for this request") - fs.Bool("all-namespaces", false, "all namespace scope for this request") - fs.Bool("create-namespace", false, "create the release namespace if not present") - fs.Bool("dependency-update", false, "update dependencies") - fs.Bool("dry-run", false, "assume aws profile") - fs.Bool("atomic", false, "delete installation on failure") - fs.Bool("wait", false, "wait until all resources a ready") + allFlags := func(fs *readline.FlagSets) { + fs.Default().Bool("help", false, "help for helm") + fs.Default().Bool("debug", false, "enable verbose output") + fs.Default().String("namespace", "", "namespace scope for this request") + fs.Default().Bool("all-namespaces", false, "all namespace scope for this request") + fs.Default().Bool("create-namespace", false, "create the release namespace if not present") + fs.Default().Bool("dependency-update", false, "update dependencies") + fs.Default().Bool("dry-run", false, "assume aws profile") + fs.Default().Bool("atomic", false, "delete installation on failure") + fs.Default().Bool("wait", false, "wait until all resources a ready") } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: inst.name, - Description: "run helm commands", + Description: "Run helm commands", Nodes: tree.Nodes{ { Name: "cluster", Values: func(ctx context.Context, r *readline.Readline) []goprompt.Suggest { return suggests.List(inst.kubectl.Clusters()) }, - Description: "cluster to run against", + Description: "Cluster to run against", Nodes: tree.Nodes{ { Name: "create", - Description: "create a new chart with the given name", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Create a new chart with the given name", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -77,8 +77,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "dependency", - Description: "manage a chart's dependencies", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Manage a chart's dependencies", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -86,8 +86,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "diff", - Description: "preview helm upgrade changes as a diff", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Preview helm upgrade changes as a diff", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -95,8 +95,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "env", - Description: "helm client environment information", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Helm client environment information", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -104,10 +104,10 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "get", - Description: "download extended information of a named release", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Download extended information of a named release", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) - fs.String("revision", "", "get the named release with revision") + fs.Default().String("revision", "", "get the named release with revision") return nil }, Args: tree.Args{ @@ -115,13 +115,13 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { Name: "value", Repeat: false, Optional: false, - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return []goprompt.Suggest{ - {Text: "all", Description: "download all information for a named release"}, - {Text: "hooks", Description: "download all hooks for a named release"}, - {Text: "manifest", Description: "download the manifest for a named release"}, - {Text: "notes", Description: "download the notes for a named release"}, - {Text: "values", Description: "download the values file for a named release"}, + {Text: "all", Description: "Download all information for a named release"}, + {Text: "hooks", Description: "Download all hooks for a named release"}, + {Text: "manifest", Description: "Download the manifest for a named release"}, + {Text: "notes", Description: "Download the notes for a named release"}, + {Text: "values", Description: "Download the values file for a named release"}, } }, }, @@ -131,7 +131,7 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { { Name: "help", Description: "Help about any command", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -139,8 +139,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "history", - Description: "fetch release history", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Fetch release history", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -148,8 +148,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "install", - Description: "install a chart", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Install a chart", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -157,8 +157,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "lint", - Description: "examine a chart for possible issues", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Examine a chart for possible issues", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -166,8 +166,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "list", - Description: "list releases", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "List releases", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -175,8 +175,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "package", - Description: "package a chart directory into a chart archive", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Package a chart directory into a chart archive", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -184,8 +184,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "plugin", - Description: "install, list, or uninstall Helm plugins", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Install, list, or uninstall Helm plugins", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -193,8 +193,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "pull", - Description: "download a chart from a repository and (optionally) unpack it in local directory", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Download a chart from a repository and (optionally) unpack it in local directory", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -202,8 +202,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "repo", - Description: "add, list, remove, update, and index chart repositories", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Add, list, remove, update, and index chart repositories", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -211,8 +211,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "rollback", - Description: "roll back a release to a previous revision", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Roll back a release to a previous revision", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -220,8 +220,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "search", - Description: "search for a keyword in charts", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Search for a keyword in charts", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -229,8 +229,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "show", - Description: "show information of a chart", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Show information of a chart", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -238,18 +238,18 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "status", - Description: "display the status of the named release", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Display the status of the named release", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) - fs.Bool("show-desc", false, "show description") + fs.Default().Bool("show-desc", false, "show description") return nil }, Execute: inst.execute, }, { Name: "template", - Description: "locally render templates", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Locally render templates", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -257,8 +257,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "test", - Description: "run tests for a release", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Run tests for a release", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -266,8 +266,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "uninstall", - Description: "uninstall a release", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Uninstall a release", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -275,8 +275,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "upgrade", - Description: "upgrade a release", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Upgrade a release", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -284,8 +284,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "verify", - Description: "verify that a chart at the given path has been signed and is valid", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Verify that a chart at the given path has been signed and is valid", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -293,8 +293,8 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, { Name: "version", - Description: "print the client version information", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { + Description: "Print the client version information", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { allFlags(fs) return nil }, @@ -303,7 +303,7 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { }, }, }, - } + }) return inst } @@ -313,11 +313,11 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -342,18 +342,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Generate helm files. - -Usage: - helm - -Available options: - debug - skiphelm - -Examples: - helm ./path/helm.yml -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -366,7 +355,6 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { return shell.New(ctx, c.l, "helm"). Args(args...). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Env(cluster.Env()). Run() diff --git a/jlesquembre/kubeprompt/command.go b/jlesquembre/kubeprompt/command.go index 0913d0e..dbadea3 100644 --- a/jlesquembre/kubeprompt/command.go +++ b/jlesquembre/kubeprompt/command.go @@ -15,7 +15,7 @@ import ( type Command struct { l log.Logger kubectl *kubectl.Kubectl - commandTree *tree.Root + commandTree tree.Root } // ------------------------------------------------------------------------------------------------ @@ -27,19 +27,17 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { l: l.Named("kubeprompt"), kubectl: kubectl, } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: "kubeprompt", - Description: "open the kubectl prompt", - Node: &tree.Node{ - Args: tree.Args{ - { - Name: "cluster", - Suggest: inst.completeClusters, - }, + Description: "Open the kubectl prompt", + Args: tree.Args{ + { + Name: "cluster", + Suggest: inst.completeClusters, }, - Execute: inst.execute, }, - } + Execute: inst.execute, + }) return inst } @@ -49,11 +47,11 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl) *Command { // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -65,14 +63,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Open interactive kubectl prompt. - -Usage: - kubeprompt [cluster] - -Examples: - kubeprompt example-cluster -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -86,6 +77,6 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { Run() } -func (c *Command) completeClusters(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { +func (c *Command) completeClusters(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(c.kubectl.Clusters()) } diff --git a/jondot/hygen/command.go b/jondot/hygen/command.go index 680b8d3..4a2c63c 100644 --- a/jondot/hygen/command.go +++ b/jondot/hygen/command.go @@ -23,7 +23,7 @@ type ( name string cache cache.Namespace templateDir string - commandTree *tree.Root + commandTree tree.Root } CommandOption func(*Command) ) @@ -60,34 +60,32 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command opt(inst) } } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: inst.name, - Description: "run hygen", - Node: &tree.Node{ - Args: tree.Args{ - { - Name: "path", - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { - return suggests.List(inst.paths(ctx)) - }, + Description: "Run hygen", + Args: tree.Args{ + { + Name: "path", + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { + return suggests.List(inst.paths(ctx)) }, }, }, Nodes: tree.Nodes{ { Name: "template", - Description: "render template", + Description: "Render template", Values: func(ctx context.Context, r *readline.Readline) []goprompt.Suggest { return suggests.List(inst.paths(ctx)) }, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Bool("dry", false, "Perform a dry run. Files will be generated but not saved") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Bool("dry", false, "Perform a dry run. Files will be generated but not saved") return nil }, Args: tree.Args{ { Name: "path", - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return nil }, }, @@ -95,7 +93,7 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command Execute: inst.execute, }, }, - } + }) return inst } @@ -105,11 +103,11 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -138,14 +136,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Generate hygen files. - -Usage: - hygen [template] [path] - -Examples: - hygen example ./target/path -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -156,7 +147,6 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { return shell.New(ctx, c.l, "hygen", "scaffold"). Args(r.Args()...). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Env(fmt.Sprintf("HYGEN_TMPLS=%s", path.Dir(c.templateDir))). Run() diff --git a/mjmlio/mjml/command.go b/mjmlio/mjml/command.go index a544804..dc31c28 100644 --- a/mjmlio/mjml/command.go +++ b/mjmlio/mjml/command.go @@ -23,7 +23,7 @@ type ( l log.Logger name string cache cache.Namespace - commandTree *tree.Root + commandTree tree.Root } CommandOption func(*Command) ) @@ -53,26 +53,24 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command opt(inst) } } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: inst.name, - Description: "run mjml", - Node: &tree.Node{ - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Int("parallel", 0, "number of parallel processes") - return nil - }, - Args: tree.Args{ - { - Name: "path", - Optional: true, - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { - return suggests.List(inst.paths(ctx)) - }, + Description: "Run mjml", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Internal().Int("parallel", 0, "number of parallel processes") + return nil + }, + Args: tree.Args{ + { + Name: "path", + Optional: true, + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { + return suggests.List(inst.paths(ctx)) }, }, - Execute: inst.execute, }, - } + Execute: inst.execute, + }) return inst } @@ -82,11 +80,11 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -113,14 +111,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Generate mjml files. - -Usage: - mjml - -Examples: - mjml ./path/mjml.yml -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -142,7 +133,6 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { out := strings.ReplaceAll(src, ".mjml", ".html") out = strings.ReplaceAll(out, "/src/", "/html/") return shell.New(ctx, c.l, "mjml", src, "-o", out). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Run() }) @@ -192,7 +182,7 @@ func (c *Command) files(ctx context.Context, root string) []string { func (c *Command) wg(ctx context.Context, r *readline.Readline) (context.Context, *errgroup.Group) { wg, ctx := errgroup.WithContext(ctx) - if value, err := r.FlagSet().GetInt("parallel"); err == nil && value != 0 { + if value, _ := r.FlagSets().Internal().GetInt("parallel"); value != 0 { wg.SetLimit(value) } else { wg.SetLimit(1) diff --git a/onepassword/command.go b/onepassword/command.go index 02b2fc3..5da7e6b 100644 --- a/onepassword/command.go +++ b/onepassword/command.go @@ -14,7 +14,7 @@ type ( Command struct { l log.Logger op *OnePassword - commandTree *tree.Root + commandTree tree.Root } CommandOption func(*Command) error ) @@ -39,37 +39,40 @@ func NewCommand(l log.Logger, op *OnePassword, opts ...CommandOption) (*Command, } } } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: "op", - Description: "execute 1Password commands", + Description: "Execute 1Password commands", + Execute: inst.signin, Nodes: tree.Nodes{ + { + Name: "signin", + Description: "Sign into your account", + Execute: inst.signin, + }, { Name: "get", - Description: "retrieve item", + Description: "Retrieve an item", Args: tree.Args{ { - Name: "id", + Name: "id", + Description: "Item name or uuid", }, }, Execute: inst.get, }, - { - Name: "signin", - Description: "sign into your account", - Execute: inst.signin, - }, { Name: "register", - Description: "register an account", + Description: "Register an account", Args: tree.Args{ { - Name: "email", + Name: "email", + Description: "User email address", }, }, Execute: inst.register, }, }, - } + }) return inst, nil } @@ -78,11 +81,11 @@ func NewCommand(l log.Logger, op *OnePassword, opts ...CommandOption) (*Command, // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -94,16 +97,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `1Password session helper. - -Usage: - op [command] - -Available commands: - get [id] Retrieve an entry from your account - signin Sign into your 1Password account for the session - register [email] Add your 1Password account -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ diff --git a/pivotal/licensefinder/command.go b/pivotal/licensefinder/command.go index b6dfe5d..0788b7f 100644 --- a/pivotal/licensefinder/command.go +++ b/pivotal/licensefinder/command.go @@ -24,7 +24,7 @@ type ( name string cache cache.Namespace configKey string - commandTree *tree.Root + commandTree tree.Root } CommandOption func(*Command) ) @@ -80,16 +80,14 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command Execute: inst.execute, } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: inst.name, - Description: "run license finder", - Node: &tree.Node{ - Execute: inst.execute, - }, + Description: "Run license finder", + Execute: inst.execute, Nodes: tree.Nodes{ { Name: "restricted_licenses", - Description: "manage restricted licenses", + Description: "Manage restricted licenses", Nodes: tree.Nodes{ &addNode, &listNode, @@ -98,7 +96,7 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command }, { Name: "ignored_dependencies", - Description: "manage ignored dependencies", + Description: "Manage ignored dependencies", Nodes: tree.Nodes{ &addNode, &listNode, @@ -107,7 +105,7 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command }, { Name: "permitted_licenses", - Description: "manage permitted licenses", + Description: "Manage permitted licenses", Nodes: tree.Nodes{ &addNode, &listNode, @@ -116,7 +114,7 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command }, { Name: "approvals", - Description: "manage approvals", + Description: "Manage approvals", Nodes: tree.Nodes{ &addNode, &removeNode, @@ -124,14 +122,14 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command }, { Name: "licenses", - Description: "manage licenses", + Description: "Manage licenses", Nodes: tree.Nodes{ &addNode, &removeNode, }, }, }, - } + }) return inst } @@ -141,11 +139,11 @@ func NewCommand(l log.Logger, cache cache.Cache, opts ...CommandOption) *Command // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -183,11 +181,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Check project licenses. - -Usage: - licensefinder -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -209,7 +203,6 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { Args(args...). Args(r.Args()...). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Run() } diff --git a/stern/stern/command.go b/stern/stern/command.go index a90ce68..2483296 100644 --- a/stern/stern/command.go +++ b/stern/stern/command.go @@ -19,7 +19,7 @@ type ( l log.Logger kubectl *kubectl.Kubectl squadron *squadron.Squadron - commandTree *tree.Root + commandTree tree.Root namespaceFn NamespaceFn } NamespaceFn func(cluster, fleet, squadron string) string @@ -58,41 +58,39 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl, squadron *squadron.Squad opt(inst) } } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: "stern", - Description: "tail your logs with stern", - Node: &tree.Node{ - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.String("container", "", "Container name when multiple containers in pod (default \".*\")") - fs.String("exclude", "", "Regex of log lines to exclude") - fs.String("exclude-container", "", "Exclude a Container name") - fs.String("include", "", "Regex of log lines to include") - fs.String("selector", "", "Selector (label query) to filter on. If present, default to \".*\" for the pod-query.") - fs.String("since", "", "Return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to 48h") - fs.String("tail", "", "The number of lines from the end of the logs to show. Defaults to -1, showing all logs. (default -1)") - return nil - }, - Args: tree.Args{ - { - Name: "cluster", - Suggest: inst.completeClusters, - }, - { - Name: "fleet", - Suggest: inst.completeFleets, - }, - { - Name: "squadron", - Suggest: inst.completeSquadrons, - }, - { - Name: "unit", - Suggest: inst.completeSquadronUnits, - }, - }, - Execute: inst.execute, + Description: "Tail your logs with stern", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().String("container", "", "Container name when multiple containers in pod (default \".*\")") + fs.Default().String("exclude", "", "Regex of log lines to exclude") + fs.Default().String("exclude-container", "", "Exclude a Container name") + fs.Default().String("include", "", "Regex of log lines to include") + fs.Default().String("selector", "", "Selector (label query) to filter on. If present, default to \".*\" for the pod-query.") + fs.Default().String("since", "", "Return logs newer than a relative duration like 5s, 2m, or 3h. Defaults to 48h") + fs.Default().String("tail", "", "The number of lines from the end of the logs to show. Defaults to -1, showing all logs. (default -1)") + return nil }, - } + Args: tree.Args{ + { + Name: "cluster", + Suggest: inst.completeClusters, + }, + { + Name: "fleet", + Suggest: inst.completeFleets, + }, + { + Name: "squadron", + Suggest: inst.completeSquadrons, + }, + { + Name: "unit", + Suggest: inst.completeSquadronUnits, + }, + }, + Execute: inst.execute, + }) return inst } @@ -102,11 +100,11 @@ func NewCommand(l log.Logger, kubectl *kubectl.Kubectl, squadron *squadron.Squad // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -118,14 +116,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Tail your logs with stern. - -Usage: - stern [cluster] [fleet] [squadron] [unit] - -Examples: - stern example-cluster my-namespace my-deployment -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -138,23 +129,22 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { Env(c.kubectl.Cluster(cluster).Env()). Args("--namespace", c.namespaceFn(cluster, fleet, squad)). Args("--selector", "\"app.kubernetes.io/name="+squad+"-"+unit+"\""). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Run() } -func (c *Command) completeClusters(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { +func (c *Command) completeClusters(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(c.kubectl.Clusters()) } -func (c *Command) completeFleets(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { +func (c *Command) completeFleets(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { if cluster, ok := c.squadron.Cluster(r.Args().At(0)); ok { return suggests.List(cluster.Fleets) } return nil } -func (c *Command) completeSquadrons(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { +func (c *Command) completeSquadrons(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { if value, err := c.squadron.List(); err != nil { c.l.Debug(err.Error()) return nil @@ -162,7 +152,7 @@ func (c *Command) completeSquadrons(ctx context.Context, t *tree.Root, r *readli return suggests.List(value) } } -func (c *Command) completeSquadronUnits(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { +func (c *Command) completeSquadronUnits(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { cluster, fleet, squad := r.Args().At(0), r.Args().At(1), r.Args().At(2) if value, err := c.squadron.ListUnits(ctx, squad, cluster, fleet, true); err != nil { c.l.Debug(err.Error()) diff --git a/webdriverio/webdriverio/command.go b/webdriverio/webdriverio/command.go index 46dc967..48eaba1 100644 --- a/webdriverio/webdriverio/command.go +++ b/webdriverio/webdriverio/command.go @@ -28,7 +28,7 @@ type ( name string cache cache.Namespace configKey string - commandTree *tree.Root + commandTree tree.Root } CommandOption func(*Command) ) @@ -70,54 +70,59 @@ func NewCommand(l log.Logger, c cache.Cache, op *onepassword.OnePassword, opts . return nil, err } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: inst.name, - Description: "run wdio commands", + Description: "Run wdio commands", Nodes: tree.Nodes{ { Name: "mode", - Description: "run mode", + Description: "Run mode", Values: func(ctx context.Context, r *readline.Readline) []goprompt.Suggest { return suggests.List(inst.cfg.Modes.Keys()) }, Nodes: tree.Nodes{ { Name: "site", - Description: "configured site", + Description: "Configured site", Values: func(ctx context.Context, r *readline.Readline) []goprompt.Suggest { return suggests.List(inst.cfg.Sites.Keys()) }, Nodes: tree.Nodes{ { Name: "env", - Description: "configured env", + Description: "Configured env", Values: func(ctx context.Context, r *readline.Readline) []goprompt.Suggest { if value, ok := inst.cfg.Sites[r.Args().At(1)]; ok { return suggests.List(value.Keys()) } return nil }, - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.String("tag", "", "run suite on specific tag") - fs.String("spec", "", "run suite on specific specs") - fs.String("suite", "", "run suite on test suite") - fs.String("scenario", "", "run suite on specific specs") - fs.String("log-level", "info", "set the log level") - fs.Bool("headless", false, "run suite in headless mode") - fs.Bool("debug", false, "run in debug mode and leave browser open after test failure") - fs.Bool("bail", false, "stop test runner after specific amount of tests have failed") + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().String("spec", "", "Run suite on specific specs") + fs.Default().String("suite", "", "Run suite on test suite") + fs.Internal().String("tag", "", "Run suite on specific tag") + fs.Internal().String("scenario", "", "Run suite on specific specs") + fs.Internal().String("log-level", "info", "Set the log level") + fs.Internal().Bool("ci", false, "Run suite on CI") + fs.Internal().Bool("headless", false, "Run suite in headless mode") + fs.Internal().Bool("debug", false, "Run in debug mode and leave browser open after test failure") + fs.Internal().Bool("bail", false, "Stop test runner after specific amount of tests have failed") if r.Args().LenGte(4) { - if err := fs.SetValues("spec", inst.specs(ctx, r.Args().At(3))...); err != nil { + if err := fs.Default().SetValues("spec", inst.specs(ctx, r.Args().At(3))...); err != nil { return err } - if err := fs.SetValues("tag", inst.tags(ctx, r.Args().At(3), fs.GetString("spec"))...); err != nil { + spec, err := fs.Default().GetString("spec") + if err != nil { return err } - if err := fs.SetValues("scenario", inst.scenarios(ctx, r.Args().At(3), fs.GetString("spec"))...); err != nil { + if err := fs.Internal().SetValues("tag", inst.tags(ctx, r.Args().At(3), spec)...); err != nil { + return err + } + if err := fs.Internal().SetValues("scenario", inst.scenarios(ctx, r.Args().At(3), spec)...); err != nil { return err } } - if err := fs.SetValues("log-level", "info", "warn", "debug"); err != nil { + if err := fs.Internal().SetValues("log-level", "info", "warn", "debug"); err != nil { return err } return nil @@ -127,7 +132,7 @@ func NewCommand(l log.Logger, c cache.Cache, op *onepassword.OnePassword, opts . Name: "path", Repeat: false, Optional: true, - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(inst.paths(ctx)) }, }, @@ -139,7 +144,7 @@ func NewCommand(l log.Logger, c cache.Cache, op *onepassword.OnePassword, opts . }, }, }, - } + }) return inst, nil } @@ -149,11 +154,11 @@ func NewCommand(l log.Logger, c cache.Cache, op *onepassword.OnePassword, opts . // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -165,11 +170,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Run wdio commands. - -Usage: - wdio [mode] [site] [env] [path] -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -177,39 +178,38 @@ Usage: // ------------------------------------------------------------------------------------------------ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { + fs := r.FlagSets().Default() + ifs := r.FlagSets().Internal() mode, site, env := r.Args().At(0), r.Args().At(1), r.Args().At(2) siteConfig := c.cfg.Sites[site][env] modeConfig := c.cfg.Modes[mode] + logLevel := log.MustGet(ifs.GetString("log-level"))(c.l) + envs := []string{ - "LOG_LEVEL=" + r.FlagSet().GetString("log-level"), + "LOG_LEVEL=" + logLevel, "NODE_TLS_REJECT_UNAUTHORIZED=0", // allow TLS errors when in local mode with self-signed certificates } var args []string - if r.FlagSet().GetBool("debug") { + if log.MustGet(ifs.GetBool("debug"))(c.l) { envs = append(envs, fmt.Sprintf("debug=%s", "true")) } - if r.FlagSet().GetBool("headless") { + if log.MustGet(ifs.GetBool("headless"))(c.l) { envs = append(envs, fmt.Sprintf("HEADLESS=%s", "true")) } - if r.FlagSet().GetBool("ci") { + if log.MustGet(ifs.GetBool("ci"))(c.l) { envs = append(envs, fmt.Sprintf("E2E_ENV=%s", "ci")) } else { envs = append(envs, fmt.Sprintf("E2E_ENV=%s", "chromium")) } - if value := r.FlagSet().GetString("scenario"); value != "" { + if value := log.MustGet(ifs.GetString("scenario"))(c.l); value != "" { envs = append(envs, fmt.Sprintf("SCENARIOS=%s", strings.Trim(value, "\""))) } - if value := r.FlagSet().GetString("tag"); value != "" { + if value := log.MustGet(ifs.GetString("tag"))(c.l); value != "" { args = append(args, "--cucumberOpts.tagExpression", "'"+strings.Trim(value, "\"")+"'") } - if value := r.FlagSet().GetString("spec"); value != "" { - args = append(args, "--spec", value) - } - if value := r.FlagSet().GetString("suite"); value != "" { - args = append(args, "--suite", value) - } + // base url baseURL := siteConfig.Domain if modeConfig.HostPrefix != "" { @@ -242,7 +242,7 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { c.l.Info("└ " + dir) if err := shell.New(ctx, c.l, "wdio", "run", "e2e/wdio.conf.ts"). Args(args...). - Args(r.PassThroughFlags()...). + Args(fs.Visited().Args()...). Args(r.AdditionalArgs()...). Dir(dir). Env(envs...). diff --git a/yarnpkg/yarn/command.go b/yarnpkg/yarn/command.go index e0f6ae8..9021945 100644 --- a/yarnpkg/yarn/command.go +++ b/yarnpkg/yarn/command.go @@ -23,7 +23,7 @@ type ( l log.Logger name string cache cache.Namespace - commandTree *tree.Root + commandTree tree.Root } CommandOption func(*Command) ) @@ -54,27 +54,25 @@ func NewCommand(l log.Logger, c cache.Cache, opts ...CommandOption) *Command { } } - inst.commandTree = &tree.Root{ + inst.commandTree = tree.New(&tree.Node{ Name: inst.name, - Description: "run yarn commands", - Node: &tree.Node{ - Execute: inst.execute, - }, + Description: "Run yarn commands", + Execute: inst.execute, Nodes: tree.Nodes{ { Name: "install", - Description: "install dependencies", + Description: "Install dependencies", Args: tree.Args{inst.pathArg()}, Execute: inst.install, }, { Name: "run", - Description: "run script", + Description: "Run script", Args: tree.Args{ inst.pathArg(), { Name: "script", - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(inst.scripts(ctx, r.Args().At(1))) }, }, @@ -83,9 +81,9 @@ func NewCommand(l log.Logger, c cache.Cache, opts ...CommandOption) *Command { }, { Name: "run-all", - Description: "run script in all", - Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSet) error { - fs.Int("parallel", 0, "number of parallel processes") + Description: "Run script in all", + Flags: func(ctx context.Context, r *readline.Readline, fs *readline.FlagSets) error { + fs.Default().Int("parallel", 0, "number of parallel processes") return nil }, Args: tree.Args{ @@ -96,7 +94,7 @@ func NewCommand(l log.Logger, c cache.Cache, opts ...CommandOption) *Command { Execute: inst.runAll, }, }, - } + }) return inst } @@ -106,11 +104,11 @@ func NewCommand(l log.Logger, c cache.Cache, opts ...CommandOption) *Command { // ------------------------------------------------------------------------------------------------ func (c *Command) Name() string { - return c.commandTree.Name + return c.commandTree.Node().Name } func (c *Command) Description() string { - return c.commandTree.Description + return c.commandTree.Node().Description } func (c *Command) Complete(ctx context.Context, r *readline.Readline) []goprompt.Suggest { @@ -122,16 +120,7 @@ func (c *Command) Execute(ctx context.Context, r *readline.Readline) error { } func (c *Command) Help(ctx context.Context, r *readline.Readline) string { - return `Run yarn commands. - -Usage: - yarn [cmd] - -Available Commands: - install install dependencies - run [path] [script] run script - run-all [script] run script in all -` + return c.commandTree.Help(ctx, r) } // ------------------------------------------------------------------------------------------------ @@ -142,7 +131,6 @@ func (c *Command) execute(ctx context.Context, r *readline.Readline) error { return shell.New(ctx, c.l, "yarn"). Args(r.Args()...). Args(r.Flags()...). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Run() } @@ -151,7 +139,6 @@ func (c *Command) run(ctx context.Context, r *readline.Readline) error { dir, script := r.Args().At(1), r.Args().At(2) c.l.Infof("Running script %q in %q", script, dir) return shell.New(ctx, c.l, "yarn", "run", script). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Dir(dir). Run() @@ -166,7 +153,6 @@ func (c *Command) runAll(ctx context.Context, r *readline.Readline) error { wg.Go(func() error { c.l.Info("└ " + dir) return shell.New(ctx, c.l, "yarn", "run", script). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Dir(dir). Run() @@ -183,7 +169,6 @@ func (c *Command) install(ctx context.Context, r *readline.Readline) error { } c.l.Infof("Running install in %q", dir) return shell.New(ctx, c.l, "yarn", "install"). - Args(r.PassThroughFlags()...). Args(r.AdditionalArgs()...). Dir(dir). Run() @@ -227,7 +212,7 @@ func (c *Command) pathArg() *tree.Arg { return &tree.Arg{ Name: "path", Optional: true, - Suggest: func(ctx context.Context, t *tree.Root, r *readline.Readline) []goprompt.Suggest { + Suggest: func(ctx context.Context, t tree.Root, r *readline.Readline) []goprompt.Suggest { return suggests.List(c.paths(ctx)) }, } @@ -235,7 +220,7 @@ func (c *Command) pathArg() *tree.Arg { func (c *Command) wg(ctx context.Context, r *readline.Readline) (context.Context, *errgroup.Group) { wg, ctx := errgroup.WithContext(ctx) - if value, err := r.FlagSet().GetInt("parallel"); err == nil && value != 0 { + if value, err := r.FlagSets().Default().GetInt("parallel"); err == nil && value != 0 { wg.SetLimit(value) } else { wg.SetLimit(1)