Merge branch 'master' of ssh://gogs.bullercodeworks.com:2200/brian/user-config
This commit is contained in:
commit
02112223fd
108
addon_config.go
Normal file
108
addon_config.go
Normal 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 **/
|
142
general_config.go
Normal file
142
general_config.go
Normal 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) {
|
||||
|
||||
}
|
||||
*/
|
Loading…
Reference in New Issue
Block a user