Compare commits

..

No commits in common. "0d071a82be903d927104bee7925f4c686634ae04" and "0b3ad0d97c3004f16bd1ea93b5235a365722842f" have entirely different histories.

2 changed files with 28 additions and 40 deletions

1
.gitignore vendored
View File

@ -1,4 +1,3 @@
.idea
/dist
*.exe

View File

@ -8,7 +8,6 @@ import (
"io"
"os"
"os/signal"
"path"
"path/filepath"
"runtime"
"runtime/debug"
@ -34,7 +33,7 @@ import (
"unlock-music.dev/cli/internal/utils"
)
var AppVersion = "v0.2.3"
var AppVersion = "v0.2.2"
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
@ -85,11 +84,6 @@ func printSupportedExtensions() {
}
func appMain(c *cli.Context) (err error) {
cwd, err := os.Getwd()
if err != nil {
return err
}
if c.Bool("supported-ext") {
printSupportedExtensions()
return nil
@ -98,7 +92,10 @@ func appMain(c *cli.Context) (err error) {
if input == "" {
switch c.Args().Len() {
case 0:
input = cwd
input, err = os.Getwd()
if err != nil {
return err
}
case 1:
input = c.Args().Get(0)
default:
@ -107,20 +104,22 @@ func appMain(c *cli.Context) (err error) {
}
output := c.String("output")
inputStat, err := os.Stat(input)
if output == "" {
var err error
output, err = os.Getwd()
if err != nil {
return err
}
if output == "" {
// Default to where the input is
if inputStat.IsDir() {
output = input
} else {
output = path.Dir(input)
if input == output {
return errors.New("input and output path are same")
}
}
inputStat, err := os.Stat(input)
if err != nil {
return err
}
outputStat, err := os.Stat(output)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
@ -143,7 +142,6 @@ func appMain(c *cli.Context) (err error) {
}
proc := &processor{
inputDir: input,
outputDir: output,
skipNoopDecoder: c.Bool("skip-noop"),
removeSource: c.Bool("remove-source"),
@ -152,8 +150,8 @@ func appMain(c *cli.Context) (err error) {
}
if inputStat.IsDir() {
watchDir := c.Bool("watch")
if !watchDir {
wacthDir := c.Bool("watch")
if !wacthDir {
return proc.processDir(input)
} else {
return proc.watchDir(input)
@ -165,7 +163,6 @@ func appMain(c *cli.Context) (err error) {
}
type processor struct {
inputDir string
outputDir string
skipNoopDecoder bool
@ -233,32 +230,29 @@ func (p *processor) processDir(inputDir string) error {
if err != nil {
return err
}
var lastError error = nil
for _, item := range items {
filePath := filepath.Join(inputDir, item.Name())
if item.IsDir() {
if err = p.processDir(filePath); err != nil {
lastError = err
}
continue
}
if err := p.processFile(filePath); err != nil {
lastError = err
filePath := filepath.Join(inputDir, item.Name())
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
if len(allDec) == 0 {
logger.Info("skipping while no suitable decoder", zap.String("source", item.Name()))
continue
}
if err := p.process(filePath, allDec); err != nil {
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
}
}
if lastError != nil {
return fmt.Errorf("last error: %w", lastError)
}
return nil
}
func (p *processor) processFile(filePath string) error {
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
if len(allDec) == 0 {
return errors.New("skipping while no suitable decoder")
logger.Fatal("skipping while no suitable decoder")
}
if err := p.process(filePath, allDec); err != nil {
@ -360,13 +354,8 @@ func (p *processor) process(inputFile string, allDec []common.NewDecoderFunc) er
}
}
inputRelDir, err := filepath.Rel(p.inputDir, filepath.Dir(inputFile))
if err != nil {
return fmt.Errorf("get relative dir failed: %w", err)
}
inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
outPath := filepath.Join(p.outputDir, inputRelDir, inFilename+params.AudioExt)
outPath := filepath.Join(p.outputDir, inFilename+params.AudioExt)
if !p.overwriteOutput {
_, err := os.Stat(outPath)