1
0
mirror of https://gitea.com/jolheiser/sip synced 2024-11-26 07:33:48 +01:00
sip/modules/config/config.go
John Olheiser bb773780df Update Beaver and fix bugs (#6)
Fix CI test

Fix origin names in PR create

Add aliases for new issue/PR and fix issues search

Update beaver and squash some bugs

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

Co-authored-by: jolheiser <john.olheiser@gmail.com>
Reviewed-on: https://gitea.com/jolheiser/sip/pulls/6
2020-02-27 02:53:11 +00:00

79 lines
1.5 KiB
Go

package config
import (
"fmt"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
"go.jolheiser.com/beaver"
"os"
"path"
)
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
}