diff options
author | Franck Cuny <franck@fcuny.net> | 2021-10-10 16:10:32 -0700 |
---|---|---|
committer | Franck Cuny <franck@fcuny.net> | 2022-06-11 14:32:08 -0700 |
commit | bb52f9d45b2524633b944a6c6117f4802d379791 (patch) | |
tree | f6aa5d08537dea0d9f0ed97dceba57b999ba896a | |
parent | scrobbler: use helper function EqualAttrs (diff) | |
download | world-bb52f9d45b2524633b944a6c6117f4802d379791.tar.gz |
mpd-scrobbler: proper default arguments
The program needs two arguments: the mpd host and port, which can be passed as flags (default is to use the local instance of mpd). We store the database in `XDG_CONFIG_HOME/mpd-scrobbler`, and we create the path if needed.
Diffstat (limited to '')
-rw-r--r-- | tools/mpd-stats/cmd/mpd-scrobbler/main.go | 33 |
1 files changed, 31 insertions, 2 deletions
diff --git a/tools/mpd-stats/cmd/mpd-scrobbler/main.go b/tools/mpd-stats/cmd/mpd-scrobbler/main.go index fdd9e4d..c2693a4 100644 --- a/tools/mpd-stats/cmd/mpd-scrobbler/main.go +++ b/tools/mpd-stats/cmd/mpd-scrobbler/main.go @@ -1,17 +1,29 @@ package main import ( + "flag" "fmt" "log" "os" + "path/filepath" "golang.fcuny.net/mpd-stats/internal/scrobbler" ) func main() { + var ( + mpdHost = flag.String("host", "localhost", "The MPD server to connect to (default: localhost)") + mpdPort = flag.Int("port", 6600, "The TCP port of the MPD server to connect to (default: 6600)") + ) + flag.Parse() + net := "tcp" - addr := "localhost:6600" - dbpath := fmt.Sprintf("%s/.config/scrobbler.sql", os.Getenv("HOME")) + addr := fmt.Sprintf("%s:%d", *mpdHost, *mpdPort) + + dbpath, err := getDbPath() + if err != nil { + log.Fatalf("failed to get the path to the database: %v", err) + } s, err := scrobbler.NewScrobbler(net, addr, dbpath) if err != nil { @@ -26,3 +38,20 @@ func main() { s.Run() } + +func getDbPath() (string, error) { + xch := os.Getenv("XDG_CONFIG_HOME") + if xch == "" { + home := os.Getenv("HOME") + xch = filepath.Join(home, ".config") + } + + scrobblerHome := filepath.Join(xch, "mpd-scrobbler") + if _, err := os.Stat(scrobblerHome); os.IsNotExist(err) { + if err := os.Mkdir(scrobblerHome, 0755); err != nil { + return "", err + } + } + + return filepath.Join(scrobblerHome, "scrobbler.sql"), nil +} |