completed the config port

This commit is contained in:
Greg Gauthier 2021-03-16 12:32:59 +00:00
parent 51b5d63eec
commit 237155552a
6 changed files with 106 additions and 1 deletions

22
commander.go Normal file
View File

@ -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
}

62
config.go Normal file
View File

@ -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)
}

4
go.mod
View File

@ -1,3 +1,5 @@
module gostations module github.com/gmgauthier/gostations
go 1.16 go 1.16
require github.com/alyu/configparser v0.0.0-20191103060215-744e9a66e7bc

2
go.sum
View File

@ -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=

5
radiostations.ini Normal file
View File

@ -0,0 +1,5 @@
[DEFAULT]
radio_browser.api=all.api.radio-browser.info
player.command=mpv
player.options=--no-video
menu_items.max=9999

12
stations.go Normal file
View File

@ -0,0 +1,12 @@
package main
import "fmt"
func main(){
fmt.Println(api())
fmt.Println(player())
fmt.Println(options())
fmt.Println(maxitems())
}