Skip to content

Commit

Permalink
feat: remove globals from stats package
Browse files Browse the repository at this point in the history
Signed-off-by: Brian McGee <[email protected]>
  • Loading branch information
brianmcgee committed Oct 9, 2024
1 parent 6824497 commit 48d4b4b
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 173 deletions.
6 changes: 3 additions & 3 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func putEntry(bucket *bolt.Bucket, path string, entry *Entry) error {

// ChangeSet is used to walk a filesystem, starting at root, and outputting any new or changed paths using pathsCh.
// It determines if a path is new or has changed by comparing against cache entries.
func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.File) error {
func ChangeSet(ctx context.Context, statz *stats.Stats, walker walk.Walker, filesCh chan<- *walk.File) error {
start := time.Now()

defer func() {
Expand Down Expand Up @@ -236,13 +236,13 @@ func ChangeSet(ctx context.Context, walker walk.Walker, filesCh chan<- *walk.Fil

changedOrNew := cached == nil || !(cached.Modified == file.Info.ModTime() && cached.Size == file.Info.Size())

stats.Add(stats.Traversed, 1)
statz.Add(stats.Traversed, 1)
if !changedOrNew {
// no change
return nil
}

stats.Add(stats.Emitted, 1)
statz.Add(stats.Emitted, 1)

// pass on the path
select {
Expand Down
44 changes: 27 additions & 17 deletions cmd/format/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,14 @@ const (

var ErrFailOnChange = errors.New("unexpected changes detected, --fail-on-change is enabled")

func Run(v *viper.Viper, cmd *cobra.Command, paths []string) error {
func Run(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, paths []string) error {
cmd.SilenceUsage = true

cfg, err := config.FromViper(v)
if err != nil {
return fmt.Errorf("failed to load config: %w", err)
}

// initialise stats collection
stats.Init()

if cfg.CI {
log.Info("ci mode enabled")

Expand Down Expand Up @@ -191,10 +188,10 @@ func Run(v *viper.Viper, cmd *cobra.Command, paths []string) error {
processedCh := make(chan *format.Task, cap(filesCh))

// start concurrent processing tasks in reverse order
eg.Go(updateCache(ctx, cfg, processedCh))
eg.Go(detectFormatted(ctx, cfg, formattedCh, processedCh))
eg.Go(applyFormatters(ctx, cfg, globalExcludes, formatters, filesCh, formattedCh))
eg.Go(walkFilesystem(ctx, cfg, paths, filesCh))
eg.Go(updateCache(ctx, cfg, statz, processedCh))
eg.Go(detectFormatted(ctx, cfg, statz, formattedCh, processedCh))
eg.Go(applyFormatters(ctx, cfg, statz, globalExcludes, formatters, filesCh, formattedCh))
eg.Go(walkFilesystem(ctx, cfg, statz, paths, filesCh))

// wait for everything to complete
return eg.Wait()
Expand All @@ -203,6 +200,7 @@ func Run(v *viper.Viper, cmd *cobra.Command, paths []string) error {
func walkFilesystem(
ctx context.Context,
cfg *config.Config,
statz *stats.Stats,
paths []string,
filesCh chan *walk.File,
) func() error {
Expand Down Expand Up @@ -258,8 +256,8 @@ func walkFilesystem(
case <-ctx.Done():
return ctx.Err()
default:
stats.Add(stats.Traversed, 1)
stats.Add(stats.Emitted, 1)
statz.Add(stats.Traversed, 1)
statz.Add(stats.Emitted, 1)
filesCh <- file
return nil
}
Expand All @@ -268,7 +266,7 @@ func walkFilesystem(

// otherwise we pass the walker to the cache and have it generate files for processing based on whether or not
// they have been added/changed since the last invocation
if err = cache.ChangeSet(ctx, walker, filesCh); err != nil {
if err = cache.ChangeSet(ctx, statz, walker, filesCh); err != nil {
return fmt.Errorf("failed to generate change set: %w", err)
}
return nil
Expand All @@ -279,6 +277,7 @@ func walkFilesystem(
func applyFormatters(
ctx context.Context,
cfg *config.Config,
statz *stats.Stats,
globalExcludes []glob.Glob,
formatters map[string]*format.Formatter,
filesCh chan *walk.File,
Expand Down Expand Up @@ -389,7 +388,7 @@ func applyFormatters(
}
} else {
// record the match
stats.Add(stats.Matched, 1)
statz.Add(stats.Matched, 1)
// create a new format task, add it to a batch based on its batch key and try to apply if the batch is full
task := format.NewTask(file, matches)
tryApply(&task)
Expand All @@ -409,7 +408,13 @@ func applyFormatters(
}
}

func detectFormatted(ctx context.Context, cfg *config.Config, formattedCh chan *format.Task, processedCh chan *format.Task) func() error {
func detectFormatted(
ctx context.Context,
cfg *config.Config,
statz *stats.Stats,
formattedCh chan *format.Task,
processedCh chan *format.Task,
) func() error {
return func() error {
defer func() {
// close formatted channel
Expand Down Expand Up @@ -438,7 +443,7 @@ func detectFormatted(ctx context.Context, cfg *config.Config, formattedCh chan *

if changed {
// record the change
stats.Add(stats.Formatted, 1)
statz.Add(stats.Formatted, 1)

logMethod := log.Debug
if cfg.FailOnChange {
Expand Down Expand Up @@ -466,7 +471,12 @@ func detectFormatted(ctx context.Context, cfg *config.Config, formattedCh chan *
}
}

func updateCache(ctx context.Context, cfg *config.Config, processedCh chan *format.Task) func() error {
func updateCache(
ctx context.Context,
cfg *config.Config,
statz *stats.Stats,
processedCh chan *format.Task,
) func() error {
return func() error {
// used to batch updates for more efficient txs
batch := make([]*format.Task, 0, BatchSize)
Expand Down Expand Up @@ -544,13 +554,13 @@ func updateCache(ctx context.Context, cfg *config.Config, processedCh chan *form
}

// if fail on change has been enabled, check that no files were actually formatted, throwing an error if so
if cfg.FailOnChange && stats.Value(stats.Formatted) != 0 {
if cfg.FailOnChange && statz.Value(stats.Formatted) != 0 {
return ErrFailOnChange
}

// print stats to stdout unless we are processing stdin and printing the results to stdout
if !cfg.Stdin {
stats.Print()
statz.Print()
}

return nil
Expand Down
15 changes: 10 additions & 5 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"os"
"path/filepath"

"github.com/numtide/treefmt/stats"

"github.com/charmbracelet/log"
"github.com/numtide/treefmt/build"
"github.com/numtide/treefmt/cmd/format"
Expand All @@ -14,7 +16,7 @@ import (
"github.com/spf13/viper"
)

func NewRoot() *cobra.Command {
func NewRoot() (*cobra.Command, *stats.Stats) {
var (
treefmtInit bool
configFile string
Expand All @@ -26,13 +28,16 @@ func NewRoot() *cobra.Command {
cobra.CheckErr(fmt.Errorf("failed to create viper instance: %w", err))
}

// create a new stats instance
statz := stats.New()

// create out root command
cmd := &cobra.Command{
Use: "treefmt <paths...>",
Short: "One CLI to format your repo",
Version: build.Version,
RunE: func(cmd *cobra.Command, args []string) error {
return runE(v, cmd, args)
return runE(v, &statz, cmd, args)
},
}

Expand Down Expand Up @@ -62,10 +67,10 @@ func NewRoot() *cobra.Command {
// conforms with https://github.com/numtide/prj-spec/blob/main/PRJ_SPEC.md
cobra.CheckErr(v.BindPFlag("prj_root", fs.Lookup("tree-root")))

return cmd
return cmd, &statz
}

func runE(v *viper.Viper, cmd *cobra.Command, args []string) error {
func runE(v *viper.Viper, statz *stats.Stats, cmd *cobra.Command, args []string) error {
flags := cmd.Flags()

// change working directory if required
Expand Down Expand Up @@ -123,5 +128,5 @@ func runE(v *viper.Viper, cmd *cobra.Command, args []string) error {
}

// format
return format.Run(v, cmd, args)
return format.Run(v, statz, cmd, args)
}
Loading

0 comments on commit 48d4b4b

Please sign in to comment.