2021-01-04 03:41:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/pelletier/go-toml"
|
|
|
|
"io/ioutil"
|
2021-01-04 10:33:29 +00:00
|
|
|
"path/filepath"
|
2021-01-04 03:41:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config contains the root of the TOML config file
|
|
|
|
type Config struct {
|
|
|
|
ActiveManager string
|
2021-01-04 10:33:29 +00:00
|
|
|
RootCommand string
|
|
|
|
Managers map[string]Manager
|
2021-01-04 03:41:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Manager contains the root of all manager sections in the TOML config file
|
|
|
|
type Manager struct {
|
2021-01-04 10:33:29 +00:00
|
|
|
UseRoot bool
|
|
|
|
Commands map[string]string
|
2021-01-04 03:41:26 +00:00
|
|
|
Shortcuts map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new Config{} with values from file path given
|
|
|
|
func NewConfig(path string) Config {
|
|
|
|
// Read file at path
|
|
|
|
data, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2021-01-04 10:33:29 +00:00
|
|
|
Log.Fatal().Err(err).Str("file", filepath.Base(path)).Msg("Error reading config")
|
2021-01-04 03:41:26 +00:00
|
|
|
}
|
|
|
|
// Create new Config{}
|
|
|
|
cfg := Config{}
|
|
|
|
// Unmarshal TOML in config
|
|
|
|
err = toml.Unmarshal(data, &cfg)
|
|
|
|
if err != nil {
|
2021-01-04 10:33:29 +00:00
|
|
|
Log.Fatal().Err(err).Str("file", filepath.Base(path)).Msg("Error unmarshalling TOML")
|
2021-01-04 03:41:26 +00:00
|
|
|
}
|
|
|
|
// Return config
|
|
|
|
return cfg
|
|
|
|
}
|