95 lines
2.2 KiB
Go
95 lines
2.2 KiB
Go
package main
|
|
|
|
// this file will handle configuration file reading
|
|
// and also some database functions
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"strings"
|
|
"github.com/spf13/viper"
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
//"strconv"
|
|
"time"
|
|
)
|
|
|
|
var ircVersion string = "sh0rtbus 0.5"
|
|
var verShort string = "0.5"
|
|
|
|
var hlf io.Writer
|
|
|
|
func readConfig() {
|
|
|
|
//////////////////// defaults //////
|
|
|
|
defSizes := map[string]interface{}{
|
|
"activewindows": 80,
|
|
"workspace": 50,
|
|
}
|
|
|
|
defColors := map[string]interface{}{
|
|
"activewindows": 80,
|
|
"workspace": 50,
|
|
}
|
|
|
|
viper.SetDefault("sizes", defSizes)
|
|
viper.SetDefault("colors", defColors)
|
|
|
|
//////////////////////////////////////
|
|
|
|
home, err := os.UserHomeDir()
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("Failed to determine the users home directory!?")
|
|
}
|
|
|
|
viper.SetConfigType("toml")
|
|
|
|
// -c command line flag to override conig file
|
|
// to override config file location
|
|
if len(os.Args) > 2 && os.Args[1] == "-c" {
|
|
log.Info().Str("config", os.Args[2]).Msg("overriding default config file location...")
|
|
f, err := os.Open(os.Args[2])
|
|
if err != nil {
|
|
log.Fatal().Str("config", os.Args[2]).Msg("failed to read config file")
|
|
}
|
|
defer f.Close()
|
|
buf, err := ioutil.ReadAll(f)
|
|
if err != nil {
|
|
log.Fatal().Str("config", os.Args[2]).Msg("failed to read config file")
|
|
}
|
|
err = viper.ReadConfig(bytes.NewBuffer(buf))
|
|
} else {
|
|
viper.SetConfigName("5bar5")
|
|
viper.AddConfigPath(".")
|
|
viper.AddConfigPath(home + "/.5bar5/")
|
|
viper.AddConfigPath(home + "/.config/5bar5/")
|
|
// read config filr and check for errors
|
|
log.Info().Msg("reading configuration file...")
|
|
err = viper.ReadInConfig()
|
|
}
|
|
|
|
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
log.Warn().Msg("Config file not found! Wrting new config to config.toml")
|
|
err = viper.SafeWriteConfigAs(home + "/.5bar5/5bar5.toml")
|
|
if err != nil {
|
|
log.Fatal().Err(err).Msg("Error writing new configuration file")
|
|
}
|
|
|
|
} else if err != nil {
|
|
log.Fatal().Err(err).Msg("Fatal error when loading configuration file")
|
|
}
|
|
|
|
/////////
|
|
// Sizes
|
|
activeWinSize = viper.GetInt("sizes.activewindow")
|
|
workspaceWidth = viper.GetInt("sizes.workspace")
|
|
/////////
|
|
// Colors
|
|
var err error
|
|
activeWinBG, err = Hex2RGB(viper.GetString("colors.activewindowbg"))
|
|
if err != nil {
|
|
|
|
}
|