Skip to content

Commit

Permalink
audioc: simplify flags & DirEntry into Config
Browse files Browse the repository at this point in the history
  • Loading branch information
rolyatsats committed Dec 13, 2019
1 parent b519478 commit face55f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 75 deletions.
38 changes: 15 additions & 23 deletions audioc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,19 @@ import (
"fmt"
"runtime"
"strings"
"path/filepath"

"github.com/jamlib/libaudio/ffmpeg"
"github.com/jamlib/libaudio/ffprobe"
"github.com/jamlib/libaudio/fsutil"
)

type Config struct {
DirEntry string
Flags flags
Dir, Artist, Album, Bitrate string
Collection, Fix, Force, Write bool
}

type audioc struct {
DirEntry string
Flags flags

Config *Config
Ffmpeg ffmpeg.Ffmpeger
Ffprobe ffprobe.Ffprober
Image string
Expand All @@ -29,36 +26,31 @@ type audioc struct {
Workdir string
}

type flags struct {
Artist, Album, Bitrate string
Collection, Fix, Force, Version, Write bool
}

func New(c *Config, ffm ffmpeg.Ffmpeger, ffp ffprobe.Ffprober) *audioc {
return &audioc{ DirEntry: filepath.Clean(c.DirEntry), Flags: c.Flags,
Ffmpeg: ffm, Ffprobe: ffp, Workers: runtime.NumCPU() }
return &audioc{ Config: c, Ffmpeg: ffm, Ffprobe: ffp,
Workers: runtime.NumCPU() }
}

func (a *audioc) Process() error {
if !a.Flags.Write {
if !a.Config.Write {
fmt.Printf("\n* To write changes to disk, please provide flag: --write\n")
}

// ensure path is is valid directory
fi, err := os.Stat(a.DirEntry)
fi, err := os.Stat(a.Config.Dir)
if err != nil || !fi.IsDir() {
return fmt.Errorf("Invalid directory: %s", a.DirEntry)
return fmt.Errorf("Invalid directory: %s", a.Config.Dir)
}

// obtain audio file list
a.Files = fsutil.FilesAudio(a.DirEntry)
a.Files = fsutil.FilesAudio(a.Config.Dir)

// if --artist mode, move innermost dir from a.DirEntry and add to each
// file path within a.Files since this folder could be the album name.
// if --artist mode, move innermost dir from a.Config.Dir and add to
// each file path within a.Files since this folder could be the album name.
// if it is only the artist name, then will mimic --collection
if a.Flags.Artist != "" {
bpa := strings.Split(a.DirEntry, fsutil.PathSep)
a.DirEntry = strings.Join(bpa[:len(bpa)-1], fsutil.PathSep)
if a.Config.Artist != "" {
bpa := strings.Split(a.Config.Dir, fsutil.PathSep)
a.Config.Dir = strings.Join(bpa[:len(bpa)-1], fsutil.PathSep)

for i := range a.Files {
a.Files[i] = bpa[len(bpa)-1] + fsutil.PathSep + a.Files[i]
Expand All @@ -67,7 +59,7 @@ func (a *audioc) Process() error {

// group files by parent directory; call a.processBundle
// a.processBundle found within bundle.go
err = fsutil.BundleFiles(a.DirEntry, a.Files, a.processBundle)
err = fsutil.BundleFiles(a.Config.Dir, a.Files, a.processBundle)
if err != nil {
return err
}
Expand Down
26 changes: 13 additions & 13 deletions audioc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestSkipFolderOnCollection(t *testing.T) {
}

for i := range tests {
a := &audioc{ Flags: flags{ Collection: tests[i].col } }
a := &audioc{ Config: &Config{ Collection: tests[i].col } }

r := a.skipFolder(tests[i].path)
if r != tests[i].skip {
Expand All @@ -45,7 +45,7 @@ func TestSkipFolderOnArtist(t *testing.T) {
}

for i := range tests {
a := &audioc{ Flags: flags{ Artist: tests[i].artist } }
a := &audioc{ Config: &Config{ Artist: tests[i].artist } }

r := a.skipFolder(tests[i].path)
if r != tests[i].skip {
Expand All @@ -66,7 +66,7 @@ func TestSkipFolderOnAlbum(t *testing.T) {
}

for i := range tests {
a := &audioc{ Flags: flags{ Artist: "Whoever", Album: tests[i].album } }
a := &audioc{ Config: &Config{ Artist: "Whoever", Album: tests[i].album } }

r := a.skipFolder(tests[i].path)
if r != tests[i].skip {
Expand All @@ -76,7 +76,7 @@ func TestSkipFolderOnAlbum(t *testing.T) {
}

func TestProcessDirDNE(t *testing.T) {
a := &audioc{ DirEntry: "audioc-dir-def-dne" }
a := &audioc{ Config: &Config{ Dir: "audioc-dir-def-dne" } }
err := a.Process()
if err == nil {
t.Errorf("Expected error, got none.")
Expand All @@ -96,8 +96,8 @@ func createTestProcessFiles(t *testing.T, entryDir string,
t.Errorf("Expected entryDir")
}

a := &audioc{ Ffmpeg: &ffmpeg.MockFfmpeg{}, Ffprobe: &ffprobe.MockFfprobe{},
Files: []string{}, Workers: 1 }
a := &audioc{ Config: &Config{}, Ffmpeg: &ffmpeg.MockFfmpeg{},
Ffprobe: &ffprobe.MockFfprobe{}, Files: []string{}, Workers: 1 }

indexes := []int{}
createFiles := []*fsutil.TestFile{}
Expand All @@ -113,8 +113,8 @@ func createTestProcessFiles(t *testing.T, entryDir string,
createFiles = append(createFiles, &fsutil.TestFile{nestedPath, string(b)})
}

a.DirEntry, _ = fsutil.CreateTestFiles(t, createFiles)
a.DirEntry += fsutil.PathSep + entryDir
a.Config.Dir, _ = fsutil.CreateTestFiles(t, createFiles)
a.Config.Dir += fsutil.PathSep + entryDir

return a, indexes
}
Expand All @@ -132,11 +132,11 @@ func TestProcessMain(t *testing.T) {
},
},
})
defer os.RemoveAll(filepath.Dir(a.DirEntry))
defer os.RemoveAll(filepath.Dir(a.Config.Dir))

a.Flags.Artist = "Phish"
a.Flags.Write = true
a.Flags.Force = true
a.Config.Artist = "Phish"
a.Config.Write = true
a.Config.Force = true

err := a.Process()

Expand All @@ -151,7 +151,7 @@ func TestProcessMain(t *testing.T) {
"Phish/2003/2003.07.17 Bonner Springs, KS/01-01 Chalk Dust Torture.mp3",
}

files := fsutil.FilesAudio(a.DirEntry)
files := fsutil.FilesAudio(a.Config.Dir)
if len(files) == 0 {
t.Errorf("No resulting files found.")
}
Expand Down
20 changes: 10 additions & 10 deletions bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import (
// process each bundle or folder of audio files
func (a *audioc) processBundle(indexes []int) error {
var err error
fullDir := filepath.Dir(filepath.Join(a.DirEntry, a.Files[indexes[0]]))
fullDir := filepath.Dir(filepath.Join(a.Config.Dir, a.Files[indexes[0]]))

// skip folder if possible (unless --force)
if !a.Flags.Force && a.skipFolder(a.Files[indexes[0]]) {
if !a.Config.Force && a.skipFolder(a.Files[indexes[0]]) {
return nil
}

fmt.Printf("\nProcessing: %v ...\n", fullDir)

if a.Flags.Write {
if a.Config.Write {
// create new random workdir within current path
a.Workdir, err = ioutil.TempDir(fullDir, "")
if err != nil {
Expand All @@ -48,13 +48,13 @@ func (a *audioc) processBundle(indexes []int) error {
return err
}

if a.Flags.Write {
if a.Config.Write {
// explicitly remove workdir (before folder is possibly renamed)
os.RemoveAll(a.Workdir)

// TODO: iterate through mdSlice moving each file individually instead of
// assuming all belong to same resulting directory
fullResultD := filepath.Dir(filepath.Join(a.DirEntry, mdSlice[0].Resultpath))
fullResultD := filepath.Dir(filepath.Join(a.Config.Dir, mdSlice[0].Resultpath))

// if not same dir, rename directory to target dir
if fullDir != fullResultD {
Expand Down Expand Up @@ -87,7 +87,7 @@ func (a *audioc) skipFolder(path string) bool {

// determine which folder in path is the album name
var alb string
if a.Flags.Collection {
if a.Config.Collection {
// true if --collection & artist path contains " - "
if strings.Index(pa[0], " - ") != -1 {
return true
Expand All @@ -105,9 +105,9 @@ func (a *audioc) skipFolder(path string) bool {

// true if album folder matches metadata.ToAlbum
if len(alb) > 0 {
if a.Flags.Album != "" {
if a.Config.Album != "" {
// if --album matches album folder
if a.Flags.Album == alb {
if a.Config.Album == alb {
return true
}
} else {
Expand All @@ -126,12 +126,12 @@ func (a *audioc) skipFolder(path string) bool {
func (a *audioc) processArtwork(file string) error {
art := &albumart.AlbumArt{ Ffmpeg: a.Ffmpeg, Ffprobe: a.Ffprobe,
ImgDecode: image.DecodeConfig, WithParentDir: true,
Fullpath: filepath.Join(a.DirEntry, file) }
Fullpath: filepath.Join(a.Config.Dir, file) }

var err error
a.Image = ""

if a.Flags.Write {
if a.Config.Write {
a.Image, err = albumart.Process(art)
}

Expand Down
27 changes: 14 additions & 13 deletions cmd/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,19 @@ func configFromFlags() (*audioc.Config, bool) {
flags := flag.NewFlagSet(os.Args[0], flag.ExitOnError)

// set mode
flags.StringVar(&c.Flags.Album, "album", "", "")
flags.StringVar(&c.Flags.Artist, "artist", "", "")
flags.BoolVar(&c.Flags.Collection, "collection", false, "")
flags.StringVar(&c.Album, "album", "", "")
flags.StringVar(&c.Artist, "artist", "", "")
flags.BoolVar(&c.Collection, "collection", false, "")

// set options
flags.StringVar(&c.Flags.Bitrate, "bitrate", "V0", "")
flags.BoolVar(&c.Flags.Fix, "fix", false, "")
flags.BoolVar(&c.Flags.Force, "force", false, "")
flags.BoolVar(&c.Flags.Write, "write", false, "")
flags.StringVar(&c.Bitrate, "bitrate", "V0", "")
flags.BoolVar(&c.Fix, "fix", false, "")
flags.BoolVar(&c.Force, "force", false, "")
flags.BoolVar(&c.Write, "write", false, "")

// set debug options
flags.BoolVar(&c.Flags.Version, "version", false, "")
var printVersion bool
flags.BoolVar(&printVersion, "version", false, "")

// create --help closure
flags.Usage = func() {
Expand All @@ -85,7 +86,7 @@ func configFromFlags() (*audioc.Config, bool) {
a := flags.Args()

// --version
if c.Flags.Version {
if printVersion {
fmt.Printf("%s\n", version)
return &c, false
}
Expand All @@ -97,17 +98,17 @@ func configFromFlags() (*audioc.Config, bool) {
}

// must specify proper MODE
if !c.Flags.Collection && c.Flags.Artist == "" {
if !c.Collection && c.Artist == "" {
fmt.Printf("\nError: Must provide a valid MODE\n")
flags.Usage()
return &c, false
}

// default to V0 unless 320 specified
if c.Flags.Bitrate != "320" {
c.Flags.Bitrate = "V0"
if c.Bitrate != "320" {
c.Bitrate = "V0"
}

c.DirEntry = filepath.Clean(a[0])
c.Dir = filepath.Clean(a[0])
return &c, true
}
4 changes: 2 additions & 2 deletions cmd/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ func TestProcessFlagsArtist(t *testing.T) {
if cont == false {
t.Errorf("Expected %v, got %v", true, cont)
}
if c.Flags.Artist != os.Args[2] {
t.Errorf("Expected %v, got %v", os.Args[2], c.Flags.Artist)
if c.Artist != os.Args[2] {
t.Errorf("Expected %v, got %v", os.Args[2], c.Artist)
}
}

Expand Down
Loading

0 comments on commit face55f

Please sign in to comment.