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 }