If config file doesn't exist, create a blank one
This commit is contained in:
parent
02112223fd
commit
b7bf623f39
@ -9,6 +9,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"os"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
@ -39,15 +40,18 @@ 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.Path + string(os.PathSeparator) + gf.Name)
|
||||
}
|
||||
|
||||
// Config files end with .conf
|
||||
cfgPath := gf.Path + "/" + gf.Name + ".conf"
|
||||
cfgPath := gf.Path + string(os.PathSeparator) + gf.Name + ".conf"
|
||||
tomlData, err := ioutil.ReadFile(cfgPath)
|
||||
if err != nil {
|
||||
// Couldn't find the file, save a new one
|
||||
if err = gf.Save(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if _, err := toml.Decode(string(tomlData), &gf); err != nil {
|
||||
return err
|
||||
}
|
||||
@ -57,7 +61,7 @@ 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"
|
||||
cfgPath := gf.Path + string(os.PathSeparator) + gf.Name + ".conf"
|
||||
if err := toml.NewEncoder(buf).Encode(gf); err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -1,142 +0,0 @@
|
||||
package userConfig
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// GeneralConfig is the basic ConfigFile
|
||||
// All configs made with package userConfig will have this file
|
||||
// All additional config files will have an entry in 'ConfigFiles' here
|
||||
type GeneralConfig struct {
|
||||
Name string `toml:"-"`
|
||||
Path string `toml:"-"`
|
||||
ConfigFiles []string `toml:"additional_config"`
|
||||
Values map[string]string `toml:"general"`
|
||||
}
|
||||
|
||||
// NewGeneralConfig generates a General Config struct
|
||||
func NewGeneralConfig(name, path string) (*GeneralConfig, error) {
|
||||
gf := &GeneralConfig{Name: name, Path: path}
|
||||
gf.ConfigFiles = []string{}
|
||||
gf.Values = make(map[string]string)
|
||||
|
||||
// Check if file exists
|
||||
//var f os.FileInfo
|
||||
var err error
|
||||
if _, err = os.Stat(gf.GetFullPath()); os.IsNotExist(err) {
|
||||
if err = gf.Save(); err != nil {
|
||||
return gf, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := gf.Load(); err != nil {
|
||||
return gf, err
|
||||
}
|
||||
return gf, nil
|
||||
}
|
||||
|
||||
/** START of ConfigFile Interface Implementation **/
|
||||
|
||||
// GetName returns the name of this config file
|
||||
func (gf *GeneralConfig) GetName() string {
|
||||
return gf.Name
|
||||
}
|
||||
|
||||
// GetPath returns the path of this config file
|
||||
func (gf *GeneralConfig) GetPath() string {
|
||||
return gf.Path
|
||||
}
|
||||
|
||||
// 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.GetFullPath())
|
||||
}
|
||||
|
||||
// Config files end with .conf
|
||||
tomlData, err := ioutil.ReadFile(gf.GetFullPath())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := toml.Decode(string(tomlData), &gf); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Save writes the config to file(s)
|
||||
func (gf *GeneralConfig) Save() error {
|
||||
buf := new(bytes.Buffer)
|
||||
if err := toml.NewEncoder(buf).Encode(gf); err != nil {
|
||||
return err
|
||||
}
|
||||
return ioutil.WriteFile(gf.GetFullPath(), buf.Bytes(), 0644)
|
||||
}
|
||||
|
||||
// Set sets a key/value pair in gf, if unable to save, revert to old value
|
||||
// (and return the error)
|
||||
func (gf *GeneralConfig) Set(k, v string) error {
|
||||
oldVal := gf.Values[k]
|
||||
gf.Values[k] = v
|
||||
if err := gf.Save(); err != nil {
|
||||
gf.Values[k] = oldVal
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Get gets a key/value pair from gf
|
||||
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"
|
||||
}
|
||||
|
||||
/** END of ConfigFile Interface Implementation **/
|
||||
|
||||
// Additional General Config Functions
|
||||
|
||||
// HasConfigFile returns true if GeneralConfig knows about <name>.conf
|
||||
func (gf *GeneralConfig) HasConfigFile(name string) bool {
|
||||
for _, v := range gf.ConfigFiles {
|
||||
if v == name {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// AddConfigFile adds the config file <name>.conf
|
||||
/*
|
||||
func (gf *GeneralConfig) AddConfigFile(name string) (ConfigFile, error) {
|
||||
// Check if file exists
|
||||
var f os.FileInfo
|
||||
var err error
|
||||
cf := ConfigFile{}
|
||||
if f, err = os.Stat(cf.GetFullPath()); os.IsNotExist(err) {
|
||||
if err = cf.Save(); err != nil {
|
||||
return cf, err
|
||||
}
|
||||
}
|
||||
if gf.HasConfigFile() {
|
||||
// We already know about this file... So just return it
|
||||
return gf.GetConfigFile(name)
|
||||
}
|
||||
gf.ConfigFiles = append(gf.ConfigFiles, name)
|
||||
return cf, nil
|
||||
}
|
||||
|
||||
// GetConfigFile returns an additional config file from the config directory
|
||||
func (gf *GeneralConfig) GetConfigFile(name string) (ConfigFile, error) {
|
||||
|
||||
}
|
||||
*/
|
Loading…
Reference in New Issue
Block a user