From 97c9e0d14f041456debd28ac666daa5e8a182921 Mon Sep 17 00:00:00 2001 From: Matt Baer Date: Tue, 25 Feb 2020 22:53:11 -0500 Subject: [PATCH] Move work to `clean` subcommand --- cmd/cdr/clean.go | 35 +++++++++++++++++++++++++++++++++++ cmd/cdr/main.go | 40 +++++++++++++++------------------------- 2 files changed, 50 insertions(+), 25 deletions(-) create mode 100644 cmd/cdr/clean.go diff --git a/cmd/cdr/clean.go b/cmd/cdr/clean.go new file mode 100644 index 0000000..65251b5 --- /dev/null +++ b/cmd/cdr/clean.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "os" + "path/filepath" + "strings" + + "github.com/thebaer/cdr" + "github.com/urfave/cli" +) + +var cmdClean = cli.Command{ + Name: "clean", + Usage: "clean and organize files", + Action: cleanAction, +} + +func cleanAction(c *cli.Context) error { + wd, err := os.Getwd() + if err != nil { + return err + } + filepath.Walk(wd, func(path string, i os.FileInfo, err error) error { + if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") { + fName := i.Name() + trackName := cdr.RenameTrack(fName) + fmt.Println("Renaming", fName, "to", trackName) + os.Rename(fName, trackName) + } + + return nil + }) + return nil +} diff --git a/cmd/cdr/main.go b/cmd/cdr/main.go index a6cbd99..2d50c7f 100644 --- a/cmd/cdr/main.go +++ b/cmd/cdr/main.go @@ -1,37 +1,27 @@ package main import ( - "flag" - "fmt" "log" "os" - "path/filepath" - "strings" - "github.com/thebaer/cdr" + "github.com/urfave/cli" ) -var printUsage = func() { - fmt.Fprintf(os.Stderr, "usage: %s [optional flags] filename\n", os.Args[0]) - flag.PrintDefaults() -} - func main() { - flag.Usage = printUsage - flag.Parse() - - wd, err := os.Getwd() - if err != nil { - log.Fatal(err) + app := &cli.App{ + Name: "CD-R 700MB", + Usage: "A static mixtape site generator", + Version: "v1.0", + Flags: []cli.Flag{}, } - filepath.Walk(wd, func(path string, i os.FileInfo, err error) error { - if !i.IsDir() && !strings.HasPrefix(i.Name(), ".") { - fName := i.Name() - trackName := cdr.RenameTrack(fName) - fmt.Println("Renaming", fName, "to", trackName) - os.Rename(fName, trackName) - } - return nil - }) + app.Commands = []*cli.Command{ + &cmdClean, + } + + err := app.Run(os.Args) + if err != nil { + log.Println(err.Error()) + os.Exit(1) + } }