1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-26 07:33:48 +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
Origin string
Upstream string
Tokens []Token
)
type config struct {
Origin string `toml:"origin"`
Upstream string `toml:"upstream"`
Tokens []Token `toml:"token"`
}
type Token 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/.sip/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 Sip home: %v", err)
}
if _, err := os.Create(configPath); err != nil {
beaver.Fatalf("could not create Sip config: %v", err)
}
}
if _, err := toml.DecodeFile(configPath, &cfg); err != nil {
beaver.Fatalf("could not decode Sip config: %v", err)
}
Origin = cfg.Origin
Upstream = cfg.Upstream
Tokens = cfg.Tokens
}
func Save() error {
cfg.Origin = Origin
cfg.Upstream = Upstream
cfg.Tokens = Tokens
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
}