mirror of
https://gitea.com/jolheiser/sip
synced 2024-11-23 04:12:00 +01:00
5b7b970a06
Signed-off-by: jolheiser <john.olheiser@gmail.com>
68 lines
1.2 KiB
Go
68 lines
1.2 KiB
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"gitea.com/jolheiser/beaver"
|
|
"github.com/BurntSushi/toml"
|
|
"github.com/mitchellh/go-homedir"
|
|
"os"
|
|
"path"
|
|
)
|
|
|
|
var (
|
|
configPath string
|
|
cfg *config
|
|
|
|
Logins []Login
|
|
)
|
|
|
|
type config struct {
|
|
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)
|
|
}
|
|
|
|
Logins = cfg.Logins
|
|
}
|
|
|
|
func Save() error {
|
|
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
|
|
}
|