mirror of
https://github.com/cgzirim/seek-tune.git
synced 2025-12-16 16:34:21 +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 only database (default)
|
||||
go run *.go erase
|
||||
go run *.go erase db
|
||||
|
||||
# Delete both database and song files
|
||||
go run *.go erase all
|
||||
```
|
||||
|
||||
## 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()
|
||||
ctx := context.Background()
|
||||
|
||||
|
|
@ -222,26 +222,31 @@ func erase(songsDir string) {
|
|||
logger.ErrorContext(ctx, msg, slog.Any("error", err))
|
||||
}
|
||||
|
||||
// delete song files
|
||||
err = filepath.Walk(songsDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("Database cleared")
|
||||
|
||||
if !info.IsDir() {
|
||||
ext := filepath.Ext(path)
|
||||
if ext == ".wav" || ext == ".m4a" {
|
||||
err := os.Remove(path)
|
||||
if err != nil {
|
||||
return err
|
||||
// delete song files only if -all flag is set
|
||||
if all {
|
||||
err = filepath.Walk(songsDir, func(path string, info os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
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
|
||||
})
|
||||
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("Songs folder cleared")
|
||||
}
|
||||
|
||||
fmt.Println("Erase complete")
|
||||
|
|
|
|||
|
|
@ -8,8 +8,8 @@ import (
|
|||
"os"
|
||||
"song-recognition/utils"
|
||||
|
||||
"github.com/mdobak/go-xerrors"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/mdobak/go-xerrors"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
|
@ -32,10 +32,16 @@ func main() {
|
|||
|
||||
if len(os.Args) < 2 {
|
||||
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)
|
||||
}
|
||||
_ = godotenv.Load()
|
||||
|
||||
|
||||
switch os.Args[1] {
|
||||
case "find":
|
||||
if len(os.Args) < 3 {
|
||||
|
|
@ -58,7 +64,28 @@ func main() {
|
|||
serveCmd.Parse(os.Args[2:])
|
||||
serve(*protocol, *port)
|
||||
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":
|
||||
indexCmd := flag.NewFlagSet("save", flag.ExitOnError)
|
||||
force := indexCmd.Bool("force", false, "save song with or without YouTube ID")
|
||||
|
|
@ -72,6 +99,12 @@ func main() {
|
|||
save(filePath, *force)
|
||||
default:
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue