-
Notifications
You must be signed in to change notification settings - Fork 118
/
Copy pathmain.go
75 lines (69 loc) · 1.9 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"song-recognition/utils"
"github.com/mdobak/go-xerrors"
)
func main() {
err := utils.CreateFolder("tmp")
if err != nil {
logger := utils.GetLogger()
err := xerrors.New(err)
ctx := context.Background()
logger.ErrorContext(ctx, "Failed create tmp dir.", slog.Any("error", err))
}
err = utils.CreateFolder(SONGS_DIR)
if err != nil {
err := xerrors.New(err)
logger := utils.GetLogger()
ctx := context.Background()
logMsg := fmt.Sprintf("failed to create directory %v", SONGS_DIR)
logger.ErrorContext(ctx, logMsg, slog.Any("error", err))
}
if len(os.Args) < 2 {
fmt.Println("Expected 'find', 'download', 'erase', 'save', or 'serve' subcommands")
os.Exit(1)
}
switch os.Args[1] {
case "find":
if len(os.Args) < 3 {
fmt.Println("Usage: main.go find <path_to_wav_file>")
os.Exit(1)
}
filePath := os.Args[2]
find(filePath)
case "download":
if len(os.Args) < 3 {
fmt.Println("Usage: main.go download <spotify_url>")
os.Exit(1)
}
url := os.Args[2]
download(url)
case "serve":
serveCmd := flag.NewFlagSet("serve", flag.ExitOnError)
protocol := serveCmd.String("proto", "http", "Protocol to use (http or https)")
port := serveCmd.String("p", "5000", "Port to use")
serveCmd.Parse(os.Args[2:])
serve(*protocol, *port)
case "erase":
erase(SONGS_DIR)
case "save":
indexCmd := flag.NewFlagSet("save", flag.ExitOnError)
force := indexCmd.Bool("force", false, "save song with or without YouTube ID")
indexCmd.BoolVar(force, "f", false, "save song with or without YouTube ID (shorthand)")
indexCmd.Parse(os.Args[2:])
if indexCmd.NArg() < 1 {
fmt.Println("Usage: main.go save [-f|--force] <path_to_wav_file_or_dir>")
os.Exit(1)
}
filePath := indexCmd.Arg(0)
save(filePath, *force)
default:
fmt.Println("Expected 'find', 'download', 'erase', 'save', or 'serve' subcommands")
os.Exit(1)
}
}