1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-22 19:51:58 +01:00
sip/modules/config/config.go

76 lines
1.4 KiB
Go
Raw Normal View History

package config
import (
"fmt"
"gitea.com/jolheiser/beaver"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"os"
"path"
)
var (
configPath string
cfg *config
Remote string
Upstream string
Logins []Login
)
type config struct {
Remote string `toml:"remote"`
Upstream string `toml:"upstream"`
Logins []Login `toml:"login"`
}
type Login struct {
Name string `toml:"name"`
URL string `toml:"url"`
Token string `toml:"token"`
}
func init() {
home, err := homedir.Dir()
if err != nil {
beaver.Fatalf("could not locate home directory: %v", err)
}
configPath = fmt.Sprintf("%s/.tea/config.toml", home)
if _, err := os.Stat(configPath); os.IsNotExist(err) {
if err := os.MkdirAll(path.Dir(configPath), os.ModePerm); err != nil {
beaver.Fatalf("could not create Tea home: %v", err)
}
if _, err := os.Create(configPath); err != nil {
beaver.Fatalf("could not create Tea config: %v", err)
}
}
if _, err := toml.DecodeFile(configPath, &cfg); err != nil {
beaver.Fatalf("could not decode Tea config: %v", err)
}
Remote = cfg.Remote
Upstream = cfg.Upstream
Logins = cfg.Logins
}
func Save() error {
cfg.Remote = Remote
cfg.Upstream = Upstream
cfg.Logins = Logins
fi, err := os.Create(configPath)
if err != nil {
return err
}
defer fi.Close()
if err := toml.NewEncoder(fi).Encode(cfg); err != nil {
return err
}
return nil
}