add options

This commit is contained in:
TJ Holowaychuk
2017-11-18 17:29:56 -08:00
parent 5ae070e74e
commit 68b8592588
2 changed files with 20 additions and 8 deletions
+3 -5
View File
@@ -22,15 +22,13 @@ func main() {
start := time.Now()
if dir == "" {
dir = "node_modules"
}
if *debug {
log.SetLevel(log.DebugLevel)
}
stats, err := prune.Prune(dir)
p := prune.New(prune.WithDir(dir))
stats, err := p.Prune()
if err != nil {
log.Fatalf("error: %s", err)
}
+17 -3
View File
@@ -22,9 +22,23 @@ type Pruner struct {
Log log.Interface
}
// Prune dir of unnecessary files.
func Prune(dir string) (*Stats, error) {
return Pruner{dir, log.Log}.Prune()
// Option function.
type Option func(*Pruner)
// New with the given options.
func New(options ...Option) *Pruner {
v := &Pruner{Dir: "node_modules", Log: log.Log}
for _, o := range options {
o(v)
}
return v
}
// WithDir option.
func WithDir(s string) Option {
return func(v *Pruner) {
v.Dir = s
}
}
// Prune performs the pruning.