Merge branch 'master' of gogs.bullercodeworks.com:brian/user-config

This commit is contained in:
Brian Buller 2016-03-31 06:43:01 -05:00
commit 51a838b9a5
4 changed files with 267 additions and 76 deletions

108
addon_config.go Normal file
View File

@ -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 **/

View File

@ -34,6 +34,12 @@ func (c *Config) Get(k string) string {
return c.generalConfig.Get(k) return c.generalConfig.Get(k)
} }
// AddConfig adds a new config file named <name>.conf
func (c *Config) AddConfig(name string) (*ConfigFile, error) {
return nil, nil
}
// GetConfigPath just returns the config path // GetConfigPath just returns the config path
func (c *Config) GetConfigPath() string { func (c *Config) GetConfigPath() string {
return c.generalConfig.Path return c.generalConfig.Path

View File

@ -1,80 +1,15 @@
// Package userConfig eases the use of config files in a user's home directory // Package userConfig eases the use of config files in a user's home directory
package userConfig package userConfig
import ( // ConfigFile is the interface for all config files
"bytes" type ConfigFile interface {
"errors" SetName(string)
"io/ioutil" GetName() string
"os" SetPath(string)
"strings" GetPath() string
Load() error
"github.com/BurntSushi/toml" Save() error
) Set(string, string)
Get(string) string
// GeneralConfig is the basic config structure GetFullPath() string
// 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)
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.Path + string(os.PathSeparator) + gf.Name)
}
// Config files end with .conf
cfgPath := gf.Path + string(os.PathSeparator) + gf.Name + ".conf"
tomlData, err := ioutil.ReadFile(cfgPath)
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)
cfgPath := gf.Path + string(os.PathSeparator) + gf.Name + ".conf"
if err := toml.NewEncoder(buf).Encode(gf); err != nil {
return err
}
return ioutil.WriteFile(cfgPath, 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]
} }

142
general_config.go Normal file
View File

@ -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 <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) {
}
*/