Archived
1
0
This repository has been archived on 2021-02-24. You can view files and clone it, but cannot push or open issues or pull requests.
linger/config.go
2021-02-24 00:14:24 +00:00

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")
}
}