|
@@ -1,6 +1,7 @@
|
|
|
package config
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
|
+ "errors"
|
|
|
"os"
|
|
"os"
|
|
|
"path/filepath"
|
|
"path/filepath"
|
|
|
|
|
|
|
@@ -37,20 +38,52 @@ func defConfig() Config {
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func Load() error {
|
|
|
|
|
|
|
+func getPath(optionalPath string) (string, error) {
|
|
|
|
|
+ // Trigger an error if config flag used but is empty.
|
|
|
|
|
+ if optionalPath == "" {
|
|
|
|
|
+ return "", errors.New("Optional path cannot be empty.")
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Use the path provided by flags.
|
|
|
|
|
+ if optionalPath != "none" {
|
|
|
|
|
+ return optionalPath, nil
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // Use the default for the OS.
|
|
|
path, err := os.UserConfigDir()
|
|
path, err := os.UserConfigDir()
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return "", err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ path = filepath.Join(path, Name)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return "", err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ path = filepath.Join(path, "config.yml")
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ return "", err
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return path, nil
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func Load(optionalPath string) error {
|
|
|
|
|
+ path, err := getPath(optionalPath)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ // Split the directory from the configuration file.
|
|
|
|
|
+ dir, file := filepath.Split(path)
|
|
|
|
|
+
|
|
|
// Create the configuration directory if it does not exist already.
|
|
// Create the configuration directory if it does not exist already.
|
|
|
- path = filepath.Join(path, Name)
|
|
|
|
|
- err = os.MkdirAll(path, os.ModePerm)
|
|
|
|
|
|
|
+ err = os.MkdirAll(dir, os.ModePerm)
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
return err
|
|
return err
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- path = filepath.Join(path, "config.yml")
|
|
|
|
|
|
|
+ path = filepath.Join(dir, file)
|
|
|
_, err = os.Stat(path)
|
|
_, err = os.Stat(path)
|
|
|
if os.IsNotExist(err) {
|
|
if os.IsNotExist(err) {
|
|
|
f, err := os.Create(path)
|
|
f, err := os.Create(path)
|