diff options
author | Carlos A Becker <caarlos0@users.noreply.github.com> | 2022-10-25 11:40:51 -0300 |
---|---|---|
committer | Christian Muehlhaeuser <muesli@gmail.com> | 2022-11-11 02:14:26 +0100 |
commit | 4dd3ba1d3c2b47a68b3ee4a7f30f97b0e5b18c78 (patch) | |
tree | 37466025f6a27dea28a351401fbfa416ea86674e | |
parent | 60d98a01e55f73c175de6e6068f96ba74b9dbeb9 (diff) |
chore: fmt & lint issues
-rw-r--r-- | .github/workflows/goreleaser.yml | 2 | ||||
-rw-r--r-- | .goreleaser.yml | 2 | ||||
-rw-r--r-- | config_cmd.go | 4 | ||||
-rw-r--r-- | github.go | 2 | ||||
-rw-r--r-- | gitlab.go | 2 | ||||
-rw-r--r-- | main.go | 19 | ||||
-rw-r--r-- | stash_cmd.go | 6 | ||||
-rw-r--r-- | style.go | 4 | ||||
-rw-r--r-- | ui/config.go | 4 | ||||
-rw-r--r-- | ui/consts_unix.go | 1 | ||||
-rw-r--r-- | ui/ignore_general.go | 1 | ||||
-rw-r--r-- | ui/keys.go | 6 | ||||
-rw-r--r-- | ui/markdown.go | 3 | ||||
-rw-r--r-- | ui/pager.go | 19 | ||||
-rw-r--r-- | ui/stash.go | 40 | ||||
-rw-r--r-- | ui/stashhelp.go | 8 | ||||
-rw-r--r-- | ui/styles.go | 5 | ||||
-rw-r--r-- | ui/ui.go | 12 | ||||
-rw-r--r-- | utils/utils.go | 1 |
19 files changed, 69 insertions, 72 deletions
diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml index cbe25e8..1ccf980 100644 --- a/.github/workflows/goreleaser.yml +++ b/.github/workflows/goreleaser.yml @@ -21,3 +21,5 @@ jobs: nfpm_gpg_key: ${{ secrets.NFPM_GPG_KEY }} nfpm_passphrase: ${{ secrets.NFPM_PASSPHRASE }} snapcraft_token: ${{ secrets.SNAPCRAFT_TOKEN }} + +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json diff --git a/.goreleaser.yml b/.goreleaser.yml index 469279a..6f6fead 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -10,3 +10,5 @@ variables: maintainer: "Christian Muehlhaeuser <muesli@charm.sh>" brew_commit_author_name: "Christian Muehlhaeuser" brew_commit_author_email: "muesli@charm.sh" + +# yaml-language-server: $schema=https://goreleaser.com/static/schema-pro.json diff --git a/config_cmd.go b/config_cmd.go index 9601ea6..6dfc835 100644 --- a/config_cmd.go +++ b/config_cmd.go @@ -6,8 +6,8 @@ import ( "os" "os/exec" "path" - "strings" "path/filepath" + "strings" gap "github.com/muesli/go-app-paths" "github.com/spf13/cobra" @@ -54,7 +54,7 @@ var configCmd = &cobra.Command{ if _, err := os.Stat(configFile); os.IsNotExist(err) { // File doesn't exist yet, create all necessary directories and // write the default config file - if err := os.MkdirAll(filepath.Dir(configFile), 0700); err != nil { + if err := os.MkdirAll(filepath.Dir(configFile), 0o700); err != nil { return err } @@ -33,6 +33,8 @@ func findGitHubREADME(s string) (*source, error) { v := u v.Path += "/master/" + r + // nolint:bodyclose + // it is closed on the caller resp, err := http.Get(v.String()) if err != nil { return nil, err @@ -32,6 +32,8 @@ func findGitLabREADME(s string) (*source, error) { v := u v.Path += "/raw/master/" + r + // nolint:bodyclose + // it is closed on the caller resp, err := http.Get(v.String()) if err != nil { return nil, err @@ -4,7 +4,6 @@ import ( "errors" "fmt" "io" - "io/ioutil" "net/http" "net/url" "os" @@ -24,7 +23,9 @@ import ( ) var ( - Version = "" + // Version as provided by goreleaser. + Version = "" + // CommitSHA as provided by goreleaser. CommitSHA = "" readmeNames = []string{"README.md", "README"} @@ -87,6 +88,7 @@ func sourceFromArg(arg string) (*source, error) { if err != nil { return nil, err } + defer resp.Body.Close() //nolint:errcheck if resp.StatusCode != http.StatusOK { return nil, fmt.Errorf("HTTP status %d", resp.StatusCode) } @@ -198,12 +200,11 @@ func execute(cmd *cobra.Command, args []string) error { return err } else if yes { src := &source{reader: os.Stdin} - defer src.reader.Close() + defer src.reader.Close() //nolint:errcheck return executeCLI(cmd, src, os.Stdout) } switch len(args) { - // TUI running on cwd case 0: return runTUI("", false) @@ -239,12 +240,12 @@ func executeArg(cmd *cobra.Command, arg string, w io.Writer) error { if err != nil { return err } - defer src.reader.Close() + defer src.reader.Close() //nolint:errcheck return executeCLI(cmd, src, w) } func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error { - b, err := ioutil.ReadAll(src.reader) + b, err := io.ReadAll(src.reader) if err != nil { return err } @@ -301,7 +302,7 @@ func executeCLI(cmd *cobra.Command, src *source, w io.Writer) error { } pa := strings.Split(pagerCmd, " ") - c := exec.Command(pa[0], pa[1:]...) + c := exec.Command(pa[0], pa[1:]...) // nolint:gosec c.Stdin = strings.NewReader(content) c.Stdout = os.Stdout return c.Run() @@ -324,7 +325,7 @@ func runTUI(workingDirectory string, stashedOnly bool) error { if err != nil { return err } - defer f.Close() + defer f.Close() //nolint:errcheck } cfg.WorkingDirectory = workingDirectory @@ -375,7 +376,7 @@ func init() { rootCmd.Flags().BoolVarP(&showAllFiles, "all", "a", false, "show system files and directories (TUI-mode only)") rootCmd.Flags().BoolVarP(&localOnly, "local", "l", false, "show local files only; no network (TUI-mode only)") rootCmd.Flags().BoolVarP(&mouse, "mouse", "m", false, "enable mouse wheel (TUI-mode only)") - rootCmd.Flags().MarkHidden("mouse") + _ = rootCmd.Flags().MarkHidden("mouse") // Config bindings _ = viper.BindPFlag("style", rootCmd.Flags().Lookup("style")) diff --git a/stash_cmd.go b/stash_cmd.go index f4bcdcb..efb8bbd 100644 --- a/stash_cmd.go +++ b/stash_cmd.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "io" "log" "os" "path" @@ -42,8 +42,8 @@ var ( return fmt.Errorf("bad filename") } - defer f.Close() - b, err := ioutil.ReadAll(f) + defer f.Close() //nolint:errcheck + b, err := io.ReadAll(f) if err != nil { return fmt.Errorf("error reading file") } @@ -1,8 +1,6 @@ package main -import ( - . "github.com/charmbracelet/lipgloss" -) +import . "github.com/charmbracelet/lipgloss" //nolint:revive var ( keyword = NewStyle(). diff --git a/ui/config.go b/ui/config.go index ef58c0e..9db2340 100644 --- a/ui/config.go +++ b/ui/config.go @@ -21,10 +21,6 @@ type Config struct { GlamourEnabled bool `env:"GLOW_ENABLE_GLAMOUR" default:"true"` } -func (c Config) showLocalFiles() bool { - return c.DocumentTypes.Contains(LocalDoc) -} - func (c Config) localOnly() bool { return c.DocumentTypes.Equals(NewDocTypeSet(LocalDoc)) } diff --git a/ui/consts_unix.go b/ui/consts_unix.go index fd13ad3..57f75dd 100644 --- a/ui/consts_unix.go +++ b/ui/consts_unix.go @@ -1,3 +1,4 @@ +//go:build !windows // +build !windows package ui diff --git a/ui/ignore_general.go b/ui/ignore_general.go index 21f4734..43c1dd1 100644 --- a/ui/ignore_general.go +++ b/ui/ignore_general.go @@ -1,3 +1,4 @@ +//go:build !darwin // +build !darwin package ui diff --git a/ui/keys.go b/ui/keys.go new file mode 100644 index 0000000..8e13f95 --- /dev/null +++ b/ui/keys.go @@ -0,0 +1,6 @@ +package ui + +const ( + keyEnter = "enter" + keyEsc = "esc" +) diff --git a/ui/markdown.go b/ui/markdown.go index f6f8f2c..c5fbff4 100644 --- a/ui/markdown.go +++ b/ui/markdown.go @@ -168,8 +168,7 @@ func wrapMarkdowns(t DocType, md []*charm.Markdown) (m []*markdown) { // Return the time in a human-readable format relative to the current time. func relativeTime(then time.Time) string { now := time.Now() - ago := now.Sub(then) - if ago < time.Minute { + if ago := now.Sub(then); ago < time.Minute { return "just now" } else if ago < humanize.Week { return humanize.CustomRelTime(then, now, "ago", "from now", magnitudes) diff --git a/ui/pager.go b/ui/pager.go index 4b50820..dea001b 100644 --- a/ui/pager.go +++ b/ui/pager.go @@ -98,8 +98,10 @@ var ( Foreground(fuschia) ) -type contentRenderedMsg string -type noteSavedMsg *charm.Markdown +type ( + contentRenderedMsg string + noteSavedMsg *charm.Markdown +) type pagerState int @@ -227,10 +229,10 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) { switch m.state { case pagerStateSetNote: switch msg.String() { - case "esc": + case keyEsc: m.state = pagerStateBrowse return m, nil - case "enter": + case keyEnter: var cmd tea.Cmd if m.textInput.Value() != m.currentDocument.Note { // don't update if the note didn't change m.currentDocument.Note = m.textInput.Value() // update optimistically @@ -242,7 +244,7 @@ func (m pagerModel) update(msg tea.Msg) (pagerModel, tea.Cmd) { } default: switch msg.String() { - case "q", "esc": + case "q", keyEsc: if m.state != pagerStateBrowse { m.state = pagerStateBrowse return m, nil @@ -416,10 +418,9 @@ func (m pagerModel) statusBarView(b *strings.Builder) { maxPercent float64 = 1.0 percentToStringMagnitude float64 = 100.0 ) - var ( - isStashed bool = m.currentDocument.docType == StashedDoc || m.currentDocument.docType == ConvertedDoc - showStatusMessage bool = m.state == pagerStateStatusMessage - ) + + isStashed := m.currentDocument.docType == StashedDoc || m.currentDocument.docType == ConvertedDoc + showStatusMessage := m.state == pagerStateStatusMessage // Logo logo := glowLogoView(" Glow ") diff --git a/ui/stash.go b/ui/stash.go index 2d0dfa3..e4ba115 100644 --- a/ui/stash.go +++ b/ui/stash.go @@ -3,8 +3,8 @@ package ui import ( "errors" "fmt" - "io/ioutil" "log" + "os" "sort" "strings" "time" @@ -55,9 +55,11 @@ var ( // MSG -type deletedStashedItemMsg int -type filteredMarkdownMsg []*markdown -type fetchedMarkdownMsg *markdown +type ( + deletedStashedItemMsg int + filteredMarkdownMsg []*markdown + fetchedMarkdownMsg *markdown +) type markdownFetchFailedMsg struct { err error @@ -218,15 +220,6 @@ func (m stashModel) loadingDone() bool { return m.loaded.Equals(m.common.cfg.DocumentTypes.Difference(ConvertedDoc)) } -func (m stashModel) hasSection(key sectionKey) bool { - for _, v := range m.sections { - if key == v.key { - return true - } - } - return false -} - func (m stashModel) currentSection() *section { return &m.sections[m.sectionIndex] } @@ -741,7 +734,7 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd { m.setCursor(m.paginator().ItemsOnPage(numDocs) - 1) // Clear filter (if applicable) - case "esc": + case keyEsc: if m.filterApplied() { m.resetFiltering() } @@ -769,7 +762,7 @@ func (m *stashModel) handleDocumentBrowsing(msg tea.Msg) tea.Cmd { m.updatePagination() // Open document - case "enter": + case keyEnter: m.hideStatusMessage() if numDocs == 0 { @@ -971,7 +964,6 @@ func (m *stashModel) handleDeleteConfirmation(msg tea.Msg) tea.Cmd { } switch md.docType { - case ConvertedDoc: // If the document was stashed in this session, convert it // back to it's original document type @@ -989,9 +981,7 @@ func (m *stashModel) handleDeleteConfirmation(msg tea.Msg) tea.Cmd { if err == nil { m.filteredMarkdowns = mds } - } - break } } @@ -1021,10 +1011,10 @@ func (m *stashModel) handleFiltering(msg tea.Msg) tea.Cmd { // Handle keys if msg, ok := msg.(tea.KeyMsg); ok { switch msg.String() { - case "esc": + case keyEsc: // Cancel filtering m.resetFiltering() - case "enter", "tab", "shift+tab", "ctrl+k", "up", "ctrl+j", "down": + case keyEnter, "tab", "shift+tab", "ctrl+k", "up", "ctrl+j", "down": m.hideStatusMessage() if len(m.markdowns) == 0 { @@ -1087,11 +1077,11 @@ func (m *stashModel) handleNoteInput(msg tea.Msg) tea.Cmd { if msg, ok := msg.(tea.KeyMsg); ok { switch msg.String() { - case "esc": + case keyEsc: // Cancel note m.noteInput.Reset() m.selectionState = selectionIdle - case "enter": + case keyEnter: // Set new note md := m.selectedMarkdown() @@ -1235,7 +1225,7 @@ func (m stashModel) headerView() string { stashedCount := m.countMarkdowns(StashedDoc) + m.countMarkdowns(ConvertedDoc) newsCount := m.countMarkdowns(NewsDoc) - var sections []string + var sections []string //nolint:prealloc // Filter results if m.filterState == filtering { @@ -1384,7 +1374,7 @@ func loadRemoteMarkdown(cc *charm.Client, md *markdown) tea.Cmd { newMD, err := fetchMarkdown(cc, md.ID, md.docType) if err != nil { if debug { - log.Printf("error loading %s markdown (ID %s, Note: '%s'): %v", md.docType, md.ID, md.Note, err) + log.Printf("error loading %s markdown (ID %d, Note: '%s'): %v", md.docType, md.ID, md.Note, err) } return markdownFetchFailedMsg{ err: err, @@ -1406,7 +1396,7 @@ func loadLocalMarkdown(md *markdown) tea.Cmd { return errMsg{errors.New("could not load file: missing path")} } - data, err := ioutil.ReadFile(md.localPath) + data, err := os.ReadFile(md.localPath) if err != nil { if debug { log.Println("error reading local markdown:", err) diff --git a/ui/stashhelp.go b/ui/stashhelp.go index a53dfca..8ab9c22 100644 --- a/ui/stashhelp.go +++ b/ui/stashhelp.go @@ -240,11 +240,9 @@ func (m stashModel) miniHelpView(entries ...string) string { } func (m stashModel) fullHelpView(groups ...[]string) string { - var ( - columns []helpColumn - tallestCol int - renderedCols [][]string // final rows grouped by column - ) + var tallestCol int + columns := make([]helpColumn, 0, len(groups)) + renderedCols := make([][]string, 0, len(groups)) // final rows grouped by column // Get key/value pairs for _, g := range groups { diff --git a/ui/styles.go b/ui/styles.go index fe7c0ff..3b3ccca 100644 --- a/ui/styles.go +++ b/ui/styles.go @@ -1,8 +1,6 @@ package ui -import ( - . "github.com/charmbracelet/lipgloss" -) +import . "github.com/charmbracelet/lipgloss" //nolint: revive // Colors. var ( @@ -33,6 +31,7 @@ var ( ) // Ulimately, we'll transition to named styles. +// nolint:deadcode,unused,varcheck var ( normalFg = NewStyle().Foreground(normal).Render dimNormalFg = NewStyle().Foreground(normalDim).Render @@ -3,7 +3,6 @@ package ui import ( "errors" "fmt" - "io/ioutil" "log" "os" "path/filepath" @@ -13,7 +12,6 @@ import ( tea "github.com/charmbracelet/bubbletea" "github.com/charmbracelet/charm" "github.com/charmbracelet/charm/keygen" - "github.com/charmbracelet/charm/ui/keygen" "github.com/charmbracelet/glow/utils" "github.com/muesli/gitcha" te "github.com/muesli/termenv" @@ -57,9 +55,9 @@ func NewProgram(cfg Config) *tea.Program { } config = cfg opts := []tea.ProgramOption{tea.WithAltScreen()} - if cfg.EnableMouse { - opts = append(opts, tea.WithMouseCellMotion()) - } + if cfg.EnableMouse { + opts = append(opts, tea.WithMouseCellMotion()) + } return tea.NewProgram(newModel(cfg), opts...) } @@ -77,6 +75,7 @@ type ( ch chan gitcha.SearchResult } ) + type ( foundLocalFileMsg gitcha.SearchResult localFileSearchFinished struct{} @@ -662,9 +661,8 @@ func stashDocument(cc *charm.Client, md markdown) tea.Cmd { // then we'll stash it anyway. if len(md.Body) == 0 { switch md.docType { - case LocalDoc: - data, err := ioutil.ReadFile(md.localPath) + data, err := os.ReadFile(md.localPath) if err != nil { if debug { log.Println("error loading document body for stashing:", err) diff --git a/utils/utils.go b/utils/utils.go index 8146f82..fe38c13 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -7,6 +7,7 @@ import ( "github.com/mitchellh/go-homedir" ) +// RemoveFrontmatter removes the front matter header of a markdown file. func RemoveFrontmatter(content []byte) []byte { if frontmatterBoundaries := detectFrontmatter(content); frontmatterBoundaries[0] == 0 { return content[frontmatterBoundaries[1]:] |