From 628a2b36399be79b623441d9047af1353d7e1aef Mon Sep 17 00:00:00 2001 From: Weston McNamee Date: Sat, 8 Aug 2020 23:11:10 -0700 Subject: [PATCH] =?UTF-8?q?style:=20=F0=9F=9A=A8=20=20lint=20vendored=20co?= =?UTF-8?q?de?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .pre-commit-config.yaml | 12 ++--- pkg/util/templates/command_groups.go | 5 +++ pkg/util/templates/templater.go | 66 ++++++++++++++++------------ pkg/util/term/resize.go | 8 ++-- pkg/util/term/term_writer.go | 5 ++- 5 files changed, 58 insertions(+), 38 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 442960a..cba396c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -20,16 +20,16 @@ repos: hooks: - id: go-fmt-import - id: go-vet - exclude: "^scripts/tools.go|pkg/util/templates|pkg/util/term" + exclude: "^scripts/tools.go" - id: go-lint - exclude: "^scripts/tools.go|pkg/util/templates|pkg/util/term" + exclude: "^scripts/tools.go" - id: go-unit-tests exclude: "^scripts/tools.go" - id: gofumpt # requires github.com/mvdan/gofumpt - exclude: "^scripts/tools.go|pkg/util/templates|pkg/util/term" + exclude: "^scripts/tools.go" - id: go-err-check # requires github.com/kisielk/errcheck - exclude: "^scripts/tools.go|pkg/util/templates|pkg/util/term" + exclude: "^scripts/tools.go" - id: go-static-check # install https://staticcheck.io/docs/ - exclude: "^scripts/tools.go|pkg/util/templates|pkg/util/term" + exclude: "^scripts/tools.go" - id: golangci-lint # requires github.com/golangci/golangci-lint - exclude: "^scripts/tools.go|pkg/util/templates|pkg/util/term" + exclude: "^scripts/tools.go" diff --git a/pkg/util/templates/command_groups.go b/pkg/util/templates/command_groups.go index 2a93ca0..28e5119 100644 --- a/pkg/util/templates/command_groups.go +++ b/pkg/util/templates/command_groups.go @@ -22,19 +22,23 @@ import ( "github.com/spf13/cobra" ) +// CommandGroup represents the commands that are grouped together within the help output type CommandGroup struct { Message string Commands []*cobra.Command } +// CommandGroups is just a slice of type CommandGroup type CommandGroups []CommandGroup +// Add adds a cobra command all groups within the CommandGroups func (g CommandGroups) Add(c *cobra.Command) { for _, group := range g { c.AddCommand(group.Commands...) } } +// Has checks if a cobra command exists within any CommandGroup within the CommandGroups slice func (g CommandGroups) Has(c *cobra.Command) bool { for _, group := range g { for _, command := range group.Commands { @@ -46,6 +50,7 @@ func (g CommandGroups) Has(c *cobra.Command) bool { return false } +// AddAdditionalCommands creates a new CommandGroup and returns the CommandGroups slice func AddAdditionalCommands(g CommandGroups, message string, cmds []*cobra.Command) CommandGroups { group := CommandGroup{Message: message} for _, c := range cmds { diff --git a/pkg/util/templates/templater.go b/pkg/util/templates/templater.go index 1fefaec..45f3c75 100644 --- a/pkg/util/templates/templater.go +++ b/pkg/util/templates/templater.go @@ -31,10 +31,12 @@ import ( flag "github.com/spf13/pflag" ) +// FlagExposer exposes flags! Fancy that type FlagExposer interface { ExposeFlags(cmd *cobra.Command, flags ...string) FlagExposer } +// ActsAsRootCommand defines which command is the "root" command for groups func ActsAsRootCommand(cmd *cobra.Command, filters []string, groups ...CommandGroup) FlagExposer { if cmd == nil { panic("nil root command") @@ -53,6 +55,8 @@ func ActsAsRootCommand(cmd *cobra.Command, filters []string, groups ...CommandGr return templater } +// UseOptionsTemplates docs to be written +// TODO: write UseOptionsTemplates docs func UseOptionsTemplates(cmd *cobra.Command) { templater := &templater{ UsageTemplate: OptionsUsageTemplate(), @@ -70,47 +74,55 @@ type templater struct { Filtered []string } -func (templater *templater) FlagErrorFunc(exposedFlags ...string) func(*cobra.Command, error) error { +// FlagErrorFunc docs to be written +// TODO: write FlagErrorFunc docs +func (t *templater) FlagErrorFunc(exposedFlags ...string) func(*cobra.Command, error) error { return func(c *cobra.Command, err error) error { c.SilenceUsage = true switch c.CalledAs() { case "options": - return fmt.Errorf("%s\nRun '%s' without flags.", err, c.CommandPath()) + return fmt.Errorf("%s\nRun '%s' without flags", err, c.CommandPath()) default: - return fmt.Errorf("%s\nSee '%s --help' for usage.", err, c.CommandPath()) + return fmt.Errorf("%s\nSee '%s --help' for usage", err, c.CommandPath()) } } } -func (templater *templater) ExposeFlags(cmd *cobra.Command, flags ...string) FlagExposer { - cmd.SetUsageFunc(templater.UsageFunc(flags...)) - return templater +// ExposeFlags docs to be written +// TODO: write ExposeFlags docs +func (t *templater) ExposeFlags(cmd *cobra.Command, flags ...string) FlagExposer { + cmd.SetUsageFunc(t.UsageFunc(flags...)) + return t } -func (templater *templater) HelpFunc() func(*cobra.Command, []string) { +// HelpFunc docs to be written +// TODO: write HelpFunc docs +func (t *templater) HelpFunc() func(*cobra.Command, []string) { return func(c *cobra.Command, s []string) { - t := template.New("help") - t.Funcs(templater.templateFuncs()) - template.Must(t.Parse(templater.HelpTemplate)) + tmpl := template.New("help") + tmpl.Funcs(t.templateFuncs()) + template.Must(tmpl.Parse(t.HelpTemplate)) out := term.NewResponsiveWriter(c.OutOrStdout()) - err := t.Execute(out, c) + err := tmpl.Execute(out, c) if err != nil { c.Println(err) } } } -func (templater *templater) UsageFunc(exposedFlags ...string) func(*cobra.Command) error { +// UsageFunc docs to be written +// TODO: write UsageFunc docs +func (t *templater) UsageFunc(exposedFlags ...string) func(*cobra.Command) error { return func(c *cobra.Command) error { - t := template.New("usage") - t.Funcs(templater.templateFuncs(exposedFlags...)) - template.Must(t.Parse(templater.UsageTemplate)) + tmpl := template.New("usage") + tmpl.Funcs(t.templateFuncs(exposedFlags...)) + template.Must(tmpl.Parse(t.UsageTemplate)) out := term.NewResponsiveWriter(c.OutOrStderr()) - return t.Execute(out, c) + return tmpl.Execute(out, c) } } -func (templater *templater) templateFuncs(exposedFlags ...string) template.FuncMap { +func (t *templater) templateFuncs(exposedFlags ...string) template.FuncMap { return template.FuncMap{ "trim": strings.TrimSpace, "trimRight": func(s string) string { return strings.TrimRightFunc(s, unicode.IsSpace) }, @@ -122,12 +134,12 @@ func (templater *templater) templateFuncs(exposedFlags ...string) template.FuncM "flagsNotIntersected": flagsNotIntersected, "visibleFlags": visibleFlags, "flagsUsages": flagsUsages, - "cmdGroups": templater.cmdGroups, - "cmdGroupsString": templater.cmdGroupsString, - "rootCmd": templater.rootCmdName, - "isRootCmd": templater.isRootCmd, - "optionsCmdFor": templater.optionsCmdFor, - "usageLine": templater.usageLine, + "cmdGroups": t.cmdGroups, + "cmdGroupsString": t.cmdGroupsString, + "rootCmd": t.rootCmdName, + "isRootCmd": t.isRootCmd, + "optionsCmdFor": t.optionsCmdFor, + "usageLine": t.usageLine, "exposed": func(c *cobra.Command) *flag.FlagSet { exposed := flag.NewFlagSet("exposed", flag.ContinueOnError) if len(exposedFlags) > 0 { @@ -142,10 +154,10 @@ func (templater *templater) templateFuncs(exposedFlags ...string) template.FuncM } } -func (templater *templater) cmdGroups(c *cobra.Command, all []*cobra.Command) []CommandGroup { - if len(templater.CommandGroups) > 0 && c == templater.RootCmd { - all = filter(all, templater.Filtered...) - return AddAdditionalCommands(templater.CommandGroups, "Other Commands:", all) +func (t *templater) cmdGroups(c *cobra.Command, all []*cobra.Command) []CommandGroup { + if len(t.CommandGroups) > 0 && c == t.RootCmd { + all = filter(all, t.Filtered...) + return AddAdditionalCommands(t.CommandGroups, "Other Commands:", all) } all = filter(all, "options") return []CommandGroup{ diff --git a/pkg/util/term/resize.go b/pkg/util/term/resize.go index 6223f03..b044f87 100644 --- a/pkg/util/term/resize.go +++ b/pkg/util/term/resize.go @@ -143,7 +143,7 @@ func (s *sizeQueue) Next() *TerminalSize { return &size } -// stop stops the background goroutine that is monitoring for terminal resizes. -func (s *sizeQueue) stop() { - close(s.stopResizing) -} +//// stop stops the background goroutine that is monitoring for terminal resizes. +//func (s *sizeQueue) stop() { +// close(s.stopResizing) +//} diff --git a/pkg/util/term/term_writer.go b/pkg/util/term/term_writer.go index 3f3b869..7491418 100644 --- a/pkg/util/term/term_writer.go +++ b/pkg/util/term/term_writer.go @@ -109,7 +109,10 @@ func NewMaxWidthWriter(w io.Writer, maxWidth uint) io.Writer { func (m maxWidthWriter) Write(p []byte) (nn int, err error) { for _, b := range p { if m.currentWidth == m.maxWidth { - m.writer.Write([]byte{'\n'}) + _, err := m.writer.Write([]byte{'\n'}) + if err != nil { + return int(m.written), err + } m.currentWidth = 0 } if b == '\n' {