From cb590e324ead2fee0300c15863aa26f0721fb3c3 Mon Sep 17 00:00:00 2001 From: Maas Lalani Date: Sat, 30 Jul 2022 17:40:33 -0400 Subject: [PATCH] chore: fix linting --- completion/bash.go | 2 +- completion/command.go | 3 +-- filter/command.go | 3 +-- format/command.go | 4 ++-- format/options.go | 2 +- input/command.go | 5 +---- internal/decode/align.go | 2 +- internal/stdin/stdin.go | 3 ++- style/borders.go | 2 +- style/spacing.go | 16 +++++++++++----- 10 files changed, 22 insertions(+), 20 deletions(-) diff --git a/completion/bash.go b/completion/bash.go index f8756db..b4f1aed 100644 --- a/completion/bash.go +++ b/completion/bash.go @@ -55,7 +55,7 @@ const ( ShellCompDirectiveFilterDirs // =========================================================================== - + // // All directives using iota should be above this one. // For internal use. shellCompDirectiveMaxValue //nolint:deadcode,unused,varcheck diff --git a/completion/command.go b/completion/command.go index ebc552f..72e25ca 100644 --- a/completion/command.go +++ b/completion/command.go @@ -39,8 +39,7 @@ func isArgument(cmd *kong.Node) bool { // writeString writes a string into a buffer, and checks if the error is not nil. func writeString(b io.StringWriter, s string) { - _, err := b.WriteString(s) - if err != nil { + if _, err := b.WriteString(s); err != nil { fmt.Fprintln(os.Stderr, "Error:", err) os.Exit(1) } diff --git a/filter/command.go b/filter/command.go index d2b4d79..f02c988 100644 --- a/filter/command.go +++ b/filter/command.go @@ -24,9 +24,8 @@ func (o Options) Run() error { i.Placeholder = o.Placeholder i.Width = o.Width - input, _ := stdin.Read() var choices []string - if input != "" { + if input, _ := stdin.Read(); input != "" { choices = strings.Split(strings.TrimSpace(input), "\n") } else { choices = files.List() diff --git a/format/command.go b/format/command.go index 293b347..1555f45 100644 --- a/format/command.go +++ b/format/command.go @@ -18,7 +18,7 @@ import ( "github.com/charmbracelet/gum/internal/stdin" ) -// Func is a function that formats some text +// Func is a function that formats some text. type Func func(string) (string, error) var formatType = map[string]Func{ @@ -28,7 +28,7 @@ var formatType = map[string]Func{ "template": template, } -// Run runs the format command +// Run runs the format command. func (o Options) Run() error { var input string if len(o.Template) > 0 { diff --git a/format/options.go b/format/options.go index 3098faf..1291251 100644 --- a/format/options.go +++ b/format/options.go @@ -1,6 +1,6 @@ package format -// Options is customization options for the format command +// Options is customization options for the format command. type Options struct { Template []string `arg:"" optional:"" help:"Template string to format (can also be provided via stdin)"` diff --git a/input/command.go b/input/command.go index 0a3f8c4..4625a17 100644 --- a/input/command.go +++ b/input/command.go @@ -14,11 +14,8 @@ import ( // Run provides a shell script interface for the text input bubble. // https://github.com/charmbracelet/bubbles/textinput func (o Options) Run() error { - in, _ := stdin.Read() - i := textinput.New() - - if in != "" && o.Value == "" { + if in, _ := stdin.Read(); in != "" && o.Value == "" { i.SetValue(in) } else { i.SetValue(o.Value) diff --git a/internal/decode/align.go b/internal/decode/align.go index 3b97919..813bcdd 100644 --- a/internal/decode/align.go +++ b/internal/decode/align.go @@ -2,7 +2,7 @@ package decode import "github.com/charmbracelet/lipgloss" -// Align maps strings to `lipgloss.Position`s +// Align maps strings to `lipgloss.Position`s. var Align = map[string]lipgloss.Position{ "center": lipgloss.Center, "left": lipgloss.Left, diff --git a/internal/stdin/stdin.go b/internal/stdin/stdin.go index 2507e72..9a20cdd 100644 --- a/internal/stdin/stdin.go +++ b/internal/stdin/stdin.go @@ -2,6 +2,7 @@ package stdin import ( "bufio" + "errors" "io" "os" "strings" @@ -11,7 +12,7 @@ import ( func Read() (string, error) { stat, err := os.Stdin.Stat() if err != nil { - return "", err + return "", errors.New("failed to get stdin stat") } if stat.Mode()&os.ModeNamedPipe == 0 && stat.Size() == 0 { diff --git a/style/borders.go b/style/borders.go index 8ce177f..0fcf3c3 100644 --- a/style/borders.go +++ b/style/borders.go @@ -2,7 +2,7 @@ package style import "github.com/charmbracelet/lipgloss" -// border maps strings to `lipgloss.Border`s +// border maps strings to `lipgloss.Border`s. var border map[string]lipgloss.Border = map[string]lipgloss.Border{ "double": lipgloss.DoubleBorder(), "hidden": lipgloss.HiddenBorder(), diff --git a/style/spacing.go b/style/spacing.go index 0c8e4bb..a1eb3f6 100644 --- a/style/spacing.go +++ b/style/spacing.go @@ -5,20 +5,26 @@ import ( "strings" ) +const maxTokens = 4 + // parsePadding parses 1 - 4 integers from a string and returns them in a top, -// right, bottom, left order for use in the lipgloss.Padding() method +// right, bottom, left order for use in the lipgloss.Padding() method. func parsePadding(s string) (int, int, int, int) { - var ints []int + var ints [maxTokens]int tokens := strings.Split(s, " ") + if len(tokens) > maxTokens { + return 0, 0, 0, 0 + } + // All tokens must be an integer - for _, token := range tokens { + for i, token := range tokens { parsed, err := strconv.Atoi(token) if err != nil { return 0, 0, 0, 0 } - ints = append(ints, parsed) + ints[i] = parsed } if len(tokens) == 1 { @@ -29,7 +35,7 @@ func parsePadding(s string) (int, int, int, int) { return ints[0], ints[1], ints[0], ints[1] } - if len(tokens) == 4 { + if len(tokens) == maxTokens { return ints[0], ints[1], ints[2], ints[3] }