From 1264d437b3952976abc9caf3a77e12f2618be4b5 Mon Sep 17 00:00:00 2001 From: David Howden Date: Mon, 2 Apr 2018 10:54:32 +1000 Subject: [PATCH] cmd: tidy up flag usage --- cmd/check/main.go | 29 +++++++++++++---------------- cmd/tag/main.go | 18 +++++++----------- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/cmd/check/main.go b/cmd/check/main.go index 21c12a6..f8370ef 100644 --- a/cmd/check/main.go +++ b/cmd/check/main.go @@ -19,15 +19,6 @@ import ( "github.com/dhowden/tag" ) -var itlXML, path string -var sum bool - -func init() { - flag.StringVar(&itlXML, "itlXML", "", "iTunes Library Path") - flag.StringVar(&path, "path", "", "path to directory containing audio files") - flag.BoolVar(&sum, "sum", false, "compute the checksum of the audio file (doesn't work for .flac or .ogg yet)") -} - func decodeLocation(l string) (string, error) { u, err := url.ParseRequestURI(l) if err != nil { @@ -38,27 +29,33 @@ func decodeLocation(l string) (string, error) { return path, nil } +var ( + itlXML = flag.String("itlXML", "", "iTunes Library Path") + path = flag.String("path", "", "path to directory containing audio files") + sum = flag.Bool("sum", false, "compute the checksum of the audio file (doesn't work for .flac or .ogg yet)") +) + func main() { flag.Parse() - if itlXML == "" && path == "" || itlXML != "" && path != "" { + if *itlXML == "" && *path == "" || *itlXML != "" && *path != "" { fmt.Println("you must specify one of -itlXML or -path") flag.Usage() os.Exit(1) } var paths <-chan string - if itlXML != "" { + if *itlXML != "" { var err error - paths, err = walkLibrary(itlXML) + paths, err = walkLibrary(*itlXML) if err != nil { fmt.Println(err) os.Exit(1) } } - if path != "" { - paths = walkPath(path) + if *path != "" { + paths = walkPath(*path) } p := &processor{ @@ -100,7 +97,7 @@ func walkPath(root string) <-chan string { } func walkLibrary(path string) (<-chan string, error) { - f, err := os.Open(itlXML) + f, err := os.Open(*itlXML) if err != nil { return nil, err } @@ -177,7 +174,7 @@ func (p *processor) do(ch <-chan string) { p.decodingErrors[err.Error()]++ } - if sum { + if *sum { _, err = tf.Seek(0, os.SEEK_SET) if err != nil { fmt.Println("DIED:", path, "error seeking back to 0:", err) diff --git a/cmd/tag/main.go b/cmd/tag/main.go index 4755d54..61ba0db 100644 --- a/cmd/tag/main.go +++ b/cmd/tag/main.go @@ -17,22 +17,18 @@ import ( "github.com/dhowden/tag/mbz" ) -var raw bool -var extractMBZ bool - var usage = func() { fmt.Fprintf(os.Stderr, "usage: %s [optional flags] filename\n", os.Args[0]) flag.PrintDefaults() } -func init() { - flag.BoolVar(&raw, "raw", false, "show raw tag data") - flag.BoolVar(&extractMBZ, "mbz", false, "extract MusicBrainz tag data (if available)") - - flag.Usage = usage -} +var ( + raw = flag.Bool("raw", false, "show raw tag data") + extractMBZ = flag.Bool("mbz", false, "extract MusicBrainz tag data (if available)") +) func main() { + flag.Usage = usage flag.Parse() if flag.NArg() != 1 { @@ -55,7 +51,7 @@ func main() { printMetadata(m) - if raw { + if *raw { fmt.Println() fmt.Println() @@ -69,7 +65,7 @@ func main() { } } - if extractMBZ { + if *extractMBZ { b, err := json.MarshalIndent(mbz.Extract(m), "", " ") if err != nil { fmt.Printf("error marshalling MusicBrainz info: %v\n", err)