mirror of
https://github.com/cgzirim/seek-tune.git
synced 2025-12-17 00:44:19 +00:00
feat(erase): add db/all subcommands with db as default
Add selective deletion to erase command: - 'db' (default): clear only database - 'all': clear database and song files Updated README and help messages with new usage examples.
This commit is contained in:
parent
e9fc155153
commit
9886871070
3 changed files with 63 additions and 20 deletions
|
|
@ -95,7 +95,12 @@ go run *.go find <path-to-wav-file>
|
||||||
```
|
```
|
||||||
#### ▸ Delete fingerprints and songs 🗑️
|
#### ▸ Delete fingerprints and songs 🗑️
|
||||||
```
|
```
|
||||||
|
# Delete only database (default)
|
||||||
go run *.go erase
|
go run *.go erase
|
||||||
|
go run *.go erase db
|
||||||
|
|
||||||
|
# Delete both database and song files
|
||||||
|
go run *.go erase all
|
||||||
```
|
```
|
||||||
|
|
||||||
## Example :film_projector:
|
## Example :film_projector:
|
||||||
|
|
|
||||||
|
|
@ -199,7 +199,7 @@ func serveHTTP(socketServer *socketio.Server, serveHTTPS bool, port string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func erase(songsDir string) {
|
func erase(songsDir string, dbOnly bool, all bool) {
|
||||||
logger := utils.GetLogger()
|
logger := utils.GetLogger()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|
||||||
|
|
@ -222,26 +222,31 @@ func erase(songsDir string) {
|
||||||
logger.ErrorContext(ctx, msg, slog.Any("error", err))
|
logger.ErrorContext(ctx, msg, slog.Any("error", err))
|
||||||
}
|
}
|
||||||
|
|
||||||
// delete song files
|
fmt.Println("Database cleared")
|
||||||
err = filepath.Walk(songsDir, func(path string, info os.FileInfo, err error) error {
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if !info.IsDir() {
|
// delete song files only if -all flag is set
|
||||||
ext := filepath.Ext(path)
|
if all {
|
||||||
if ext == ".wav" || ext == ".m4a" {
|
err = filepath.Walk(songsDir, func(path string, info os.FileInfo, err error) error {
|
||||||
err := os.Remove(path)
|
if err != nil {
|
||||||
if err != nil {
|
return err
|
||||||
return err
|
}
|
||||||
|
|
||||||
|
if !info.IsDir() {
|
||||||
|
ext := filepath.Ext(path)
|
||||||
|
if ext == ".wav" || ext == ".m4a" {
|
||||||
|
err := os.Remove(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
msg := fmt.Sprintf("Error walking through directory %s: %v\n", songsDir, err)
|
||||||
|
logger.ErrorContext(ctx, msg, slog.Any("error", err))
|
||||||
}
|
}
|
||||||
return nil
|
fmt.Println("Songs folder cleared")
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
msg := fmt.Sprintf("Error walking through directory %s: %v\n", songsDir, err)
|
|
||||||
logger.ErrorContext(ctx, msg, slog.Any("error", err))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Erase complete")
|
fmt.Println("Erase complete")
|
||||||
|
|
|
||||||
|
|
@ -8,8 +8,8 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"song-recognition/utils"
|
"song-recognition/utils"
|
||||||
|
|
||||||
"github.com/mdobak/go-xerrors"
|
|
||||||
"github.com/joho/godotenv"
|
"github.com/joho/godotenv"
|
||||||
|
"github.com/mdobak/go-xerrors"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
@ -32,10 +32,16 @@ func main() {
|
||||||
|
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
fmt.Println("Expected 'find', 'download', 'erase', 'save', or 'serve' subcommands")
|
fmt.Println("Expected 'find', 'download', 'erase', 'save', or 'serve' subcommands")
|
||||||
|
fmt.Println("\nUsage examples:")
|
||||||
|
fmt.Println(" find <path_to_wav_file>")
|
||||||
|
fmt.Println(" download <spotify_url>")
|
||||||
|
fmt.Println(" erase [db | all] (default: db)")
|
||||||
|
fmt.Println(" save [-f|--force] <path_to_file_or_dir>")
|
||||||
|
fmt.Println(" serve [-proto <http|https>] [-p <port>]")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
_ = godotenv.Load()
|
_ = godotenv.Load()
|
||||||
|
|
||||||
switch os.Args[1] {
|
switch os.Args[1] {
|
||||||
case "find":
|
case "find":
|
||||||
if len(os.Args) < 3 {
|
if len(os.Args) < 3 {
|
||||||
|
|
@ -58,7 +64,28 @@ func main() {
|
||||||
serveCmd.Parse(os.Args[2:])
|
serveCmd.Parse(os.Args[2:])
|
||||||
serve(*protocol, *port)
|
serve(*protocol, *port)
|
||||||
case "erase":
|
case "erase":
|
||||||
erase(SONGS_DIR)
|
// Default is to clear only database (db mode)
|
||||||
|
dbOnly := true
|
||||||
|
all := false
|
||||||
|
|
||||||
|
if len(os.Args) > 2 {
|
||||||
|
subCmd := os.Args[2]
|
||||||
|
switch subCmd {
|
||||||
|
case "db":
|
||||||
|
dbOnly = true
|
||||||
|
all = false
|
||||||
|
case "all":
|
||||||
|
dbOnly = false
|
||||||
|
all = true
|
||||||
|
default:
|
||||||
|
fmt.Println("Usage: main.go erase [db | all]")
|
||||||
|
fmt.Println(" db : only clear the database (default)")
|
||||||
|
fmt.Println(" all : clear database and songs folder")
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
erase(SONGS_DIR, dbOnly, all)
|
||||||
case "save":
|
case "save":
|
||||||
indexCmd := flag.NewFlagSet("save", flag.ExitOnError)
|
indexCmd := flag.NewFlagSet("save", flag.ExitOnError)
|
||||||
force := indexCmd.Bool("force", false, "save song with or without YouTube ID")
|
force := indexCmd.Bool("force", false, "save song with or without YouTube ID")
|
||||||
|
|
@ -72,6 +99,12 @@ func main() {
|
||||||
save(filePath, *force)
|
save(filePath, *force)
|
||||||
default:
|
default:
|
||||||
fmt.Println("Expected 'find', 'download', 'erase', 'save', or 'serve' subcommands")
|
fmt.Println("Expected 'find', 'download', 'erase', 'save', or 'serve' subcommands")
|
||||||
|
fmt.Println("\nUsage examples:")
|
||||||
|
fmt.Println(" find <path_to_wav_file>")
|
||||||
|
fmt.Println(" download <spotify_url>")
|
||||||
|
fmt.Println(" erase [db | all] (default: db)")
|
||||||
|
fmt.Println(" save [-f|--force] <path_to_file_or_dir>")
|
||||||
|
fmt.Println(" serve [-proto <http|https>] [-p <port>]")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue