From 620154e2079708df6d2eefb1f1ba4443d642ca83 Mon Sep 17 00:00:00 2001 From: cometjun Date: Wed, 8 Jan 2025 17:38:24 +0000 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20cmd/um/main.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 增加可以接收拖动多个文件参数,或多个文件夹参数功能 --- cmd/um/main.go | 155 ++++++++++++++++++++++++++----------------------- 1 file changed, 83 insertions(+), 72 deletions(-) diff --git a/cmd/um/main.go b/cmd/um/main.go index 2568d5d..de41737 100644 --- a/cmd/um/main.go +++ b/cmd/um/main.go @@ -122,88 +122,99 @@ func appMain(c *cli.Context) (err error) { printSupportedExtensions() return nil } + + var inputs []string input := c.String("input") - if input == "" { - switch c.Args().Len() { - case 0: - input = cwd - case 1: - input = c.Args().Get(0) - default: - return errors.New("please specify input file (or directory)") + if input != "" { + inputs = append(inputs, input) + } + + if c.Args().Len() > 0 { + inputs = append(inputs, c.Args().Slice()...) + } + + if len(inputs) == 0 { + inputs = append(inputs, cwd) + } + + for _, input := range inputs { + input, absErr := filepath.Abs(input) + if absErr != nil { + logger.Warn("get abs path failed", zap.String("input", input), zap.Error(absErr)) + continue } - } - input, absErr := filepath.Abs(input) - if absErr != nil { - return fmt.Errorf("get abs path failed: %w", absErr) - } - - output := c.String("output") - inputStat, err := os.Stat(input) - if err != nil { - return err - } - - var inputDir string - if inputStat.IsDir() { - inputDir = input - } else { - inputDir = filepath.Dir(input) - } - inputDir, absErr = filepath.Abs(inputDir) - if absErr != nil { - return fmt.Errorf("get abs path (inputDir) failed: %w", absErr) - } - - if output == "" { - // Default to where the input dir is - output = inputDir - } - logger.Debug("resolve input/output path", zap.String("inputDir", inputDir), zap.String("input", input), zap.String("output", output)) - - outputStat, err := os.Stat(output) - if err != nil { - if errors.Is(err, os.ErrNotExist) { - err = os.MkdirAll(output, 0755) - } + inputStat, err := os.Stat(input) if err != nil { - return err + logger.Warn("stat input failed", zap.String("input", input), zap.Error(err)) + continue } - } else if !outputStat.IsDir() { - return errors.New("output should be a writable directory") - } - if mmkv := c.String("qmc-mmkv"); mmkv != "" { - // If key is not set, the mmkv vault will be treated as unencrypted. - key := c.String("qmc-mmkv-key") - err := qmc.OpenMMKV(mmkv, key, logger) - if err != nil { - return err - } - } - - proc := &processor{ - logger: logger, - inputDir: inputDir, - outputDir: output, - skipNoopDecoder: c.Bool("skip-noop"), - removeSource: c.Bool("remove-source"), - updateMetadata: c.Bool("update-metadata"), - overwriteOutput: c.Bool("overwrite"), - } - - if inputStat.IsDir() { - watchDir := c.Bool("watch") - if !watchDir { - return proc.processDir(input) + var inputDir string + if inputStat.IsDir() { + inputDir = input } else { - return proc.watchDir(input) + inputDir = filepath.Dir(input) + } + inputDir, absErr = filepath.Abs(inputDir) + if absErr != nil { + return fmt.Errorf("get abs path (inputDir) failed: %w", absErr) + } + + output := c.String("output") + if output == "" { + // Default to where the input dir is + output = inputDir + } + logger.Debug("resolve input/output path", zap.String("inputDir", inputDir), zap.String("input", input), zap.String("output", output)) + + outputStat, err := os.Stat(output) + if err != nil { + if errors.Is(err, os.ErrNotExist) { + err = os.MkdirAll(output, 0755) + } + if err != nil { + return err + } + } else if !outputStat.IsDir() { + return errors.New("output should be a writable directory") + } + + if mmkv := c.String("qmc-mmkv"); mmkv != "" { + // If key is not set, the mmkv vault will be treated as unencrypted. + key := c.String("qmc-mmkv-key") + err := qmc.OpenMMKV(mmkv, key, logger) + if err != nil { + return err + } + } + + proc := &processor{ + logger: logger, + inputDir: inputDir, + outputDir: output, + skipNoopDecoder: c.Bool("skip-noop"), + removeSource: c.Bool("remove-source"), + updateMetadata: c.Bool("update-metadata"), + overwriteOutput: c.Bool("overwrite"), + } + + if inputStat.IsDir() { + watchDir := c.Bool("watch") + if !watchDir { + err = proc.processDir(input) + } else { + err = proc.watchDir(input) + } + } else { + err = proc.processFile(input) + } + if err != nil { + logger.Error("conversion failed", zap.String("source", input), zap.Error(err)) } - } else { - return proc.processFile(input) } + return nil } type processor struct { -- 2.45.2