Some Updates
This commit is contained in:
		
							
								
								
									
										76
									
								
								config.go
									
									
									
									
									
								
							
							
						
						
									
										76
									
								
								config.go
									
									
									
									
									
								
							| @@ -5,6 +5,7 @@ import ( | ||||
| 	"errors" | ||||
| 	"os" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/br0xen/user-config/ext/go-xdg" | ||||
| ) | ||||
| @@ -24,16 +25,58 @@ func NewConfig(name string) (*Config, error) { | ||||
| 	return c, nil | ||||
| } | ||||
|  | ||||
| // GetKeyList at the config level returns all keys in the <c.name>.conf file | ||||
| func (c *Config) GetKeyList() []string { | ||||
| 	return c.generalConfig.GetKeyList() | ||||
| } | ||||
|  | ||||
| // Set at the config level sets a value in the <c.name>.conf file | ||||
| func (c *Config) Set(k, v string) error { | ||||
| 	return c.generalConfig.Set(k, v) | ||||
| } | ||||
|  | ||||
| // SetBytes at the config level sets a value in the <c.name>.conf file | ||||
| func (c *Config) SetBytes(k string, v []byte) error { | ||||
| 	return c.generalConfig.SetBytes(k, v) | ||||
| } | ||||
|  | ||||
| // SetInt saves an integer (as a string) in the <c.name>.conf file | ||||
| func (c *Config) SetInt(k string, v int) error { | ||||
| 	return c.generalConfig.SetInt(k, v) | ||||
| } | ||||
|  | ||||
| // SetDateTime saves a time.Time (as a string) in the <c.name>.conf file | ||||
| func (c *Config) SetDateTime(k string, v time.Time) error { | ||||
| 	return c.generalConfig.SetDateTime(k, v) | ||||
| } | ||||
|  | ||||
| // Get at the config level retrieves a value from the <c.name>.conf file | ||||
| func (c *Config) Get(k string) string { | ||||
| 	return c.generalConfig.Get(k) | ||||
| } | ||||
|  | ||||
| // GetBytes at the config level retrieves a value from the <c.name>.conf file | ||||
| // and returns it as a byte slice | ||||
| func (c *Config) GetBytes(k string) []byte { | ||||
| 	return c.generalConfig.GetBytes(k) | ||||
| } | ||||
|  | ||||
| // GetInt at the config level retrieves a value from the <c.name>.conf file | ||||
| // 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) | ||||
| } | ||||
|  | ||||
| // GetDateTime at the config level retrieves a value from the <c.name>.conf file | ||||
| func (c *Config) GetDateTime(k string) (time.Time, error) { | ||||
| 	return c.generalConfig.GetDateTime(k) | ||||
| } | ||||
|  | ||||
| // DeleteKey at the config level removes a key from the <c.name>.conf file | ||||
| func (c *Config) DeleteKey(k string) { | ||||
| 	c.generalConfig.DeleteKey(k) | ||||
| } | ||||
|  | ||||
| // GetConfigPath just returns the config path | ||||
| func (c *Config) GetConfigPath() string { | ||||
| 	return c.generalConfig.Path | ||||
| @@ -49,7 +92,7 @@ func (c *Config) Load() error { | ||||
| 	var cfgPath string | ||||
| 	cfgPath = xdg.Config.Dirs()[0] | ||||
| 	if cfgPath != "" { | ||||
| 		cfgPath = cfgPath + "/" + c.name | ||||
| 		cfgPath = cfgPath + string(os.PathSeparator) + c.name | ||||
| 		if err = c.verifyOrCreateDirectory(cfgPath); err != nil { | ||||
| 			return err | ||||
| 		} | ||||
| @@ -70,37 +113,6 @@ func (c *Config) Save() error { | ||||
| 		return errors.New("Bad setup.") | ||||
| 	} | ||||
| 	return c.generalConfig.Save() | ||||
| 	/* | ||||
| 		var cfgPath string | ||||
| 		var configLines []string | ||||
| 		//configLines = append(configLines, "server="+client.ServerAddr) | ||||
| 		//configLines = append(configLines, "key="+client.ServerKey) | ||||
| 		cfgPath = os.Getenv("HOME") | ||||
| 		if cfgPath != "" { | ||||
| 			cfgPath = cfgPath + "/.config" | ||||
| 			if err := c.verifyOrCreateDirectory(cfgPath); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 			cfgPath = cfgPath + "/" + c.name | ||||
| 		} | ||||
| 		if cfgPath != "" { | ||||
| 			file, err := os.Create(cfgPath) | ||||
| 			if err != nil { | ||||
| 				// Couldn't load config even though one was specified | ||||
| 				return err | ||||
| 			} | ||||
| 			defer file.Close() | ||||
|  | ||||
| 			w := bufio.NewWriter(file) | ||||
| 			for _, line := range configLines { | ||||
| 				fmt.Fprintln(w, line) | ||||
| 			} | ||||
| 			if err = w.Flush(); err != nil { | ||||
| 				return err | ||||
| 			} | ||||
| 		} | ||||
| 		return nil | ||||
| 	*/ | ||||
| } | ||||
|  | ||||
| // verifyOrCreateDirectory is a helper function for building an | ||||
|   | ||||
| @@ -5,7 +5,9 @@ import ( | ||||
| 	"bytes" | ||||
| 	"errors" | ||||
| 	"io/ioutil" | ||||
| 	"strconv" | ||||
| 	"strings" | ||||
| 	"time" | ||||
|  | ||||
| 	"github.com/BurntSushi/toml" | ||||
| ) | ||||
| @@ -61,6 +63,15 @@ func (gf *GeneralConfig) Save() error { | ||||
| 	return ioutil.WriteFile(cfgPath, buf.Bytes(), 0644) | ||||
| } | ||||
|  | ||||
| // GetKeyList returns a list of all keys in the config file | ||||
| func (gf *GeneralConfig) GetKeyList() []string { | ||||
| 	var ret []string | ||||
| 	for k, _ := range gf.Values { | ||||
| 		ret = append(ret, k) | ||||
| 	} | ||||
| 	return ret | ||||
| } | ||||
|  | ||||
| // 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 { | ||||
| @@ -73,7 +84,45 @@ func (gf *GeneralConfig) Set(k, v string) error { | ||||
| 	return nil | ||||
| } | ||||
|  | ||||
| // SetBytes at the config level sets a value in the <c.name>.conf file | ||||
| func (gf *GeneralConfig) SetBytes(k string, v []byte) error { | ||||
| 	return gf.Set(k, string(v)) | ||||
| } | ||||
|  | ||||
| // SetInt sets an integer value (as a string) in the config file | ||||
| func (gf *GeneralConfig) SetInt(k string, v int) error { | ||||
| 	return gf.Set(k, strconv.Itoa(v)) | ||||
| } | ||||
|  | ||||
| // SetDateTime sets a DateTime value (as a string) in the config file | ||||
| func (gf *GeneralConfig) SetDateTime(k string, v time.Time) error { | ||||
| 	return gf.Set(k, v.Format(time.RFC3339)) | ||||
| } | ||||
|  | ||||
| // Get gets a key/value pair from gf | ||||
| func (gf *GeneralConfig) Get(k string) string { | ||||
| 	return gf.Values[k] | ||||
| } | ||||
| } | ||||
|  | ||||
| // GetInt gets a key/value pair from gf and return it as an integer | ||||
| // An error if it can't be converted | ||||
| func (gf *GeneralConfig) GetInt(k string) (int, error) { | ||||
| 	return strconv.Atoi(gf.Get(k)) | ||||
| } | ||||
|  | ||||
| // GetDateTime gets a key/value pair from gf and returns it as a time.Time | ||||
| // An error if it can't be converted | ||||
| func (gf *GeneralConfig) GetDateTime(k string) (time.Time, error) { | ||||
| 	return time.Parse(time.RFC3339, k) | ||||
| } | ||||
|  | ||||
| // GetBytes gets a key/value pair from gf and returns it as a byte slice | ||||
| // Or an error if it fails for whatever reason | ||||
| func (gf *GeneralConfig) GetBytes(k string) []byte { | ||||
| 	return []byte(gf.Get(k)) | ||||
| } | ||||
|  | ||||
| // DeleteKey removes a key from the file | ||||
| func (gf *GeneralConfig) DeleteKey(k string) { | ||||
| 	delete(gf.Values, k) | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user