diff --git a/.golangci.yaml b/.golangci.yaml deleted file mode 100644 index 766ec2a..0000000 --- a/.golangci.yaml +++ /dev/null @@ -1,39 +0,0 @@ -linters: - enable: - - errcheck - - gosimple - - govet - - ineffassign - - staticcheck - - unused - - copyloopvar - - dupl - - err113 - - errname - - exptostd - - fatcontext - - funlen - - gocognit - - goconst - - gocritic - - gocyclo - - godot - - godox - - gosec - - perfsprint - - testifylint - -linters-settings: - testifylint: - enable-all: true - disable: - - require-error - gocognit: - min-complexity: 16 - gocyclo: - min-complexity: 15 - gocritic: - enable-all: true - settings: - hugeParam: - sizeThreshold: 255 diff --git a/Taskfile.yml b/Taskfile.yml index 6f2562e..fce2e25 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -14,7 +14,7 @@ tasks: desc: fmt, vet, and build deps: - fmt - - lint + - analyze - build-all fmt: @@ -24,12 +24,30 @@ tasks: cmds: - go fmt ./... - lint: - desc: Statically analyze code + vet: + desc: Vet go code sources: - '**/*.go' cmds: - - golangci-lint run + - go vet ./... + critic: + desc: Critique go code + sources: + - '**/*.go' + cmds: + - gocritic check ./... + staticcheck: + desc: Static check go code + sources: + - '**/*.go' + cmds: + - staticcheck ./... + analyze: + desc: Do static analysis + deps: + - vet + - critic + - staticcheck cmd-build: internal: true diff --git a/chill/chill.go b/chill/chill.go index 5e90044..e18bf39 100644 --- a/chill/chill.go +++ b/chill/chill.go @@ -42,20 +42,16 @@ func Chill(baseCtx context.Context) context.Context { return ret(ErrChilledOut) } - go runChill(current, cancel) + go func() { + for current >= MaxTemp && err == nil { + time.Sleep(Sleep) + current, err = getCurrentTemp() + } + if err == nil { + err = ErrChilledOut + } + cancel(err) + }() return ctx } - -func runChill(current int, cancel context.CancelCauseFunc) { - var err error - for current >= MaxTemp && err == nil { - time.Sleep(Sleep) - current, err = getCurrentTemp() - } - if err == nil { - err = ErrChilledOut - } - cancel(err) - -} diff --git a/cli/spin/spin.go b/cli/spin/spin.go index a3697a8..d5570fa 100644 --- a/cli/spin/spin.go +++ b/cli/spin/spin.go @@ -20,17 +20,17 @@ type errMsg error type textMessage string -func newModel(text string) *model { +func newModel(text string) model { s := spinner.New() s.Spinner = spinner.Dot - return &model{spinner: s, text: text} + return model{spinner: s, text: text} } -func (m *model) Init() tea.Cmd { +func (m model) Init() tea.Cmd { return m.spinner.Tick } -func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { +func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { switch msg := msg.(type) { case errMsg: m.err = msg @@ -47,7 +47,7 @@ func (m *model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } } -func (m *model) View() string { +func (m model) View() string { if m.err != nil { return m.err.Error() } diff --git a/cmd/test/main.go b/cmd/test/main.go deleted file mode 100644 index a58c715..0000000 --- a/cmd/test/main.go +++ /dev/null @@ -1,20 +0,0 @@ -package main - -import ( - e "codeberg.org/danjones000/utils/cli/err" - cli "codeberg.org/danjones000/utils/internal/cli/convids" - "fmt" - "os" -) - -func main() { - flags, err := cli.ParseFlags(os.Args[0], os.Args[1:]) - e.HandleErr(err) - if *cli.Help { - flags.Usage() - } - - fmt.Println("Help", *cli.Help) - fmt.Println("Loop", *cli.Loop) - fmt.Println("DryRun", *cli.DryRun) -} diff --git a/convids/logic.go b/convids/logic.go index 3786295..6d90c2b 100644 --- a/convids/logic.go +++ b/convids/logic.go @@ -10,9 +10,7 @@ import ( fp "path/filepath" "regexp" "strings" - "time" - "codeberg.org/danjones000/ezcache" "gopkg.in/yaml.v3" ) @@ -84,33 +82,22 @@ func processFile(input processInput) (processed bool, processError, cbError erro return } -func processSource(allFiles ezcache.Cache[string, []os.DirEntry], d *Data, s *Show, source string, stopOnError bool, cb ShowWalker) (count int, err error) { - files, filesErr := allFiles.Get(source) - if filesErr != nil { - return 0, filesErr - } - for _, file := range files { - pIn := processInput{file, source, s, d.Config.extRe, d.Config.Skip, cb} - processed, processErr, cbErr := processFile(pIn) - if processErr != nil { - return 0, processErr - } - if cbErr != nil && stopOnError { - return 0, cbErr - } +type fileMap map[string][]os.DirEntry - if errors.Is(processErr, ErrSkipped) || (!processed && processErr == nil && cbErr == nil) { - continue - } - if cbErr == nil && processed { - count++ - } - if cbErr != nil { - err = cbErr - } +func (fm fileMap) GetFiles(path string) []os.DirEntry { + files, ok := fm[path] + if ok { + return files + } + fm.ReadPath(path) + return fm[path] +} + +func (fm fileMap) ReadPath(path string) { + if _, ok := fm[path]; ok { return } - return + fm[path], _ = os.ReadDir(path) } func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err error) { @@ -122,10 +109,8 @@ func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err e cb = DryRun(os.Stdout) } count := 0 - allFiles, cacheErr := ezcache.New(os.ReadDir, 5*time.Minute) - if cacheErr != nil { - return err - } + allFiles := fileMap{} + var files []os.DirEntry if err != nil { return @@ -134,13 +119,32 @@ func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err e if len(s.Sources) == 0 { s.Sources = []string{d.Config.Source} } + showstart: for _, source := range s.Sources { - var c int - c, err = processSource(allFiles, d, s, source, stopOnError, cb) - if err != nil { - break + files = allFiles.GetFiles(source) + for _, file := range files { + pIn := processInput{file, source, s, d.Config.extRe, d.Config.Skip, cb} + processed, processErr, cbErr := processFile(pIn) + if !processed && processErr == nil && cbErr == nil { + continue + } + if errors.Is(processErr, ErrSkipped) { + continue + } + if processErr != nil { + return processErr + } + if cbErr != nil && stopOnError { + return cbErr + } + if cbErr == nil && processed { + count++ + } + if cbErr != nil { + err = cbErr + } + break showstart } - count += c } } if err == nil && count < 1 { @@ -161,9 +165,8 @@ func GetShow(ctx context.Context) ShowWalker { return GetShowWithIO(ctx, os.Stdin, os.Stdout, os.Stderr) } -func GetShowWithIO(ctx context.Context, stdin io.Reader, stdout, stderr io.Writer) ShowWalker { +func GetShowWithIO(ctx context.Context, stdin io.Reader, stdout io.Writer, stderr io.Writer) ShowWalker { return func(s *Show, path string) error { - //nolint:gosec // I don't care if this is using user input cmd := exec.CommandContext(ctx, "get-shows", s.Folder, path) cmd.Stdin = stdin cmd.Stdout = stdout diff --git a/convids/methods.go b/convids/methods.go index f71981a..e10a8a9 100644 --- a/convids/methods.go +++ b/convids/methods.go @@ -62,28 +62,21 @@ func (d *Data) AllShows(outputGroup GroupPrinter) iter.Seq[*Show] { return } for _, groupName := range d.Config.Groups { - if !d.yieldGroup(yield, outputGroup, groupName) { - return + group := (*d.Shows)[groupName] + if group == nil || len(*group) < 1 { + continue + } + if outputGroup != nil { + outputGroup(groupName, *group) + } + for sh := range group.All() { + if sh == nil { + continue + } + if !yield(sh) { + return + } } } } } - -func (d *Data) yieldGroup(yield func(*Show) bool, outputGroup GroupPrinter, groupName string) bool { - group := (*d.Shows)[groupName] - if group == nil || len(*group) < 1 { - return true - } - if outputGroup != nil { - outputGroup(groupName, *group) - } - for sh := range group.All() { - if sh == nil { - continue - } - if !yield(sh) { - return false - } - } - return true -} diff --git a/go.mod b/go.mod index 7dca67b..83e42d4 100644 --- a/go.mod +++ b/go.mod @@ -1,9 +1,8 @@ module codeberg.org/danjones000/utils -go 1.23.7 +go 1.23.1 require ( - codeberg.org/danjones000/ezcache v0.5.2 github.com/charmbracelet/bubbles v0.20.0 github.com/charmbracelet/bubbletea v1.1.1 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 8361db0..c826619 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -codeberg.org/danjones000/ezcache v0.5.2 h1:msWRwbLj+HHCU3dnFbsdedCRuhyPQCOFbT9EgjwN9KM= -codeberg.org/danjones000/ezcache v0.5.2/go.mod h1:wLTvTXfXxaEDUS9dOILqQmhAlB/9lUZCC2yB5CyGBfk= github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= github.com/charmbracelet/bubbles v0.20.0 h1:jSZu6qD8cRQ6k9OMfR1WlM+ruM8fkPWkHvQWD9LIutE= @@ -12,8 +10,6 @@ github.com/charmbracelet/x/ansi v0.2.3 h1:VfFN0NUpcjBRd4DnKfRaIRo53KRgey/nhOoEqo github.com/charmbracelet/x/ansi v0.2.3/go.mod h1:dk73KoMTT5AX5BsX0KrqhsTqAnhZZoCBjs7dGWp4Ktw= github.com/charmbracelet/x/term v0.2.0 h1:cNB9Ot9q8I711MyZ7myUR5HFWL/lc3OpU8jZ4hwm0x0= github.com/charmbracelet/x/term v0.2.0/go.mod h1:GVxgxAbjUrmpvIINHIQnJJKpMlHiZ4cktEQCN6GWyF0= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4= github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM= github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= @@ -30,15 +26,11 @@ github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELU github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo= github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo= github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= -github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/internal/cli/convids/flags.go b/internal/cli/convids/flags.go index 88b2e3f..af2b2af 100644 --- a/internal/cli/convids/flags.go +++ b/internal/cli/convids/flags.go @@ -32,10 +32,7 @@ func ParseFlags(name string, args []string) (*pflag.FlagSet, error) { } if *DryRun { - err := flags.Set("loop", "false") - if err != nil { - return nil, err - } + flags.Set("loop", "false") } return flags, nil diff --git a/internal/cli/convids/walker.go b/internal/cli/convids/walker.go index b90b51f..a7dc85c 100644 --- a/internal/cli/convids/walker.go +++ b/internal/cli/convids/walker.go @@ -2,6 +2,7 @@ package convids import ( "context" + "fmt" "io" "codeberg.org/danjones000/utils/chill" @@ -17,7 +18,7 @@ func GetWalker(ctx context.Context, dryRun bool, in io.Reader, out, errOut io.Wr gs := conutils.GetShowWithIO(ctx, in, out, errOut) return func(s *conutils.Show, path string) error { ct := chill.Chill(ctx) - msg := "Waiting for CPU to cool, for " + path + msg := fmt.Sprintf("Waiting for CPU to cool, for %s", path) sp := spin.Spin(ct, msg) err := sp.Wait() if err != nil {