Skip to content

Commit

Permalink
feat: more stringent golangci-lint rules
Browse files Browse the repository at this point in the history
Applies more stringent `golangci-lint` rules to the repository, borrowed from https://github.com/nix-community/go-nix.

Also fixes a bug where we were not returning errors when calling `walk.Reader.Read()` in `cmd/format`

Signed-off-by: Brian McGee <[email protected]>
Co-authored-by: Jeremy Fleischman <[email protected]>
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee and jfly committed Oct 16, 2024
1 parent 7b6fc1b commit fc88f98
Show file tree
Hide file tree
Showing 25 changed files with 285 additions and 191 deletions.
39 changes: 39 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# taken from https://github.com/nix-community/go-nix/blob/main/.golangci.yml
linters:
enable:
- errname
- exhaustive
- gci
- gochecknoglobals
- gochecknoinits
- goconst
- godot
- gofumpt
- goheader
- goimports
- gosec
- importas
- ireturn
- lll
- makezero
- misspell
- nakedret
- nestif
- nilerr
- nilnil
- nlreturn
- noctx
- nolintlint
- prealloc
- predeclared
- revive
- rowserrcheck
- stylecheck
- tagliatelle
- tenv
- testpackage
- unconvert
- unparam
- wastedassign
- whitespace
- wsl
4 changes: 2 additions & 2 deletions build/build.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package build

var (
Name = "treefmt"
Version = "v0.0.1+dev"
Name = "treefmt" //nolint:gochecknoglobals
Version = "v0.0.1+dev" //nolint:gochecknoglobals
)
30 changes: 15 additions & 15 deletions cmd/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ import (
"syscall"
"time"

"github.com/numtide/treefmt/walk/cache"
bolt "go.etcd.io/bbolt"

"github.com/charmbracelet/log"
"github.com/gobwas/glob"
"github.com/numtide/treefmt/config"
"github.com/numtide/treefmt/format"
"github.com/numtide/treefmt/stats"
"github.com/numtide/treefmt/walk"
"github.com/numtide/treefmt/walk/cache"
"github.com/spf13/cobra"
"github.com/spf13/viper"
bolt "go.etcd.io/bbolt"
"golang.org/x/sync/errgroup"

"mvdan.cc/sh/v3/expand"
)

Expand Down Expand Up @@ -60,20 +58,23 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
// Wait until we tick over into the next second before processing to ensure our EPOCH level modtime comparisons
// for change detection are accurate.
// This can fail in CI between checkout and running treefmt if everything happens too quickly.
// For humans, the second level precision should not be a problem as they are unlikely to run treefmt in sub-second succession.
// For humans, the second level precision should not be a problem as they are unlikely to run treefmt in
// sub-second succession.
<-time.After(time.Until(startAfter))
}

// cpu profiling
if cfg.CpuProfile != "" {
cpuProfile, err := os.Create(cfg.CpuProfile)
if cfg.CPUProfile != "" {
cpuProfile, err := os.Create(cfg.CPUProfile)
if err != nil {
return fmt.Errorf("failed to open file for writing cpu profile: %w", err)
} else if err = pprof.StartCPUProfile(cpuProfile); err != nil {
return fmt.Errorf("failed to start cpu profile: %w", err)
}

defer func() {
pprof.StopCPUProfile()

if err := cpuProfile.Close(); err != nil {
log.Errorf("failed to close cpu profile: %v", err)
}
Expand All @@ -99,6 +100,7 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)

if errors.Is(err, format.ErrCommandNotFound) && cfg.AllowMissingFormatter {
log.Debugf("formatter command not found: %v", name)

continue
} else if err != nil {
return fmt.Errorf("%w: failed to initialise formatter: %v", err, name)
Expand All @@ -110,8 +112,8 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)

var db *bolt.DB

// open the db unless --no-cache was specified
if !cfg.NoCache {
// open the db
db, err = cache.Open(cfg.TreeRoot)
if err != nil {
return fmt.Errorf("failed to open cache: %w", err)
Expand All @@ -123,7 +125,9 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
log.Errorf("failed to close cache: %v", err)
}
}()
}

if db != nil {
// clear the cache if desired
if cfg.ClearCache {
if err = cache.Clear(db); err != nil {
Expand Down Expand Up @@ -226,9 +230,7 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
break
} else if err != nil {
// something went wrong
log.Errorf("failed to read files: %v", err)
cancel()
break
return fmt.Errorf("failed to read files: %w", err)
}
}

Expand All @@ -243,7 +245,6 @@ func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string)
return reader.Close()
}

// applyFormatters
func applyFormatters(
ctx context.Context,
cfg *config.Config,
Expand Down Expand Up @@ -275,7 +276,6 @@ func applyFormatters(

// process the batch if it's full, or we've been asked to flush partial batches
if flush || len(batch) == BatchSize {

// copy the batch as we re-use it for the next batch
tasks := make([]*format.Task, len(batch))
copy(tasks, batch)
Expand All @@ -287,6 +287,7 @@ func applyFormatters(
formatters := tasks[0].Formatters

var formatErrors []error

for idx := range formatters {
if err := formatters[idx].Apply(ctx, tasks); err != nil {
formatErrors = append(formatErrors, err)
Expand Down Expand Up @@ -330,7 +331,6 @@ func applyFormatters(

// iterate the file channel
for file := range filesCh {

// a list of formatters that match this file
var matches []*format.Formatter

Expand Down Expand Up @@ -392,6 +392,7 @@ func applyFormatters(
if err := fg.Wait(); err != nil {
return fmt.Errorf("formatting failure: %w", err)
}

return nil
}
}
Expand All @@ -406,7 +407,6 @@ func postProcessing(
LOOP:
for {
select {

// detect ctx cancellation
case <-ctx.Done():
return ctx.Err()
Expand Down
4 changes: 3 additions & 1 deletion cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
var initBytes []byte

func Run() error {
if err := os.WriteFile("treefmt.toml", initBytes, 0o644); err != nil {
if err := os.WriteFile("treefmt.toml", initBytes, 0o600); err != nil {
return fmt.Errorf("failed to write treefmt.toml: %w", err)
}

fmt.Printf("Generated treefmt.toml. Now it's your turn to edit it.\n")

return nil
}
17 changes: 12 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ import (
"os"
"path/filepath"

"github.com/numtide/treefmt/stats"

"github.com/charmbracelet/log"
"github.com/numtide/treefmt/build"
"github.com/numtide/treefmt/cmd/format"
_init "github.com/numtide/treefmt/cmd/init"
"github.com/numtide/treefmt/config"
"github.com/numtide/treefmt/stats"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -33,7 +32,7 @@ func NewRoot() (*cobra.Command, *stats.Stats) {

// create out root command
cmd := &cobra.Command{
Use: "treefmt <paths...>",
Use: fmt.Sprintf("%s <paths...>", build.Name),
Short: "One CLI to format your repo",
Version: build.Version,
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -55,8 +54,15 @@ func NewRoot() (*cobra.Command, *stats.Stats) {
cmd.HelpTemplate()

// add a couple of special flags which don't have a corresponding entry in treefmt.toml
fs.StringVar(&configFile, "config-file", "", "Load the config file from the given path (defaults to searching upwards for treefmt.toml or .treefmt.toml).")
fs.BoolVarP(&treefmtInit, "init", "i", false, "Create a treefmt.toml file in the current directory.")
fs.StringVar(
&configFile, "config-file", "",
"Load the config file from the given path (defaults to searching upwards for treefmt.toml or "+
".treefmt.toml).",
)
fs.BoolVarP(
&treefmtInit, "init", "i", false,
"Create a treefmt.toml file in the current directory.",
)

// bind our command's flags to viper
if err := v.BindPFlags(fs); err != nil {
Expand Down Expand Up @@ -110,6 +116,7 @@ func runE(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, args []string)

// read in the config
v.SetConfigFile(configFile)

if err := v.ReadInConfig(); err != nil {
cobra.CheckErr(fmt.Errorf("failed to read config file '%s': %w", configFile, err))
}
Expand Down
Loading

0 comments on commit fc88f98

Please sign in to comment.