forked from wildeyedskies/stmp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stmps.go
111 lines (94 loc) · 2.63 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
// Copyright 2023 The STMPS Authors
// SPDX-License-Identifier: GPL-3.0-only
package main
import (
"flag"
"fmt"
"os"
"runtime"
"github.com/spezifisch/stmps/logger"
"github.com/spezifisch/stmps/mpvplayer"
"github.com/spezifisch/stmps/remote"
"github.com/spezifisch/stmps/subsonic"
"github.com/spf13/viper"
)
func readConfig() {
required_properties := []string{"auth.username", "auth.password", "server.host"}
viper.SetConfigName("stmp")
viper.SetConfigType("toml")
viper.AddConfigPath("$HOME/.config/stmp")
viper.AddConfigPath(".")
err := viper.ReadInConfig()
if err != nil {
fmt.Printf("Config file error: %s \n", err)
os.Exit(1)
}
for _, prop := range required_properties {
if !viper.IsSet(prop) {
fmt.Printf("Config property %s is required\n", prop)
}
}
}
func main() {
help := flag.Bool("help", false, "Print usage")
enableMpris := flag.Bool("mpris", false, "Enable MPRIS2")
flag.Parse()
if *help {
fmt.Printf("USAGE: %s <args>\n", os.Args[0])
flag.Usage()
os.Exit(0)
}
readConfig()
logger := logger.Init()
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")
indexResponse, err := connection.GetIndexes()
if err != nil {
fmt.Printf("Error fetching indexes from server: %s\n", err)
os.Exit(1)
}
playlistResponse, err := connection.GetPlaylists()
if err != nil {
fmt.Printf("Error fetching indexes from server: %s\n", err)
os.Exit(1)
}
// init mpv engine
player, err := mpvplayer.NewPlayer(logger)
if err != nil {
fmt.Println("Unable to initialize mpv. Is mpv installed?")
os.Exit(1)
}
// init mpris2 player control (linux only but fails gracefully on other systems)
if *enableMpris {
mpris, 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")
os.Exit(1)
}
defer mpris.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)
os.Exit(1)
} else {
logger.Print("MacOS MediaPlayer registered")
}
}
ui := InitGui(&indexResponse.Indexes.Index,
&playlistResponse.Playlists.Playlists,
connection,
player,
logger)
// run main loop
if err := ui.Run(); err != nil {
panic(err)
}
}