Fixed new config creation

This commit is contained in:
Brian Buller 2016-03-26 20:53:27 -05:00
parent 437d19f0b4
commit d6e4559752
2 changed files with 24 additions and 36 deletions

View File

@ -70,37 +70,6 @@ func (c *Config) Save() error {
return errors.New("Bad setup.") return errors.New("Bad setup.")
} }
return c.generalConfig.Save() return c.generalConfig.Save()
/*
var cfgPath string
var configLines []string
//configLines = append(configLines, "server="+client.ServerAddr)
//configLines = append(configLines, "key="+client.ServerKey)
cfgPath = os.Getenv("HOME")
if cfgPath != "" {
cfgPath = cfgPath + "/.config"
if err := c.verifyOrCreateDirectory(cfgPath); err != nil {
return err
}
cfgPath = cfgPath + "/" + c.name
}
if cfgPath != "" {
file, err := os.Create(cfgPath)
if err != nil {
// Couldn't load config even though one was specified
return err
}
defer file.Close()
w := bufio.NewWriter(file)
for _, line := range configLines {
fmt.Fprintln(w, line)
}
if err = w.Flush(); err != nil {
return err
}
}
return nil
*/
} }
// verifyOrCreateDirectory is a helper function for building an // verifyOrCreateDirectory is a helper function for building an

View File

@ -4,7 +4,9 @@ package userConfig
import ( import (
"bytes" "bytes"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"os"
"strings" "strings"
"github.com/BurntSushi/toml" "github.com/BurntSushi/toml"
@ -27,6 +29,19 @@ func NewGeneralConfig(name, path string) (*GeneralConfig, error) {
gf.RawFiles = []string{} gf.RawFiles = []string{}
gf.Values = make(map[string]string) gf.Values = make(map[string]string)
// Check if file exists
fmt.Println("Checking if config file exists...")
var f os.FileInfo
var err error
if f, err = os.Stat(gf.GetFullPath()); os.IsNotExist(err) {
fmt.Println("Nope, saving default config...")
if err = gf.Save(); err != nil {
fmt.Println("Error saving default config...")
return gf, err
}
}
fmt.Println(f)
if err := gf.Load(); err != nil { if err := gf.Load(); err != nil {
return gf, err return gf, err
} }
@ -36,12 +51,11 @@ func NewGeneralConfig(name, path string) (*GeneralConfig, error) {
// Load loads config files into the config // Load loads config files into the config
func (gf *GeneralConfig) Load() error { func (gf *GeneralConfig) Load() error {
if strings.TrimSpace(gf.Name) == "" || strings.TrimSpace(gf.Path) == "" { if strings.TrimSpace(gf.Name) == "" || strings.TrimSpace(gf.Path) == "" {
return errors.New("Invalid ConfigFile Name: " + gf.Path + "/" + gf.Name) return errors.New("Invalid ConfigFile Name: " + gf.GetFullPath())
} }
// Config files end with .conf // Config files end with .conf
cfgPath := gf.Path + "/" + gf.Name + ".conf" tomlData, err := ioutil.ReadFile(gf.GetFullPath())
tomlData, err := ioutil.ReadFile(cfgPath)
if err != nil { if err != nil {
return err return err
} }
@ -54,11 +68,11 @@ func (gf *GeneralConfig) Load() error {
// Save writes the config to file(s) // Save writes the config to file(s)
func (gf *GeneralConfig) Save() error { func (gf *GeneralConfig) Save() error {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
cfgPath := gf.Path + "/" + gf.Name + ".conf" fmt.Println("Save Config: " + gf.GetFullPath())
if err := toml.NewEncoder(buf).Encode(gf); err != nil { if err := toml.NewEncoder(buf).Encode(gf); err != nil {
return err return err
} }
return ioutil.WriteFile(cfgPath, buf.Bytes(), 0644) return ioutil.WriteFile(gf.GetFullPath(), buf.Bytes(), 0644)
} }
// Set sets a key/value pair in gf, if unable to save, revert to old value // Set sets a key/value pair in gf, if unable to save, revert to old value
@ -77,3 +91,8 @@ func (gf *GeneralConfig) Set(k, v string) error {
func (gf *GeneralConfig) Get(k string) string { func (gf *GeneralConfig) Get(k string) string {
return gf.Values[k] return gf.Values[k]
} }
// GetFullPath returns the full path & filename to the config file
func (gf *GeneralConfig) GetFullPath() string {
return gf.Path + "/" + gf.Name + ".conf"
}