add handler for config file. Generate one if none is present

This commit is contained in:
Greg Gauthier 2021-03-17 08:47:51 +00:00
parent 73dc128eb9
commit ccca9cde16
2 changed files with 58 additions and 2 deletions

View File

@ -19,9 +19,28 @@ func str2int(strnum string) int {
return i
}
func configStat(configFile string) string{
xdgConfigPath := os.Getenv("XDG_CONFIG_HOME")
if xdgConfigPath == "" {
xdgConfigPath = os.Getenv("HOME")+"/.config"
}
configFile = xdgConfigPath + "/gostations/" + configFile
if _, err := os.Stat(configFile); errors.Is(err, os.ErrNotExist) {
log.Printf("Your stations config file seems to be missing. A default will be generated.")
err = createIniFile(configFile)
if err != nil {
log.Printf("Erorr creating ini file...")
log.Fatal(err.Error())
}
}
return configFile
}
func Config(option string) (string, error) {
configparser.Delimiter = "="
configFile := "radiostations.ini"
configFile := configStat("radiostations.ini")
runtimeSection := "DEFAULT"
config, err := configparser.Read(configFile)

37
filer.go Normal file
View File

@ -0,0 +1,37 @@
package main
import (
"log"
"os"
"path/filepath"
)
func createDir(fpath string) error {
if err := os.Mkdir(filepath.Dir(fpath + string(filepath.Separator)), 0770); err != nil {
return err
}
return nil
}
func createIniFile(fpath string) error {
log.Printf("Creating config file: %s\n", fpath)
if err := os.MkdirAll(filepath.Dir(fpath), 0770); err != nil {
return err
}
file, err := os.Create(fpath)
if err != nil {
log.Fatal(err.Error())
return err
}
_, err = file.Write([]byte("[DEFAULT]\n"))
_, err = file.Write([]byte("radio_browser.api=all.api.radio-browser.info\n"))
_, err = file.Write([]byte("player.command=mpv\n"))
_, err = file.Write([]byte("player.options=--no-video\n"))
_, err = file.Write([]byte("menu_items.max=9999\n"))
defer file.Close()
return err
}