Commit da6f005b authored by Lukas Burgey's avatar Lukas Burgey
Browse files

Search the configuration in a few places

parent 6b4ab53d
......@@ -266,7 +266,7 @@ func (conf *Config) validateConfig() (err error) {
}
// ReadConfig reads a config file and validates it
func ReadConfig(r io.Reader) (conf *Config, err error) {
func ReadConfig(r io.Reader, fileName string) (conf *Config, err error) {
configBytes, err := ioutil.ReadAll(r)
if err != nil {
return
......@@ -274,13 +274,13 @@ func ReadConfig(r io.Reader) (conf *Config, err error) {
conf = new(Config)
if strings.HasSuffix(configFile, ".yaml") {
if strings.HasSuffix(fileName, ".yaml") {
err = yaml.Unmarshal(configBytes, conf)
if err != nil {
err = fmt.Errorf("Error decoding yaml: %s", err)
return
}
} else if strings.HasSuffix(configFile, ".json") {
} else if strings.HasSuffix(fileName, ".json") {
err = json.Unmarshal(configBytes, conf)
if err != nil {
err = fmt.Errorf("Error decoding json: %s", err)
......
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"path/filepath"
"git.scc.kit.edu/feudal/feudalClient/config"
deps "git.scc.kit.edu/feudal/feudalClient/deployments"
......@@ -138,6 +141,82 @@ func prepareDebugging(conf *config.Config) {
}
}
// search for a config file
// dir pattern: $HOME/.config -> /etc/feudal -> $CWD
// file pattern: *.yaml -> *.json
func findConfigFile(arg string) (handle *os.File, err error) {
globString := func(gs string) string {
var files []string
files, err = filepath.Glob(gs)
if err == nil && len(files) > 0 {
log.Printf("Globbed: %v", files)
_, err = os.Stat(files[0])
if err == nil {
return files[0]
} else {
log.Printf("Error opening: %s", err)
}
}
return ""
}
globDir := func(ds string) (name string) {
name = globString(path.Join(ds, "*.yaml"))
if name != "" {
return
}
name = globString(path.Join(ds, "*.json"))
if name != "" {
return
}
return
}
findName := func() string {
name := ""
if *configFile != "" {
// check if file exists
_, err = os.Stat(*configFile)
if err == nil {
return *configFile
} else {
log.Printf("Error opening: %s", err)
}
}
var home, cwd string
// search $HOME/.config
if home, err = os.UserHomeDir(); err == nil {
name = globDir(path.Join(home, ".config/feudal"))
if name != "" {
return name
}
}
name = globDir("/etc/feudal")
if name != "" {
return name
}
cwd, err = os.Getwd()
if err == nil {
name = globDir(cwd)
if name != "" {
return name
}
}
return ""
}
firstName := findName()
if firstName == "" {
err = fmt.Errorf(`No config file!
Provide -c/--config or place a config json/yaml in $HOME/.config/feudal or /etc/feudal or the current working directory`)
return
}
return os.Open(firstName)
}
func main() {
var err error
......@@ -158,17 +237,15 @@ func main() {
return
}
// read the config file
if *configFile == "" {
log.Fatalf("[Conf] Please provide the --config flag. See --help")
}
log.Printf("[Conf] Opening config file %s", *configFile)
handle, err := os.Open(*configFile)
// search appropriate config
handle, err := findConfigFile(*configFile)
if err != nil {
log.Fatalf("[Conf] Error opening config file: %s", err)
}
log.Printf("[Conf] Opened config file %s", handle.Name())
conf, err := config.ReadConfig(handle)
// read the config file
conf, err := config.ReadConfig(handle, handle.Name())
if err != nil {
log.Fatalf("[Conf] %s", err)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment