-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (73 loc) · 2.19 KB
/
Copy pathmain.go
File metadata and controls
86 lines (73 loc) · 2.19 KB
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
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"context"
_ "embed"
"fmt"
"log/slog"
"os"
"path/filepath"
"github.com/curt-hash/mkvbot/pkg/makemkv"
"github.com/urfave/cli/v3"
)
//go:embed makemkv.xml
var profileBytes []byte
//go:embed mkvbot.toml
var configBytes []byte
func main() {
cmd := newCLICommand()
if err := cmd.Run(context.Background(), os.Args); err != nil {
fmt.Printf("Fatal error: %s\n", err.Error())
os.Exit(1)
}
}
func run(ctx context.Context, cmd *cli.Command) error {
if cmd.Bool(demoFlagName) {
return runDemo(ctx)
}
if cmd.Bool(createConfigFlagName) {
if err := os.WriteFile(defaultConfigPath, configBytes, 0600); err != nil {
return fmt.Errorf("create %q: %w", defaultConfigPath, err)
}
fmt.Printf("wrote mkvbot config file %q\n", defaultConfigPath)
return nil
}
if cmd.Bool(createProfileFlagName) {
if err := os.WriteFile(defaultProfilePath, profileBytes, 0600); err != nil {
return fmt.Errorf("create %q: %w", defaultProfilePath, err)
}
fmt.Printf("wrote makemkv profile %q\n", defaultProfilePath)
return nil
}
profilePath := cmd.String(profileFlagName)
if _, err := os.Stat(profilePath); err == nil {
if profilePath, err = filepath.Abs(profilePath); err != nil {
return fmt.Errorf("get absolute path of %q: %w", profilePath, err)
}
} else {
slog.Warn("profile does not exist", "path", profilePath)
profilePath = ""
}
weights := make(map[string]int64, len(bestTitleHeuristics))
for _, h := range bestTitleHeuristics {
weights[h.name] = cmd.Int64(h.flagName)
}
cfg := &applicationConfig{
outputDirPath: cmd.String(outputDirFlagName),
makemkvConfig: &makemkv.Config{
ExePath: cmd.String(makemkvconFlagName),
ProfilePath: profilePath,
ReadCacheSizeMB: cmd.Int64(cacheFlagName),
MinLengthSeconds: cmd.Int64(minLengthFlagName),
},
debug: cmd.Bool(debugFlagName),
quiet: cmd.Bool(quietFlagName),
bestTitleHeuristicsWeights: weights,
askForTitle: cmd.Bool(askForTitleFlagName),
logFilePath: cmd.String(logFileFlagName),
}
app, err := newApplication(cfg)
if err != nil {
return fmt.Errorf("initialize application: %w", err)
}
return app.run(ctx)
}