2022-03-08 01:17:24 +04:00
|
|
|
package configuration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Configuration struct {
|
|
|
|
ListenOn string `yaml:"listen_on"`
|
|
|
|
Icecast struct {
|
|
|
|
URL string `yaml:"url"`
|
|
|
|
Playlist string `yaml:"playlist_path"`
|
|
|
|
} `yaml:"icecast"`
|
2023-02-19 21:18:36 +04:00
|
|
|
FilelistPath string `yaml:"filelist_path"`
|
|
|
|
Liquidsoap struct {
|
2022-08-29 07:17:40 +04:00
|
|
|
ExecPath string `yaml:"executable_path"`
|
|
|
|
ScriptPath string `yaml:"script_path"`
|
|
|
|
} `yaml:"liquidsoap"`
|
2022-03-08 01:17:24 +04:00
|
|
|
ListLastNSongs int `yaml:"list_last_n_songs"`
|
|
|
|
Log struct {
|
2023-02-07 02:24:44 +04:00
|
|
|
Error string `yaml:"error"`
|
2022-03-08 01:17:24 +04:00
|
|
|
} `yaml:"log"`
|
|
|
|
}
|
|
|
|
|
2023-02-07 02:29:45 +04:00
|
|
|
// Load reads a YAML file that stores configuration of a service.
|
|
|
|
func Load(path string) (*Configuration, error) {
|
2022-03-08 01:17:24 +04:00
|
|
|
configFile, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to open configuration file")
|
|
|
|
}
|
|
|
|
defer configFile.Close()
|
|
|
|
|
|
|
|
config := &Configuration{}
|
|
|
|
|
|
|
|
if err := yaml.NewDecoder(configFile).Decode(config); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to parse configuration file")
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
2023-02-07 02:29:45 +04:00
|
|
|
// SplitNetworkAddress splits ListenOn option into network type (e.g. tcp, unix,
|
|
|
|
// udp) and address:port or /path/to/service.socket to listen on.
|
2022-03-08 01:17:24 +04:00
|
|
|
func (c *Configuration) SplitNetworkAddress() (string, string) {
|
|
|
|
s := strings.Split(c.ListenOn, " ")
|
|
|
|
return s[0], s[1]
|
|
|
|
}
|