1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-23 04:12:00 +01:00
sip/modules/config/config.go
John Olheiser 448e12ce38 Imp formatting (#17)
Ran imp

Signed-off-by: jolheiser <john.olheiser@gmail.com>

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/sip/pulls/17
2020-05-07 01:09:31 +00:00

80 lines
1.5 KiB
Go

package config
import (
"fmt"
"os"
"path"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"go.jolheiser.com/beaver"
)
var (
configPath string
cfg *config
// Config items
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"`
}
// Load on init so that CLI contexts are correctly populated
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
}