2016-02-09 14:32:36 +00:00
|
|
|
// Package userConfig eases the use of config files in a user's home directory
|
|
|
|
package userConfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2017-02-04 22:14:19 +00:00
|
|
|
"time"
|
2016-02-09 14:32:36 +00:00
|
|
|
|
2017-07-28 12:21:40 +00:00
|
|
|
"github.com/casimir/xdg-go"
|
2016-02-09 14:32:36 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is a stuct for managing the config
|
|
|
|
type Config struct {
|
|
|
|
name string
|
|
|
|
generalConfig *GeneralConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewConfig generates a Config struct
|
|
|
|
func NewConfig(name string) (*Config, error) {
|
|
|
|
c := &Config{name: name}
|
|
|
|
if err := c.Load(); err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// GetKeyList at the config level returns all keys in the <c.name>.conf file
|
2017-02-04 22:14:19 +00:00
|
|
|
func (c *Config) GetKeyList() []string {
|
|
|
|
return c.generalConfig.GetKeyList()
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// Set at the config level sets a value in the <c.name>.conf file
|
2016-02-09 14:32:36 +00:00
|
|
|
func (c *Config) Set(k, v string) error {
|
|
|
|
return c.generalConfig.Set(k, v)
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// SetBytes at the config level sets a value in the <c.name>.conf file
|
2017-02-04 22:14:19 +00:00
|
|
|
func (c *Config) SetBytes(k string, v []byte) error {
|
|
|
|
return c.generalConfig.SetBytes(k, v)
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// SetInt saves an integer (as a string) in the <c.name>.conf file
|
2017-02-04 22:14:19 +00:00
|
|
|
func (c *Config) SetInt(k string, v int) error {
|
|
|
|
return c.generalConfig.SetInt(k, v)
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// SetDateTime saves a time.Time (as a string) in the <c.name>.conf file
|
2017-02-04 22:14:19 +00:00
|
|
|
func (c *Config) SetDateTime(k string, v time.Time) error {
|
|
|
|
return c.generalConfig.SetDateTime(k, v)
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// SetArray saves a string slice in the <c.name>.conf file
|
2017-03-01 12:58:34 +00:00
|
|
|
func (c *Config) SetArray(k string, v []string) error {
|
|
|
|
return c.generalConfig.SetArray(k, v)
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// Get at the config level retrieves a value from the <c.name>.conf file
|
2016-02-09 14:32:36 +00:00
|
|
|
func (c *Config) Get(k string) string {
|
|
|
|
return c.generalConfig.Get(k)
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// GetBytes at the config level retrieves a value from the <c.name>.conf file
|
2017-02-04 22:14:19 +00:00
|
|
|
// and returns it as a byte slice
|
|
|
|
func (c *Config) GetBytes(k string) []byte {
|
|
|
|
return c.generalConfig.GetBytes(k)
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// GetInt at the config level retrieves a value from the <c.name>.conf file
|
2017-02-04 22:14:19 +00:00
|
|
|
// and returns it as an integer (or an error if conversion fails)
|
|
|
|
func (c *Config) GetInt(k string) (int, error) {
|
|
|
|
return c.generalConfig.GetInt(k)
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// GetDateTime at the config level retrieves a value from the <c.name>.conf file
|
2017-02-04 22:14:19 +00:00
|
|
|
func (c *Config) GetDateTime(k string) (time.Time, error) {
|
|
|
|
return c.generalConfig.GetDateTime(k)
|
|
|
|
}
|
|
|
|
|
2017-03-01 12:58:34 +00:00
|
|
|
func (c *Config) GetArray(k string) ([]string, error) {
|
|
|
|
return c.generalConfig.GetArray(k)
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:45:16 +00:00
|
|
|
// DeleteKey at the config level removes a key from the <c.name>.conf file
|
2017-03-01 12:58:34 +00:00
|
|
|
func (c *Config) DeleteKey(k string) error {
|
|
|
|
return c.generalConfig.DeleteKey(k)
|
2017-02-04 22:14:19 +00:00
|
|
|
}
|
|
|
|
|
2016-02-09 14:32:36 +00:00
|
|
|
// GetConfigPath just returns the config path
|
|
|
|
func (c *Config) GetConfigPath() string {
|
|
|
|
return c.generalConfig.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load loads config files into the config
|
|
|
|
func (c *Config) Load() error {
|
|
|
|
var err error
|
|
|
|
if strings.TrimSpace(c.name) == "" {
|
|
|
|
return errors.New("Invalid Config Name: " + c.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
var cfgPath string
|
2017-07-28 12:21:40 +00:00
|
|
|
app := xdg.App{Name: c.name}
|
|
|
|
cfgPath = app.ConfigPath("")
|
2016-02-09 14:32:36 +00:00
|
|
|
if cfgPath != "" {
|
|
|
|
if err = c.verifyOrCreateDirectory(cfgPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Load general config
|
|
|
|
if c.generalConfig, err = NewGeneralConfig(c.name, cfgPath); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save writes the config to file(s)
|
|
|
|
func (c *Config) Save() error {
|
|
|
|
if c.generalConfig == nil {
|
|
|
|
return errors.New("Bad setup.")
|
|
|
|
}
|
|
|
|
return c.generalConfig.Save()
|
|
|
|
}
|
|
|
|
|
|
|
|
// verifyOrCreateDirectory is a helper function for building an
|
|
|
|
// individual directory
|
|
|
|
func (c *Config) verifyOrCreateDirectory(path string) error {
|
|
|
|
var tstDir *os.File
|
|
|
|
var tstDirInfo os.FileInfo
|
|
|
|
var err error
|
|
|
|
if tstDir, err = os.Open(path); err != nil {
|
|
|
|
if err = os.Mkdir(path, 0755); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if tstDir, err = os.Open(path); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tstDirInfo, err = tstDir.Stat(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !tstDirInfo.IsDir() {
|
|
|
|
return errors.New(path + " exists and is not a directory")
|
|
|
|
}
|
|
|
|
// We were able to open the path and it was a directory
|
|
|
|
return nil
|
|
|
|
}
|