56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package config
|
|
|
|
import (
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
Logging struct {
|
|
Format string `yaml:"format"`
|
|
Level string `yaml:"level"`
|
|
Path string `yaml:"path"`
|
|
} `yaml:"logging"`
|
|
Clipboard struct {
|
|
MaxItems int `yaml:"max_items"`
|
|
} `yaml:"clipboard"`
|
|
}
|
|
|
|
func logFilePath() string {
|
|
usr, err := user.Current()
|
|
if err != nil {
|
|
return "./kcm.log"
|
|
}
|
|
dir := filepath.Join(usr.HomeDir, ".local", "share", "kcm")
|
|
os.MkdirAll(dir, 0700)
|
|
return filepath.Join(dir, "kcm.log")
|
|
}
|
|
|
|
func LoadConfig() (Config, string) {
|
|
paths := []string{"/etc/kcm/config.yml", "./config.yml"}
|
|
var cfg Config
|
|
for _, path := range paths {
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
d := yaml.NewDecoder(file)
|
|
if err := d.Decode(&cfg); err == nil {
|
|
file.Close()
|
|
if cfg.Logging.Path == "" {
|
|
cfg.Logging.Path = logFilePath()
|
|
}
|
|
return cfg, path
|
|
}
|
|
file.Close()
|
|
}
|
|
|
|
cfg.Logging.Format = "console"
|
|
cfg.Logging.Level = "info"
|
|
cfg.Logging.Path = logFilePath()
|
|
return cfg, ""
|
|
}
|