Merge pull request '更新 cmd/um/main.go' (#1) from cometjun-MutliFile into main

Reviewed-on: cometjun/cli#1
This commit is contained in:
cometjun 2025-01-08 17:39:10 +00:00
commit 28b2b9a6db

View File

@ -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 {