1
0
forked from um/cli

更新 cmd/um/main.go

增加可以接收拖动多个文件参数,或多个文件夹参数功能
This commit is contained in:
cometjun 2025-01-08 17:38:24 +00:00
parent 28ca8cef60
commit 620154e207

View File

@ -122,88 +122,99 @@ func appMain(c *cli.Context) (err error) {
printSupportedExtensions() printSupportedExtensions()
return nil return nil
} }
var inputs []string
input := c.String("input") input := c.String("input")
if input == "" { if input != "" {
switch c.Args().Len() { inputs = append(inputs, input)
case 0: }
input = cwd
case 1: if c.Args().Len() > 0 {
input = c.Args().Get(0) inputs = append(inputs, c.Args().Slice()...)
default: }
return errors.New("please specify input file (or directory)")
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) inputStat, err := os.Stat(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)
}
if err != nil { 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 != "" { var inputDir string
// If key is not set, the mmkv vault will be treated as unencrypted. if inputStat.IsDir() {
key := c.String("qmc-mmkv-key") inputDir = input
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)
} else { } 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 { type processor struct {