Skip to content

Commit

Permalink
style: 🚨 lint vendored code
Browse files Browse the repository at this point in the history
  • Loading branch information
ghostsquad committed Aug 9, 2020
1 parent 33bb391 commit 628a2b3
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 38 deletions.
12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Expand Up @@ -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"
5 changes: 5 additions & 0 deletions pkg/util/templates/command_groups.go
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
66 changes: 39 additions & 27 deletions pkg/util/templates/templater.go
Expand Up @@ -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")
Expand All @@ -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(),
Expand All @@ -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) },
Expand All @@ -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 {
Expand All @@ -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{
Expand Down
8 changes: 4 additions & 4 deletions pkg/util/term/resize.go
Expand Up @@ -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)
//}
5 changes: 4 additions & 1 deletion pkg/util/term/term_writer.go
Expand Up @@ -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' {
Expand Down

0 comments on commit 628a2b3

Please sign in to comment.