2024-01-16 15:21:35 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
2024-12-18 15:29:16 +00:00
|
|
|
"io/fs"
|
2024-01-16 15:21:35 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/creasty/defaults"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
var configPath string
|
|
|
|
|
|
|
|
type _config struct {
|
|
|
|
Username string `yaml:"username"`
|
|
|
|
Password string `yaml:"password"`
|
|
|
|
URL string `yaml:"url"`
|
2024-12-18 15:29:16 +00:00
|
|
|
EnableScrobble bool `default:"false" yaml:"enableScrobble"`
|
|
|
|
MaxRadioSongs int `default:"50" yaml:"maxRadioSongs"`
|
|
|
|
ExperimentalRadioAlgo bool `default:"false" yaml:"experimentalRadioAlgo"`
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var configStruct *_config
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
userConfigDir, err := os.UserConfigDir()
|
|
|
|
configDir := path.Join(userConfigDir, "subsonictui")
|
|
|
|
configPath = path.Join(configDir, "config.yaml")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[ERROR] Failed to fetch user config directory. %e\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if _, err := os.Stat(configDir); os.IsNotExist(err) {
|
2024-12-18 15:29:16 +00:00
|
|
|
var permissions fs.FileMode = 0o700
|
|
|
|
|
|
|
|
err := os.MkdirAll(configDir, permissions)
|
2024-01-16 15:21:35 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
var configFile *os.File
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
|
|
|
configFile, err = os.Create(configPath)
|
|
|
|
defer func() {
|
|
|
|
err := configFile.Close()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[ERROR] Failed to create config file @ %s. %e\n", configPath, err)
|
2024-12-18 15:29:16 +00:00
|
|
|
panic("unable to create config file")
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
configStruct, err = loadConfig()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[ERROR] Failed to load config file @ %s. %e\n", configPath, err)
|
2024-12-18 15:29:16 +00:00
|
|
|
panic("unable to load config file")
|
2024-01-16 15:21:35 +00:00
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
fmt.Printf("Init Config %s\n", configPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
func URL() string {
|
|
|
|
return configStruct.URL
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
func Username() string {
|
|
|
|
return configStruct.Username
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
func Password() string {
|
|
|
|
p, _ := base64.StdEncoding.DecodeString(configStruct.Password)
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return strings.TrimSpace(string(p))
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
func ScrobbleEnabled() bool {
|
|
|
|
return configStruct.EnableScrobble
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
func MaxRadioSongs() int {
|
|
|
|
return configStruct.MaxRadioSongs
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
func ExperimentalRadioAlgo() bool {
|
|
|
|
return configStruct.ExperimentalRadioAlgo
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetPassword(p string) {
|
|
|
|
configStruct.Password = base64.StdEncoding.EncodeToString([]byte(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetUsername(u string) {
|
|
|
|
configStruct.Username = u
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetURL(u string) {
|
|
|
|
configStruct.URL = u
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadConfig() (*_config, error) {
|
|
|
|
c := &_config{}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
err := defaults.Set(c)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
file, err := os.ReadFile(configPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
if err = yaml.Unmarshal(file, c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
2024-01-16 15:21:35 +00:00
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func SaveConfig() {
|
|
|
|
yml, err := yaml.Marshal(configStruct)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[ERROR] Failed to convert config to yaml. %e\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2024-12-18 15:29:16 +00:00
|
|
|
|
|
|
|
var permissions fs.FileMode = 0o600
|
|
|
|
|
|
|
|
err = os.WriteFile(configPath, yml, permissions)
|
2024-01-16 15:21:35 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("[ERROR] Failed to save config file @ %s. %e\n", configPath, err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|