54 lines
929 B
Go
54 lines
929 B
Go
package main
|
|
|
|
import (
|
|
"os"
|
|
"log"
|
|
"io/ioutil"
|
|
"encoding/json"
|
|
)
|
|
|
|
type config struct {
|
|
Host string `json:"host"`
|
|
Path string `json:"path"`
|
|
User string `json:"user"`
|
|
Token string `json:"token"`
|
|
}
|
|
|
|
func handleTilde(in string) string {
|
|
if in[0] != '~' {
|
|
return in
|
|
}
|
|
return os.Getenv("HOME") + in[1:]
|
|
}
|
|
|
|
func configPath() string {
|
|
confHome := os.Getenv("XDG_CONFIG_HOME")
|
|
if confHome == "" {
|
|
confHome = "~/.config"
|
|
}
|
|
confDir := handleTilde(confHome) + "/linger"
|
|
_ = os.Mkdir(confDir, 0755)
|
|
return confDir + "/config.json"
|
|
}
|
|
|
|
func configLoad() *config {
|
|
conf := config{}
|
|
data, err := ioutil.ReadFile(configPath())
|
|
if err != nil {
|
|
return &conf
|
|
}
|
|
err = json.Unmarshal(data, &conf)
|
|
if err != nil {
|
|
return &conf
|
|
}
|
|
return &conf
|
|
}
|
|
|
|
func configWrite(conf *config) {
|
|
data, err := json.Marshal(conf)
|
|
err = ioutil.WriteFile(configPath(), data, 0644)
|
|
if err != nil {
|
|
log.Fatal("Failed to write config")
|
|
}
|
|
}
|