2020-02-14 21:46:02 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"gitea.com/jolheiser/beaver"
|
|
|
|
"github.com/BurntSushi/toml"
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
configPath string
|
|
|
|
cfg *config
|
|
|
|
|
2020-02-16 06:51:14 +01:00
|
|
|
Remote string
|
|
|
|
Upstream string
|
|
|
|
Logins []Login
|
2020-02-14 21:46:02 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type config struct {
|
2020-02-16 06:51:14 +01:00
|
|
|
Remote string `toml:"remote"`
|
|
|
|
Upstream string `toml:"upstream"`
|
|
|
|
Logins []Login `toml:"login"`
|
2020-02-14 21:46:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-02-16 06:51:14 +01:00
|
|
|
Remote = cfg.Remote
|
|
|
|
Upstream = cfg.Upstream
|
2020-02-14 21:46:02 +01:00
|
|
|
Logins = cfg.Logins
|
|
|
|
}
|
|
|
|
|
|
|
|
func Save() error {
|
2020-02-16 06:51:14 +01:00
|
|
|
cfg.Remote = Remote
|
|
|
|
cfg.Upstream = Upstream
|
2020-02-14 21:46:02 +01:00
|
|
|
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
|
|
|
|
}
|