From 2a8e9d54e8e4c3f16c9a409bc9b8036636bd96de Mon Sep 17 00:00:00 2001 From: TJ Holowaychuk Date: Sat, 18 Nov 2017 17:18:26 -0800 Subject: [PATCH] refactor into pkg --- cmd/node-prune/main.go | 53 ++------------------------------ prune.go | 68 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+), 50 deletions(-) create mode 100644 prune.go diff --git a/cmd/node-prune/main.go b/cmd/node-prune/main.go index 140bee6..26e9993 100644 --- a/cmd/node-prune/main.go +++ b/cmd/node-prune/main.go @@ -2,15 +2,13 @@ package main import ( "flag" - "os" - "path/filepath" - "sync/atomic" "time" "github.com/apex/log" "github.com/apex/log/handlers/text" "github.com/dustin/go-humanize" - "github.com/pkg/errors" + + "github.com/tj/node-prune" ) func init() { @@ -32,7 +30,7 @@ func main() { log.SetLevel(log.DebugLevel) } - stats, err := prune(dir) + stats, err := prune.Prune(dir) if err != nil { log.Fatalf("error: %s", err) } @@ -44,48 +42,3 @@ func main() { "duration": time.Since(start).Round(time.Millisecond).String(), }).Info("complete") } - -// Stats of the prune. -type Stats struct { - FilesTotal int64 - FilesRemoved int64 - SizeRemoved int64 -} - -// prune files in dir. -func prune(dir string) (*Stats, error) { - var stats Stats - - err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error { - if err != nil { - return err - } - - if info.IsDir() { - return nil - } - - atomic.AddInt64(&stats.FilesTotal, 1) - - if !prunable(path, info) { - return nil - } - - log.WithField("path", path).Debug("prune") - atomic.AddInt64(&stats.FilesRemoved, 1) - atomic.AddInt64(&stats.SizeRemoved, info.Size()) - if err := os.Remove(path); err != nil { - return errors.Wrap(err, "removing") - } - - return nil - }) - - return &stats, err -} - -// prunable returns true if the file should be pruned. -func prunable(path string, info os.FileInfo) bool { - ext := filepath.Ext(path) - return ext == ".ts" || ext == ".md" -} diff --git a/prune.go b/prune.go new file mode 100644 index 0000000..7565bce --- /dev/null +++ b/prune.go @@ -0,0 +1,68 @@ +// Package prune provides node_modules pruning of unnecessary files. +package prune + +import ( + "os" + "path/filepath" + "sync/atomic" + + "github.com/apex/log" + "github.com/pkg/errors" +) + +// Stats for a prune. +type Stats struct { + FilesTotal int64 + FilesRemoved int64 + SizeRemoved int64 +} + +// Pruner is a module pruner. +type Pruner struct { + Dir string + Log log.Interface +} + +// Prune dir of unnecessary files. +func Prune(dir string) (*Stats, error) { + return Pruner{dir, log.Log}.Prune() +} + +// Prune performs the pruning. +func (p Pruner) Prune() (*Stats, error) { + var stats Stats + + err := filepath.Walk(p.Dir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + + if info.IsDir() { + return nil + } + + atomic.AddInt64(&stats.FilesTotal, 1) + + if !prunable(path, info) { + return nil + } + + p.Log.WithField("path", path).Debug("prune") + atomic.AddInt64(&stats.FilesRemoved, 1) + atomic.AddInt64(&stats.SizeRemoved, info.Size()) + + if err := os.Remove(path); err != nil { + return errors.Wrap(err, "removing") + } + + return nil + }) + + return &stats, err +} + +// prunable returns true if the file should be pruned. +func prunable(path string, info os.FileInfo) bool { + ext := filepath.Ext(path) + return ext == ".ts" || ext == ".md" +}