From 237155552a0ee745e4e36a593fb30e905b0f5d63 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Tue, 16 Mar 2021 12:32:59 +0000 Subject: [PATCH] completed the config port --- commander.go | 22 +++++++++++++++++ config.go | 62 +++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 4 ++- go.sum | 2 ++ radiostations.ini | 5 ++++ stations.go | 12 +++++++++ 6 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 commander.go create mode 100644 config.go create mode 100644 radiostations.ini create mode 100644 stations.go diff --git a/commander.go b/commander.go new file mode 100644 index 0000000..8adf706 --- /dev/null +++ b/commander.go @@ -0,0 +1,22 @@ +package main + +import ( + "os/exec" + "strings" +) + +func isInstalled(name string) bool { + cmd := exec.Command("/bin/sh", "-c", "command -v " + name) + if err := cmd.Run(); err != nil { + return false + } + return true +} + +func execute(cmdstr string) (string, error) { + cmdargs := strings.Split(cmdstr, " ") // string arrayified + cmd := cmdargs[0] // command + cmdargs = append(cmdargs[:0], cmdargs[1:]...) // argument array sans cmd + out, err := exec.Command(cmd, cmdargs...).CombinedOutput() + return string(out[:]), err +} diff --git a/config.go b/config.go new file mode 100644 index 0000000..d5e7730 --- /dev/null +++ b/config.go @@ -0,0 +1,62 @@ +package main + +import ( + "errors" + "fmt" + "log" + "os" + "strconv" + + "github.com/alyu/configparser" +) + +func str2int(strnum string) int { + i, err := strconv.Atoi(strnum) + if err != nil { + fmt.Println(err) + os.Exit(2) + } + return i +} + +func Config(option string) (string, error) { + configparser.Delimiter = "=" + configFile := "radiostations.ini" + runtimeSection := "DEFAULT" + + config, err := configparser.Read(configFile) + if err != nil { + log.Fatal(err) + } + + section, err := config.Section(runtimeSection) + if err != nil { + log.Fatal(err) + } + + optval := section.Options()[option] + if optval == "" { + return "", errors.New("no value for option '%s'") + } + return optval, nil +} + +func api() string { + url, _ := Config("radio_browser.api") + return url +} + +func player() string { + player, _ := Config("player.command") + return player +} + +func options() string { + options, _ := Config("player.options") + return options +} + +func maxitems() int { + items, _ := Config("menu_items.max") + return str2int(items) +} \ No newline at end of file diff --git a/go.mod b/go.mod index 24cad62..63c67f4 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ -module gostations +module github.com/gmgauthier/gostations go 1.16 + +require github.com/alyu/configparser v0.0.0-20191103060215-744e9a66e7bc diff --git a/go.sum b/go.sum index e69de29..e386739 100644 --- a/go.sum +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/alyu/configparser v0.0.0-20191103060215-744e9a66e7bc h1:eN2FUvn4J1A31pICABioDYukoh1Tmlei6L3ImZUin/I= +github.com/alyu/configparser v0.0.0-20191103060215-744e9a66e7bc/go.mod h1:BYq/NZTroWuzkvsTPJgRBqSHGxKMHCz06gtlfY/W5RU= diff --git a/radiostations.ini b/radiostations.ini new file mode 100644 index 0000000..670de80 --- /dev/null +++ b/radiostations.ini @@ -0,0 +1,5 @@ +[DEFAULT] +radio_browser.api=all.api.radio-browser.info +player.command=mpv +player.options=--no-video +menu_items.max=9999 diff --git a/stations.go b/stations.go new file mode 100644 index 0000000..c6dbd13 --- /dev/null +++ b/stations.go @@ -0,0 +1,12 @@ +package main + +import "fmt" + +func main(){ + fmt.Println(api()) + fmt.Println(player()) + fmt.Println(options()) + fmt.Println(maxitems()) + + +} \ No newline at end of file