Skip to content

Commit

Permalink
add more rules to golangci
Browse files Browse the repository at this point in the history
and correct all linting errors
  • Loading branch information
chmouel committed Oct 17, 2024
1 parent 2c6693d commit 692c30a
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 25 deletions.
108 changes: 108 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
linters-settings:
gocritic:
disabled-checks:
- unlambda
gofumpt:
extra-rules: true
linters:
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
#- containedctx
#- contextcheck
#- cyclop
- decorder
#- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- errorlint
# - execinquery
- exhaustive
#- exhaustruct
# - forbidigo
- forcetypeassert
#- funlen
#- gci
- ginkgolinter
- gocheckcompilerdirectives
#- gochecknoglobals
- gochecknoinits
- gochecksumtype
#- gocognit
#- goconst
- gocritic
#- gocyclo
- godot
#- godox
#- goerr113
#- gofmt
- gofumpt
- goheader
- goimports
#- gomnd
#- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
- gosimple
- gosmopolitan
- govet
- grouper
- importas
#- inamedparam
#- interfacebloat
#- ireturn
#- lll
- loggercheck
#- maintidx
- makezero
- mirror
- misspell
#- musttag
- nakedret
#- nestif
- nilerr
#- nilnil
#- nlreturn
- noctx
#- nolintlint
#- nonamedreturns
- nosprintfhostport
#- paralleltest
#- perfsprint
- prealloc
- predeclared
- promlinter
- protogetter
- reassign
- revive
#- rowserrcheck
- sloglint
#- sqlclosecheck
- staticcheck
- stylecheck
- tagalign
#- tagliatelle
- tenv
- testableexamples
# - testifylint
#- testpackage
#- thelper
- tparallel
#- unconvert
- unparam
- unused
- usestdlibvars
#- varnamelen
#- wastedassign
- whitespace
#- wrapcheck
#- wsl
- zerologlint
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
OUTPUT_DIR = bin
NAME := clipman
GOLANGCI_LINT := $(shell command -v golangci-lint 2> /dev/null)
GOFUMPT := $(shell command -v gofumpt 2> /dev/null)

all: lint $(OUTPUT_DIR)/$(NAME)

Expand All @@ -14,3 +15,9 @@ $(OUTPUT_DIR)/$(NAME): *.go mkdir
lint: $(GOLANGCI_LINT)
@echo "linting..."
@$(GOLANGCI_LINT) run

fumpt:
@find . -name '*.go'|xargs -P4 $(GOFUMPT) -w -extra


.PHONY: fumpt lint mkdir all
49 changes: 38 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
Expand Down Expand Up @@ -44,9 +43,9 @@ var (
clearAll = clearer.Flag("all", "Remove all items").Short('a').Default("false").Bool()
clearEsc = clearer.Flag("print0", "Separate items using NULL; recommended if your tool supports --read0 or similar").Default("false").Bool()

_ = app.Command("show-history", "Show all items from history")
_ = app.Command("restore", "Serve the last recorded item from history")
_ = app.Command("show-history", "Show all items from history")

_ = app.Command("restore", "Serve the last recorded item from history")
)

func main() {
Expand Down Expand Up @@ -101,8 +100,13 @@ func main() {
serveTxt(history[len(history)-1])
case "show-history":
if len(history) != 0 {
urlsJson, _ := json.Marshal(history)
fmt.Println(string(urlsJson))
urlsJSON, err := json.Marshal(history)
if err != nil {
fmt.Printf("Error marshalling history: %s\n", err.Error())
return
}

fmt.Println(string(urlsJSON))
return
}
fmt.Println("Nothing to show")
Expand Down Expand Up @@ -177,14 +181,14 @@ func getHistory(rawPath string) (string, []string, error) {

// read history if it exists
var history []string
b, err := ioutil.ReadFile(histfile)
b, err := os.ReadFile(histfile)
if err != nil {
if !os.IsNotExist(err) {
return "", nil, fmt.Errorf("failure reading history file: %s", err)
return "", nil, fmt.Errorf("failure reading history file: %w", err)
}
} else {
if err := json.Unmarshal(b, &history); err != nil {
return "", nil, fmt.Errorf("failure parsing history: %s", err)
return "", nil, fmt.Errorf("failure parsing history: %w", err)
}
}

Expand Down Expand Up @@ -217,7 +221,20 @@ func serveTxt(s string) {
}
}

// modified from standard lib to not drop \r and \n
// scanLines is a custom implementation of a split function for a bufio.Scanner.
// It has been modified from the standard library version to ensure that carriage return (\r)
// and newline (\n) characters are not dropped. This is important for maintaining the integrity
// of the input data, especially when dealing with text files or streams where these characters
// are significant.
//
// Parameters:
// - data: The byte slice to be scanned.
// - atEOF: A boolean indicating if the end of the file has been reached.
//
// Returns:
// - advance: The number of bytes to advance the input.
// - token: The next token to return to the user.
// - err: Any error encountered during scanning.
func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
Expand Down Expand Up @@ -245,7 +262,17 @@ func scanLines(data []byte, atEOF bool) (advance int, token []byte, err error) {
return 0, nil, nil
}

// dropCR drops a terminal \r from the data. Modified from Go's Stdlib
// dropCR drops a terminal \r from the data. This function has been modified from Go's
// standard library to ensure that carriage return (\r) characters are properly handled.
// It checks if the data ends with a newline (\n) and removes the preceding carriage return (\r)
// if present. This is useful for processing text data that may have different line ending
// conventions (e.g., Windows vs. Unix).
//
// Parameters:
// - data: The byte slice from which the terminal \r should be dropped.
//
// Returns:
// - A new byte slice with the terminal \r removed, if it was present.
func dropCR(data []byte) []byte {
orig := data

Expand Down
3 changes: 1 addition & 2 deletions notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func smartLog(message, urgency string, alert bool) {
}
}

func notify(message string, urgency string) error {
func notify(message, urgency string) error {
var timeout time.Duration
switch urgency {
// cases accepted by notify-send: low, normal, critical
Expand All @@ -40,5 +40,4 @@ func notify(message string, urgency string) error {
args := []string{"-a", "Clipman", "-u", urgency, "-t", millisec, message}

return exec.Command("notify-send", args...).Run()

}
8 changes: 4 additions & 4 deletions selector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/kballard/go-shellquote"
)

func selector(data []string, max int, tool, prompt, toolArgs string, null, errorOnNoSelection bool) (string, error) {
func selector(data []string, maxChar int, tool, prompt, toolArgs string, null, errorOnNoSelection bool) (string, error) {
if len(data) == 0 {
return "", errors.New("nothing to show: no data available")
}
Expand Down Expand Up @@ -39,15 +39,15 @@ func selector(data []string, max int, tool, prompt, toolArgs string, null, error
"-fn",
"-misc-dejavu sans mono-medium-r-normal--17-120-100-100-m-0-iso8859-16",
"-l",
strconv.Itoa(max),
strconv.Itoa(maxChar),
}
case "bemenu":
args = []string{"bemenu", "--prompt", prompt, "--list", strconv.Itoa(max)}
args = []string{"bemenu", "--prompt", prompt, "--list", strconv.Itoa(maxChar)}
case "rofi":
args = []string{
"rofi", "-p", prompt, "-dmenu",
"-lines",
strconv.Itoa(max),
strconv.Itoa(maxChar),
}
case "wofi":
args = []string{"wofi", "-p", prompt, "--cache-file", "/dev/null", "--dmenu"}
Expand Down
16 changes: 8 additions & 8 deletions storer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)

func store(text string, history []string, histfile string, max int, persist bool) error {
func store(text string, history []string, histfile string, maxChar int, persist bool) error {
if text == "" {
return nil
}
Expand All @@ -22,9 +22,9 @@ func store(text string, history []string, histfile string, max int, persist bool

// drop oldest items that exceed max list size
// if max = 0, we allow infinite history; NOTE: users should NOT rely on this behaviour as we might change it without notice
if max != 0 && l >= max {
if maxChar != 0 && l >= maxChar {
// usually just one item, but more if we suddenly reduce our --max-items
history = history[l-max+1:]
history = history[l-maxChar+1:]
}

// remove duplicates
Expand All @@ -35,7 +35,7 @@ func store(text string, history []string, histfile string, max int, persist bool

// dump history to file so that other apps can query it
if err := write(history, histfile); err != nil {
return fmt.Errorf("error writing history: %s", err)
return fmt.Errorf("error writing history: %w", err)
}

// make the copy buffer available to all applications,
Expand All @@ -47,7 +47,7 @@ func store(text string, history []string, histfile string, max int, persist bool
return nil
}

// filter removes all occurrences of text
// filter removes all occurrences of text.
func filter(slice []string, text string) []string {
var filtered []string
for _, s := range slice {
Expand All @@ -59,12 +59,12 @@ func filter(slice []string, text string) []string {
return filtered
}

// write dumps history to json file
// write dumps history to json file.
func write(history []string, histfile string) error {
b, err := json.Marshal(history)
if err != nil {
return err
}

return ioutil.WriteFile(histfile, b, 0600)
return os.WriteFile(histfile, b, 0o600)
}

0 comments on commit 692c30a

Please sign in to comment.