Fixed new config creation
This commit is contained in:
parent
437d19f0b4
commit
d6e4559752
31
config.go
31
config.go
@ -70,37 +70,6 @@ func (c *Config) Save() error {
|
||||
return errors.New("Bad setup.")
|
||||
}
|
||||
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
|
||||
|
@ -4,7 +4,9 @@ package userConfig
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
@ -27,6 +29,19 @@ func NewGeneralConfig(name, path string) (*GeneralConfig, error) {
|
||||
gf.RawFiles = []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 {
|
||||
return gf, err
|
||||
}
|
||||
@ -36,12 +51,11 @@ func NewGeneralConfig(name, path string) (*GeneralConfig, error) {
|
||||
// Load loads config files into the config
|
||||
func (gf *GeneralConfig) Load() error {
|
||||
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
|
||||
cfgPath := gf.Path + "/" + gf.Name + ".conf"
|
||||
tomlData, err := ioutil.ReadFile(cfgPath)
|
||||
tomlData, err := ioutil.ReadFile(gf.GetFullPath())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -54,11 +68,11 @@ func (gf *GeneralConfig) Load() error {
|
||||
// Save writes the config to file(s)
|
||||
func (gf *GeneralConfig) Save() error {
|
||||
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 {
|
||||
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
|
||||
@ -77,3 +91,8 @@ func (gf *GeneralConfig) Set(k, v string) error {
|
||||
func (gf *GeneralConfig) Get(k string) string {
|
||||
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"
|
||||
}
|
Loading…
Reference in New Issue
Block a user