From d33177319c776cc9bc5628e4ccbbe8867d43a002 Mon Sep 17 00:00:00 2001 From: Fan Peilin Date: Fri, 13 Jan 2023 22:53:48 +0800 Subject: [PATCH] add recursive flag --- cmd/um/main.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/cmd/um/main.go b/cmd/um/main.go index 8402142..cd858d2 100644 --- a/cmd/um/main.go +++ b/cmd/um/main.go @@ -55,6 +55,7 @@ func main() { &cli.BoolFlag{Name: "update-metadata", Usage: "update metadata & album art from network", Required: false, Value: false}, &cli.BoolFlag{Name: "overwrite", Usage: "overwrite output file without asking", Required: false, Value: false}, &cli.BoolFlag{Name: "watch", Usage: "watch the input dir and process new files", Required: false, Value: false}, + &cli.BoolFlag{Name: "recursive", Aliases: []string{"r"}, Usage: "unblock dir recursively", Required: false, Value: false}, &cli.BoolFlag{Name: "supported-ext", Usage: "show supported file extensions and exit", Required: false, Value: false}, }, @@ -139,6 +140,9 @@ func appMain(c *cli.Context) (err error) { } if inputStat.IsDir() { + if c.Bool("recursive") { + return proc.processDirRecursively(input, "") + } wacthDir := c.Bool("watch") if !wacthDir { return proc.processDir(input) @@ -238,6 +242,49 @@ func (p *processor) processDir(inputDir string) error { return nil } +func (p *processor) processDirRecursively(inputDir string, appendDir string) error { + items, err := os.ReadDir(inputDir) + if err != nil { + logger.Error("cannot read dir", zap.String("source", inputDir), zap.Error(err)) + return err + } + var rtErr error = nil + for _, item := range items { + itemPath := filepath.Join(inputDir, item.Name()) + if item.IsDir() { + rtErr = p.processDirRecursively(itemPath, filepath.Join(appendDir, item.Name())) + continue + } + + allDec := common.GetDecoder(itemPath, p.skipNoopDecoder) + if len(allDec) == 0 { + logger.Info("skipping while no suitable decoder", zap.String("source", item.Name())) + continue + } + + outDirTmp := p.outputDir + p.outputDir = filepath.Join(p.outputDir, appendDir) + outputStat, err := os.Stat(p.outputDir) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + err = os.MkdirAll(p.outputDir, 0755) + } + } else if !outputStat.IsDir() { + err = errors.New("output should be a writable directory") + } + if err == nil { // process when there is no error + if err := p.process(itemPath, allDec); err != nil { + logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err)) + } + } else { + logger.Error("error", zap.Error(err)) + rtErr = err + } + p.outputDir = outDirTmp + } + return rtErr +} + func (p *processor) processFile(filePath string) error { allDec := common.GetDecoder(filePath, p.skipNoopDecoder) if len(allDec) == 0 {