forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 7
/
stmps.go
277 lines (244 loc) · 7.75 KB
/
stmps.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright 2023 The STMPS Authors
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"flag"
"fmt"
"log"
"net/url"
"os"
"runtime"
"runtime/debug"
"runtime/pprof"
"github.com/spezifisch/stmps/logger"
"github.com/spezifisch/stmps/mpvplayer"
"github.com/spezifisch/stmps/remote"
"github.com/spezifisch/stmps/subsonic"
tviewcommand "github.com/spezifisch/tview-command"
"github.com/spf13/viper"
)
var osExit = os.Exit // A variable to allow mocking os.Exit in tests
var headlessMode bool // This can be set to true during tests
var testMode bool // This can be set to true during tests, too
const DEVELOPMENT = "development"
var Version string = DEVELOPMENT
func readConfig(configFile *string) error {
required_properties := []string{"auth.username", "auth.password", "server.host"}
if configFile != nil && *configFile != "" {
// use custom config file
viper.SetConfigFile(*configFile)
} else {
// lookup default dirs
viper.SetConfigName("stmp") // TODO this should be stmps
viper.SetConfigType("toml")
viper.AddConfigPath("$HOME/.config/stmp") // TODO this should be stmps only
viper.AddConfigPath("$HOME/.config/stmps")
viper.AddConfigPath(".")
}
// read it
err := viper.ReadInConfig()
if err != nil {
return fmt.Errorf("Config file error: %s\n", err)
}
// validate
for _, prop := range required_properties {
if !viper.IsSet(prop) {
return fmt.Errorf("Config property %s is required\n", prop)
}
}
return nil
}
// parseConfig takes the first non-flag arguments from flags and parses it
// into the viper config.
func parseConfig() {
if u, e := url.Parse(flag.Arg(0)); e == nil {
// If credentials were provided
if len(u.User.Username()) > 0 {
viper.Set("auth.username", u.User.Username())
// If the password wasn't provided, the program will fail as normal
if p, s := u.User.Password(); s {
viper.Set("auth.password", p)
}
}
// Blank out the credentials so we can use the URL formatting
u.User = nil
viper.Set("server.host", u.String())
} else {
fmt.Printf("Invalid server format; must be a valid URL!")
fmt.Printf("Usage: %s <args> [http[s]://[user:pass@]server:port]\n", os.Args[0])
osExit(1)
}
}
// initCommandHandler sets up tview-command as main input handler
func initCommandHandler(logger *logger.Logger) {
tviewcommand.SetLogHandler(func(msg string) {
logger.Print(msg)
})
configPath := "HACK.commands.toml"
// Load the configuration file
config, err := tviewcommand.LoadConfig(configPath)
if err != nil || config == nil {
logger.PrintError("Failed to load command-shortcut config", err)
}
//env := keybinding.SetupEnvironment()
//keybinding.RegisterCommands(env)
}
// return codes:
// 0 - OK
// 1 - generic errors
// 2 - main config errors
// 2 - keybinding config errors
func main() {
// parse flags and config
help := flag.Bool("help", false, "Print usage")
enableMpris := flag.Bool("mpris", false, "Enable MPRIS2")
list := flag.Bool("list", false, "list server data")
cpuprofile := flag.String("cpuprofile", "", "write cpu profile to `file`")
memprofile := flag.String("memprofile", "", "write memory profile to `file`")
configFile := flag.String("config", "", "use config `file`")
version := flag.Bool("version", false, "print the stmps version and exit")
flag.Parse()
if *help {
fmt.Printf("USAGE: %s <args> [[user:pass@]server:port]\n", os.Args[0])
flag.Usage()
osExit(0)
}
if Version == DEVELOPMENT {
if bi, ok := debug.ReadBuildInfo(); ok {
Version = bi.Main.Version
}
}
if *version {
fmt.Printf("stmps %s", Version)
osExit(0)
}
// cpu/memprofile code straight from https://pkg.go.dev/runtime/pprof
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal("could not create CPU profile: ", err)
}
defer f.Close() // error handling omitted for example
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal("could not start CPU profile: ", err)
}
defer pprof.StopCPUProfile()
}
// config gathering
if len(flag.Args()) > 0 {
parseConfig()
}
if err := readConfig(configFile); err != nil {
if configFile == nil {
fmt.Fprintf(os.Stderr, "Failed to read configuration: configuration file is nil\n")
} else {
fmt.Fprintf(os.Stderr, "Failed to read configuration from file '%s': %v\n", *configFile, err)
}
osExit(2)
}
logger := logger.Init()
initCommandHandler(logger)
// init mpv engine
player, err := mpvplayer.NewPlayer(logger)
if err != nil {
fmt.Println("Unable to initialize mpv. Is mpv installed?")
osExit(1)
}
var mprisPlayer *remote.MprisPlayer
// init mpris2 player control (linux only but fails gracefully on other systems)
if *enableMpris {
mprisPlayer, err = remote.RegisterMprisPlayer(player, logger)
if err != nil {
fmt.Printf("Unable to register MPRIS with DBUS: %s\n", err)
fmt.Println("Try running without MPRIS")
osExit(1)
}
defer mprisPlayer.Close()
}
// init macos mediaplayer control
if runtime.GOOS == "darwin" {
if err = remote.RegisterMPMediaHandler(player, logger); err != nil {
fmt.Printf("Unable to initialize MediaPlayer bindings: %s\n", err)
osExit(1)
} else {
logger.Print("MacOS MediaPlayer registered")
}
}
if testMode {
fmt.Println("Running in test mode for testing.")
osExit(0x23420001)
return
}
connection := subsonic.Init(logger)
connection.SetClientInfo(clientName, clientVersion)
connection.Username = viper.GetString("auth.username")
connection.Password = viper.GetString("auth.password")
connection.Host = viper.GetString("server.host")
connection.PlaintextAuth = viper.GetBool("auth.plaintext")
connection.Scrobble = viper.GetBool("server.scrobble")
connection.RandomSongNumber = viper.GetUint("client.random-songs")
indexResponse, err := connection.GetIndexes()
if err != nil {
fmt.Printf("Error fetching playlists from server: %s\n", err)
osExit(1)
}
if *list {
fmt.Printf("Index response:\n")
fmt.Printf(" Directory: %s\n", indexResponse.Directory.Name)
fmt.Printf(" Status: %s\n", indexResponse.Status)
fmt.Printf(" Error: %s\n", indexResponse.Error.Message)
fmt.Printf(" Playlist: %s\n", indexResponse.Playlist.Name)
fmt.Printf(" Playlists: (%d)\n", len(indexResponse.Playlists.Playlists))
for _, pl := range indexResponse.Playlists.Playlists {
fmt.Printf(" [%d] %s\n", pl.Entries.Len(), pl.Name)
}
fmt.Printf(" Indexes:\n")
for _, pl := range indexResponse.Indexes.Index {
fmt.Printf(" %s\n", pl.Name)
}
fmt.Printf("Playlist response: (this can take a while)\n")
playlistResponse, err := connection.GetPlaylists()
if err != nil {
fmt.Printf("Error fetching indexes from server: %s\n", err)
osExit(1)
}
fmt.Printf(" Directory: %s\n", playlistResponse.Directory.Name)
fmt.Printf(" Status: %s\n", playlistResponse.Status)
fmt.Printf(" Error: %s\n", playlistResponse.Error.Message)
fmt.Printf(" Playlist: %s\n", playlistResponse.Playlist.Name)
fmt.Printf(" Playlists: (%d)\n", len(indexResponse.Playlists.Playlists))
for _, pl := range playlistResponse.Playlists.Playlists {
fmt.Printf(" [%d] %s\n", pl.Entries.Len(), pl.Name)
}
fmt.Printf(" Indexes:\n")
for _, pl := range playlistResponse.Indexes.Index {
fmt.Printf(" %s\n", pl.Name)
}
osExit(0)
}
if headlessMode {
fmt.Println("Running in headless mode for testing.")
osExit(0)
return
}
ui := InitGui(&indexResponse.Indexes.Index,
connection,
player,
logger,
mprisPlayer)
// run main loop
if err := ui.Run(); err != nil {
panic(err)
}
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal("could not create memory profile: ", err)
}
defer f.Close() // error handling omitted for example
runtime.GC() // get up-to-date statistics
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}