From 1d63dca67cb42efd502487fb53a04eacaa069c7a Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sun, 27 Mar 2016 10:43:00 -0500 Subject: [PATCH] Doing some more changes --- addon_config.go | 108 +++++++++++++++++++++++++++++++++++ config.go | 6 ++ config_file.go | 105 ++++------------------------------ general_config.go | 142 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 267 insertions(+), 94 deletions(-) create mode 100644 addon_config.go create mode 100644 general_config.go diff --git a/addon_config.go b/addon_config.go new file mode 100644 index 0000000..d066a49 --- /dev/null +++ b/addon_config.go @@ -0,0 +1,108 @@ +package userConfig + +import ( + "bytes" + "errors" + "fmt" + "io/ioutil" + "os" + "strings" +) + +// AddonConfig is an additional ConfigFile +type AddonConfig struct { + Name string `toml:"-"` + Path string `toml:"-"` + Values map[string]map[string]string `toml:"-"` +} + +// NewAddonConfig generates a Additional Config struct +func NewAddonConfig(name, path string) (*AddonConfig, error) { + af := &AddonConfig{Name: name, Path: path} + af.Values = make(map[string]map[string]string) + + // Check if file exists + //var f os.FileInfo + var err error + if _, err = os.Stat(af.GetFullPath()); os.IsNotExist(err) { + if err = af.Save(); err != nil { + return af, err + } + } + + if err := af.Load(); err != nil { + return af, err + } + return af, nil +} + +/** START of ConfigFile Interface Implementation **/ + +// GetName returns the name of this config file +func (af *AddonConfig) GetName() string { + return af.Name +} + +// GetPath returns the path of this config file +func (af *AddonConfig) GetPath() string { + return af.Path +} + +// Load loads config files into the config +func (af *AddonConfig) Load() error { + if strings.TrimSpace(af.Name) == "" || strings.TrimSpace(af.Path) == "" { + return errors.New("Invalid ConfigFile Name: " + af.GetFullPath()) + } + + // Config files end with .conf + tomlData, err := ioutil.ReadFile(af.GetFullPath()) + if err != nil { + return err + } + fmt.Println(tomlData) + // TODO: Figure out loading this into the struct + //if _, err := toml.Decode(string(tomlData), &af); err != nil { + // return err + //} + return nil +} + +// Save writes the config to file(s) +func (af *AddonConfig) Save() error { + buf := new(bytes.Buffer) + // TODO: Figure out writing struct to buf + //if err := toml.NewEncoder(buf).Encode(af); err != nil { + // return err + //} + return ioutil.WriteFile(af.GetFullPath(), buf.Bytes(), 0644) +} + +// Set sets a key/value pair in af, if unable to save, revert to old value +// (and return the error) +func (af *AddonConfig) Set(category, k, v string) error { + if _, ok := af.Values[category]; !ok { + af.Values[category] = make(map[string]string) + } + oldVal := af.Values[category][k] + af.Values[category][k] = v + if err := af.Save(); err != nil { + af.Values[category][k] = oldVal + return err + } + return nil +} + +// Get gets a key/value pair from af +func (af *AddonConfig) Get(category, k string) string { + if _, ok := af.Values[category]; !ok { + return "" + } + return af.Values[category][k] +} + +// GetFullPath returns the full path & filename to the config file +func (af *AddonConfig) GetFullPath() string { + return af.Path + "/" + af.Name + ".conf" +} + +/** END of ConfigFile Interface Implementation **/ diff --git a/config.go b/config.go index 780bfc2..be3772d 100644 --- a/config.go +++ b/config.go @@ -34,6 +34,12 @@ func (c *Config) Get(k string) string { return c.generalConfig.Get(k) } +// AddConfig adds a new config file named .conf +func (c *Config) AddConfig(name string) (*ConfigFile, error) { + + return nil, nil +} + // GetConfigPath just returns the config path func (c *Config) GetConfigPath() string { return c.generalConfig.Path diff --git a/config_file.go b/config_file.go index 1c6cf05..d5facf2 100644 --- a/config_file.go +++ b/config_file.go @@ -1,98 +1,15 @@ // Package userConfig eases the use of config files in a user's home directory package userConfig -import ( - "bytes" - "errors" - "fmt" - "io/ioutil" - "os" - "strings" - - "github.com/BurntSushi/toml" -) - -// GeneralConfig is the basic config structure -// All configs make with package userConfig will have this file -type GeneralConfig struct { - Name string `toml:"-"` - Path string `toml:"-"` - ConfigFiles []string `toml:"additional_config"` - RawFiles []string `toml:"raw_files"` - 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.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 - } - return gf, nil -} - -// 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) - fmt.Println("Save Config: " + gf.GetFullPath()) - 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" +// ConfigFile is the interface for all config files +type ConfigFile interface { + SetName(string) + GetName() string + SetPath(string) + GetPath() string + Load() error + Save() error + Set(string, string) + Get(string) string + GetFullPath() string } \ No newline at end of file diff --git a/general_config.go b/general_config.go new file mode 100644 index 0000000..79654a3 --- /dev/null +++ b/general_config.go @@ -0,0 +1,142 @@ +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 .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 .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) { + +} +*/