2021-11-11 09:33:35 -07:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2021-12-16 12:36:18 -07:00
|
|
|
"github.com/aditya-K2/gomp/utils"
|
2021-11-11 09:33:35 -07:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2021-12-08 13:58:42 -07:00
|
|
|
HOME_DIR, _ = os.UserHomeDir()
|
|
|
|
USER_CACHE_DIR, _ = os.UserCacheDir()
|
|
|
|
defaults = map[string]interface{}{
|
2021-11-11 09:33:35 -07:00
|
|
|
"ADDITIONAL_PADDING_X": 12,
|
|
|
|
"ADDITIONAL_PADDING_Y": 16,
|
|
|
|
"IMAGE_WIDTH_EXTRA_X": -1.5,
|
|
|
|
"IMAGE_WIDTH_EXTRA_Y": -3.75,
|
2022-08-30 23:58:02 -06:00
|
|
|
"NETWORK_TYPE": "tcp",
|
|
|
|
"NETWORK_ADDRESS": "localhost",
|
2021-12-12 13:05:40 -07:00
|
|
|
"MUSIC_DIRECTORY": utils.CheckDirectoryFmt(getMusicDirectory()),
|
2021-11-11 09:33:35 -07:00
|
|
|
"PORT": "6600",
|
|
|
|
"DEFAULT_IMAGE_PATH": "default.jpg",
|
2021-12-12 13:05:40 -07:00
|
|
|
"CACHE_DIR": utils.CheckDirectoryFmt(USER_CACHE_DIR),
|
2022-09-08 14:18:05 -06:00
|
|
|
"SEEK_OFFSET": 1,
|
2021-11-11 09:33:35 -07:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func ReadConfig() {
|
2022-09-12 11:46:24 -06:00
|
|
|
|
2021-11-11 09:33:35 -07:00
|
|
|
for k, v := range defaults {
|
|
|
|
viper.SetDefault(k, v)
|
|
|
|
}
|
2022-09-12 11:46:24 -06:00
|
|
|
|
2021-11-11 09:33:35 -07:00
|
|
|
viper.SetConfigName("config")
|
2021-12-18 10:55:47 -07:00
|
|
|
viper.AddConfigPath(HOME_DIR + "/.config/gomp")
|
2022-09-12 11:46:24 -06:00
|
|
|
|
2021-11-11 09:33:35 -07:00
|
|
|
err := viper.ReadInConfig()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Could Not Read Config file.")
|
|
|
|
}
|
2022-09-12 11:46:24 -06:00
|
|
|
|
|
|
|
// Expanding ~ to the User's Home Directory
|
|
|
|
viper.Set("MUSIC_DIRECTORY", utils.ExpandHomeDir(viper.GetString("MUSIC_DIRECTORY")))
|
|
|
|
viper.Set("DEFAULT_IMAGE_PATH", utils.ExpandHomeDir(viper.GetString("DEFAULT_IMAGE_PATH")))
|
|
|
|
viper.Set("CACHE_DIR", utils.ExpandHomeDir(viper.GetString("CACHE_DIR")))
|
2022-09-14 12:36:18 -06:00
|
|
|
viper.Set("NETWORK_ADDRESS", utils.ExpandHomeDir(viper.GetString("NETWORK_ADDRESS")))
|
2021-11-11 09:33:35 -07:00
|
|
|
}
|
|
|
|
|
2021-11-11 10:54:04 -07:00
|
|
|
func GenerateKeyMap(funcMap map[string]func()) {
|
2021-11-11 09:33:35 -07:00
|
|
|
for k := range funcMap {
|
2021-11-11 11:28:57 -07:00
|
|
|
mappingsForFunctionK := viper.GetStringSlice(k)
|
|
|
|
if len(mappingsForFunctionK) != 0 {
|
|
|
|
for _, i := range mappingsForFunctionK {
|
|
|
|
aV, err := GetAsciiValue(i)
|
|
|
|
if err == nil {
|
|
|
|
KEY_MAP[aV] = k
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-11 09:33:35 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMusicDirectory() string {
|
|
|
|
content, err := ioutil.ReadFile(HOME_DIR + "/.config/mpd/mpd.conf")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("No Config File for mpd Found")
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
ab := string(content)
|
|
|
|
maps := strings.Split(ab, "\n")
|
|
|
|
for _, j := range maps {
|
|
|
|
if strings.Contains(j, "music_directory") {
|
|
|
|
s := strings.SplitAfter(strings.ReplaceAll(j, " ", ""), "y")[1]
|
|
|
|
s = strings.ReplaceAll(s, "\t", "")
|
|
|
|
d := ""
|
|
|
|
for z, m := range s {
|
|
|
|
if (z != 0) && (z != (len(s) - 1)) {
|
|
|
|
d += string(m)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|