Compare commits

..

No commits in common. "0bb001e7e2b2dc7e27898116a4d7ac3c9c820459" and "30a50c1eaebd3bde8cb3d72942c015f487bf7d69" have entirely different histories.

3 changed files with 37 additions and 114 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env bash #!/usr/bin/env bash
VERSION=1.6.0 VERSION=1.5.0
# Clean up the build directory # Clean up the build directory
rm -rf build rm -rf build

104
main.go
View File

@ -10,19 +10,13 @@ import (
flag "github.com/spf13/pflag" flag "github.com/spf13/pflag"
) )
func processFile(filePath string, outputDir string) error { func processFile(filePath string) error {
// skip if the extension is not .ncm
if filePath[len(filePath)-4:] != ".ncm" {
return nil
}
// process the file
currentFile, err := ncmcrypt.NewNeteaseCloudMusic(filePath) currentFile, err := ncmcrypt.NewNeteaseCloudMusic(filePath)
if err != nil { if err != nil {
utils.ErrorPrintfln("Reading '%s' failed: %s", filePath, err.Error()) utils.ErrorPrintfln("Reading '%s' failed: %s", filePath, err.Error())
return err return err
} }
dump, err := currentFile.Dump(outputDir) dump, err := currentFile.Dump(filepath.Dir(filePath))
if err != nil { if err != nil {
utils.ErrorPrintfln("Processing '%s' failed: %s", filePath, err.Error()) utils.ErrorPrintfln("Processing '%s' failed: %s", filePath, err.Error())
return err return err
@ -39,13 +33,10 @@ func processFile(filePath string, outputDir string) error {
} }
func main() { func main() {
var sourceDir string var folderPath string
var outputDir string
showHelp := flag.BoolP("help", "h", false, "Display help message") showHelp := flag.BoolP("help", "h", false, "Display help message")
showVersion := flag.BoolP("version", "v", false, "Display version information") showVersion := flag.BoolP("version", "v", false, "Display version information")
processRecursive := flag.BoolP("recursive", "r", false, "Process all files in the directory recursively") flag.StringVarP(&folderPath, "dir", "d", "", "Process all files in the directory")
flag.StringVarP(&outputDir, "output", "o", "", "Output directory for the dump files")
flag.StringVarP(&sourceDir, "dir", "d", "", "Process all files in the directory")
flag.Parse() flag.Parse()
if len(os.Args) == 1 { if len(os.Args) == 1 {
@ -59,59 +50,27 @@ func main() {
} }
if *showVersion { if *showVersion {
fmt.Println("ncmdump version 1.6.0") fmt.Println("ncmdump version 1.5.0")
os.Exit(0) os.Exit(0)
} }
if !flag.Lookup("dir").Changed && sourceDir == "" && len(flag.Args()) == 0 { if folderPath != "" {
flag.Usage() // check if the folder exists
os.Exit(1) info, err := os.Stat(folderPath)
}
if flag.Lookup("recursive").Changed && !flag.Lookup("dir").Changed {
utils.ErrorPrintfln("The -r option can only be used with the -d option")
os.Exit(1)
}
outputDirSpecified := flag.Lookup("output").Changed
if outputDirSpecified {
if utils.PathExists(outputDir) {
if !utils.IsDir(outputDir) {
utils.ErrorPrintfln("Output directory '%s' is not valid.", outputDir)
os.Exit(1)
}
} else {
_ = os.MkdirAll(outputDir, os.ModePerm)
}
}
if sourceDir != "" {
if !utils.IsDir(sourceDir) {
utils.ErrorPrintfln("The source directory '%s' is not valid.", sourceDir)
os.Exit(1)
}
if *processRecursive {
_ = filepath.WalkDir(sourceDir, func(p string, d os.DirEntry, err_ error) error {
if !outputDirSpecified {
outputDir = sourceDir
}
relativePath := utils.GetRelativePath(sourceDir, p)
destinationPath := filepath.Join(outputDir, relativePath)
if utils.IsRegularFile(p) {
parentDir := filepath.Dir(destinationPath)
_ = os.MkdirAll(parentDir, os.ModePerm)
_ = processFile(p, parentDir)
}
return nil
})
} else {
// dump files in the folder
files, err := os.ReadDir(sourceDir)
if err != nil { if err != nil {
utils.ErrorPrintfln("Unable to read directory: '%s'", sourceDir) utils.ErrorPrintfln("Unable to access directory: '%s'", folderPath)
os.Exit(1)
}
if !info.IsDir() {
utils.ErrorPrintfln("Not a directory: %s", folderPath)
os.Exit(1)
}
// dump files in the folder
files, err := os.ReadDir(folderPath)
if err != nil {
utils.ErrorPrintfln("Unable to read directory: '%s'", folderPath)
os.Exit(1) os.Exit(1)
} }
@ -120,25 +79,26 @@ func main() {
continue continue
} }
filePath := filepath.Join(sourceDir, file.Name()) filePath := filepath.Join(folderPath, file.Name())
if outputDirSpecified { // skip if the extension is not .ncm
_ = processFile(filePath, outputDir) if filePath[len(filePath)-4:] != ".ncm" {
} else { continue
_ = processFile(filePath, sourceDir)
} }
err = processFile(filePath)
if err != nil {
continue
} }
} }
} else { } else {
// process files from args // dump file from args
for _, filePath := range flag.Args() { for _, filePath := range flag.Args() {
// skip if the extension is not .ncm // skip if the extension is not .ncm
if filePath[len(filePath)-4:] != ".ncm" { if filePath[len(filePath)-4:] != ".ncm" {
continue continue
} }
if outputDirSpecified { err := processFile(filePath)
_ = processFile(filePath, outputDir) if err != nil {
} else { continue
_ = processFile(filePath, sourceDir)
} }
} }
} }

View File

@ -1,7 +1,6 @@
package utils package utils
import ( import (
"os"
"path/filepath" "path/filepath"
"strings" "strings"
) )
@ -10,39 +9,3 @@ func ReplaceExtension(filepathStr, newExt string) string {
ext := filepath.Ext(filepathStr) ext := filepath.Ext(filepathStr)
return strings.TrimSuffix(filepathStr, ext) + newExt return strings.TrimSuffix(filepathStr, ext) + newExt
} }
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
func GetRelativePath(from, to string) string {
rel, err := filepath.Rel(from, to)
if err != nil {
return ""
}
return rel
}
func IsRegularFile(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.Mode().IsRegular()
}