add handler for config file. Generate one if none is present
This commit is contained in:
parent
73dc128eb9
commit
ccca9cde16
23
config.go
23
config.go
@ -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)
|
||||
@ -59,4 +78,4 @@ func options() string {
|
||||
func maxitems() int {
|
||||
items, _ := Config("menu_items.max")
|
||||
return str2int(items)
|
||||
}
|
||||
}
|
||||
|
37
filer.go
Normal file
37
filer.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user