Old and unmaintained
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.4 KiB

package main
import (
"encoding/json"
"io"
"io/ioutil"
"os"
)
// config holds the configuration for musicd.
type config struct {
HTTPPort int `json:"httpPort"`
Playlists playlists `json:"playlists"`
// MPDURL is the URL MPD listens on.
MPDURL string `json:"mpdURL"`
// SpeakBaseURL is the HTTP URL for communicating with speakd.
SpeakdBaseURL string `json:"speakdBaseURL"`
// SpeakdCallbackBaseURL is the HTTP URL speakd can use to send callbacks to this
// musicd. It should be the same as the HTTP server of musicd, thus also the
// same port as set in HTTPPort.
SpeakdCallbackBaseURL string `json:"speakdCallbackBaseURL"`
}
type playlists map[string]playlist
type playlist struct {
// MpdName is the playlist name known to MPD.
MpdName string `json:"mpdName,omitempty"`
// SpeakNote, if set, is spoken when speaking musicd's status with speakd.
SpeakNote string `json:"speakNote,omitempty"`
// SpeakIgnoreTitle, if true, the current song's/item's title won't be spoken
// with speakd.
SpeakIgnoreTitle bool `json:"speakIgnoreTitle,omitempty"`
}
// defaultConfig is written to the configuration file by loadConfig in JSON when the
// file doesn't exist yet.
var defaultConfig = config{
HTTPPort: 8002,
Playlists: playlists{
"playlist1": {
MpdName: "example-playlist-name-known-to-mpd",
SpeakNote: "Example playlist",
},
},
MPDURL: "127.0.0.1:6600",
SpeakdBaseURL: "http://127.0.0.1:8001",
SpeakdCallbackBaseURL: "http://127.0.0.1:8002",
}
// loadConfig unmarhsals the contents of the file with given filename as JSON, which
// is returned.
// If there is no file with the given filename the file will be created containing
// defaultConfig as JSON. isNew is then set to true.
func loadConfig(filename string) (cfg config, isNew bool, err error) {
f, err := os.Open(filename)
if err != nil {
// Configuration file probably doesn't exist.
// Create it with defaultConfig in JSON as contents.
err := createConfig(filename, defaultConfig)
return defaultConfig, true, err
}
dec := json.NewDecoder(f)
err = dec.Decode(&cfg)
if err != nil && err != io.EOF {
return cfg, isNew, err
}
return cfg, isNew, nil
}
func createConfig(filename string, cfg config) (err error) {
data, err := json.MarshalIndent(cfg, "", " ")
if err != nil {
return err
}
err = ioutil.WriteFile(filename, data, 0600)
if err != nil {
return err
}
return nil
}