mirror of
https://git.um-react.app/um/cli.git
synced 2025-11-28 11:43:02 +00:00
Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0d071a82be | ||
|
|
b8e6196248 | ||
|
|
1323fb9e1a | ||
|
|
36df203bdd | ||
|
|
2afc232eb1 | ||
|
|
2abdd47c9c | ||
|
|
8b59bc026d | ||
|
|
91855f8f5b | ||
|
|
7edd326b95 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
|||||||
.idea
|
.idea
|
||||||
|
|
||||||
/dist
|
/dist
|
||||||
|
*.exe
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/debug"
|
"runtime/debug"
|
||||||
@@ -33,7 +34,7 @@ import (
|
|||||||
"unlock-music.dev/cli/internal/utils"
|
"unlock-music.dev/cli/internal/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
var AppVersion = "v0.2.2"
|
var AppVersion = "v0.2.3"
|
||||||
|
|
||||||
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
|
var logger, _ = logging.NewZapLogger() // TODO: inject logger to application, instead of using global logger
|
||||||
|
|
||||||
@@ -84,6 +85,11 @@ func printSupportedExtensions() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func appMain(c *cli.Context) (err error) {
|
func appMain(c *cli.Context) (err error) {
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
if c.Bool("supported-ext") {
|
if c.Bool("supported-ext") {
|
||||||
printSupportedExtensions()
|
printSupportedExtensions()
|
||||||
return nil
|
return nil
|
||||||
@@ -92,10 +98,7 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
if input == "" {
|
if input == "" {
|
||||||
switch c.Args().Len() {
|
switch c.Args().Len() {
|
||||||
case 0:
|
case 0:
|
||||||
input, err = os.Getwd()
|
input = cwd
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case 1:
|
case 1:
|
||||||
input = c.Args().Get(0)
|
input = c.Args().Get(0)
|
||||||
default:
|
default:
|
||||||
@@ -104,22 +107,20 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
output := c.String("output")
|
output := c.String("output")
|
||||||
if output == "" {
|
|
||||||
var err error
|
|
||||||
output, err = os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if input == output {
|
|
||||||
return errors.New("input and output path are same")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inputStat, err := os.Stat(input)
|
inputStat, err := os.Stat(input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if output == "" {
|
||||||
|
// Default to where the input is
|
||||||
|
if inputStat.IsDir() {
|
||||||
|
output = input
|
||||||
|
} else {
|
||||||
|
output = path.Dir(input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
outputStat, err := os.Stat(output)
|
outputStat, err := os.Stat(output)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
if errors.Is(err, os.ErrNotExist) {
|
||||||
@@ -142,6 +143,7 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
proc := &processor{
|
proc := &processor{
|
||||||
|
inputDir: input,
|
||||||
outputDir: output,
|
outputDir: output,
|
||||||
skipNoopDecoder: c.Bool("skip-noop"),
|
skipNoopDecoder: c.Bool("skip-noop"),
|
||||||
removeSource: c.Bool("remove-source"),
|
removeSource: c.Bool("remove-source"),
|
||||||
@@ -150,8 +152,8 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if inputStat.IsDir() {
|
if inputStat.IsDir() {
|
||||||
wacthDir := c.Bool("watch")
|
watchDir := c.Bool("watch")
|
||||||
if !wacthDir {
|
if !watchDir {
|
||||||
return proc.processDir(input)
|
return proc.processDir(input)
|
||||||
} else {
|
} else {
|
||||||
return proc.watchDir(input)
|
return proc.watchDir(input)
|
||||||
@@ -163,6 +165,7 @@ func appMain(c *cli.Context) (err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type processor struct {
|
type processor struct {
|
||||||
|
inputDir string
|
||||||
outputDir string
|
outputDir string
|
||||||
|
|
||||||
skipNoopDecoder bool
|
skipNoopDecoder bool
|
||||||
@@ -230,29 +233,32 @@ func (p *processor) processDir(inputDir string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var lastError error = nil
|
||||||
for _, item := range items {
|
for _, item := range items {
|
||||||
if item.IsDir() {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
filePath := filepath.Join(inputDir, item.Name())
|
filePath := filepath.Join(inputDir, item.Name())
|
||||||
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
if item.IsDir() {
|
||||||
if len(allDec) == 0 {
|
if err = p.processDir(filePath); err != nil {
|
||||||
logger.Info("skipping while no suitable decoder", zap.String("source", item.Name()))
|
lastError = err
|
||||||
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.process(filePath, allDec); err != nil {
|
if err := p.processFile(filePath); err != nil {
|
||||||
|
lastError = err
|
||||||
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
|
logger.Error("conversion failed", zap.String("source", item.Name()), zap.Error(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if lastError != nil {
|
||||||
|
return fmt.Errorf("last error: %w", lastError)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *processor) processFile(filePath string) error {
|
func (p *processor) processFile(filePath string) error {
|
||||||
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
allDec := common.GetDecoder(filePath, p.skipNoopDecoder)
|
||||||
if len(allDec) == 0 {
|
if len(allDec) == 0 {
|
||||||
logger.Fatal("skipping while no suitable decoder")
|
return errors.New("skipping while no suitable decoder")
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := p.process(filePath, allDec); err != nil {
|
if err := p.process(filePath, allDec); err != nil {
|
||||||
@@ -354,8 +360,13 @@ 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))
|
inFilename := strings.TrimSuffix(filepath.Base(inputFile), filepath.Ext(inputFile))
|
||||||
outPath := filepath.Join(p.outputDir, inFilename+params.AudioExt)
|
outPath := filepath.Join(p.outputDir, inputRelDir, inFilename+params.AudioExt)
|
||||||
|
|
||||||
if !p.overwriteOutput {
|
if !p.overwriteOutput {
|
||||||
_, err := os.Stat(outPath)
|
_, err := os.Stat(outPath)
|
||||||
|
|||||||
Reference in New Issue
Block a user