package main import ( "bytes" "io/ioutil" "strings" "github.com/BurntSushi/toml" ) func NewSongbookConfig(dir string) { sc := &SongbookConfig{ConfigPath: dir + "songbook.toml"} err := sc.Load() return sc, err } type SongbookConfig struct { Username string `toml:"username"` Password string `toml:"enc_pw"` ConfigPath string `toml:"-"` } func (sc *SongbookConfig) Load() error { tomlData, err := ioutil.ReadFile(sc.ConfigPath) if err != nil { // Couldn't find the file, save a new one if err = sc.Save(); err != nil { return err } } if _, err := toml.Decode(string(tomlData), &sc); err != nil { return err } return nil } func (sc *SongbookConfig) Save() error { buf := new(bytes.Buffer) if err := toml.NewEncoder(buf).Encode(sc); err != nil { return err } return ioutil.WriteFile(sc.ConfigPath, buf.Bytes(), 0644) } func NewModel(dir string) *model { m := new(model) m.config = NewSongbookConfig(dir) return m } type model struct { config *SongbookConfig } func (m *model) hasUser() bool { // is there a user in the config? if strings.TrimSpace(m.config.Username) == "" || strings.TrimSpace(m.Config.Password) == "" { return false } return true }