-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
105 lines (83 loc) · 1.95 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Package main
package main
import (
"context"
"fmt"
"log"
"log/slog"
"os"
"strings"
"github.com/docopt/docopt-go"
"github.com/env25/mpdlrc/internal"
"github.com/env25/mpdlrc/internal/client"
"github.com/env25/mpdlrc/internal/config"
"github.com/env25/mpdlrc/internal/dirs"
)
const usage = `
Display synchronized lyrics for track playing in MPD.
Usage:
mpdlrc -h|--help
mpdlrc [options]
mpdlrc [options] --config=FILE...
Options:
-h, --help Show this help and exit
--config=FILE Use config file
--dump-config Print final config
Configuration Options:
--lyricsdir=DIR override cfg.LyricsDir
--musicdir=DIR override cfg.MusicDir
--mpd-connection=CONN override cfg.MPD.Connection
--mpd-address=ADDR override cfg.MPD.Address
--mpd-password=PASSWD override cfg.MPD.Password
`
func maine() (_ int, err error) {
opts, err := docopt.ParseDoc(usage)
if err != nil {
return 1, err
}
cfg := config.DefaultConfig()
err = cfg.FromFiles(opts["--config"].([]string))
if err != nil {
return 1, err
}
cfg.FromEnv(dirs.GetEnv)
cfg.FromOpts(opts)
conn, err := client.NewMPDClient(&cfg.MPD.Connection, &cfg.MPD.Address, &cfg.MPD.Password, &cfg.LyricsDir)
if err != nil {
return 1, err
}
defer conn.Close()
cfg.FromClient(conn)
cfg.Expand()
if opts["--dump-config"].(bool) {
fmt.Print(cfg)
return
}
err = cfg.Assert()
if err != nil {
return 1, err
}
if config.Debug {
fmt.Fprint(os.Stderr, "\n", cfg, "\n")
}
err = internal.NewApplication(cfg, conn).Run(context.Background())
if err != nil {
return 1, err
}
return
}
func main() {
var logBuilder strings.Builder
log.SetFlags(0)
if config.Debug {
slog.SetDefault(slog.New(slog.NewTextHandler(&logBuilder, &slog.HandlerOptions{Level: slog.LevelDebug})))
} else {
log.SetOutput(&logBuilder)
}
code, err := maine()
if err != nil {
slog.Error("main", err)
}
fmt.Fprint(os.Stderr, &logBuilder)
os.Exit(code)
}