mirror of
https://github.com/mudler/luet.git
synced 2025-09-01 15:18:28 +00:00
Review configuration file parsing logic
Luet support now these priorities on read configuration file: - command line option (if available) - $PWD/.luet.yaml - $HOME/.luet.yaml - /etc/luet/luet.yaml
This commit is contained in:
38
cmd/root.go
38
cmd/root.go
@@ -185,22 +185,38 @@ func init() {
|
|||||||
|
|
||||||
// initConfig reads in config file and ENV variables if set.
|
// initConfig reads in config file and ENV variables if set.
|
||||||
func initConfig() {
|
func initConfig() {
|
||||||
|
// Luet support these priorities on read configuration file:
|
||||||
|
// - command line option (if available)
|
||||||
|
// - $PWD/.luet.yaml
|
||||||
|
// - $HOME/.luet.yaml
|
||||||
|
// - /etc/luet/luet.yaml
|
||||||
|
//
|
||||||
|
// Note: currently a single viper instance support only one config name.
|
||||||
|
|
||||||
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
|
||||||
if err != nil {
|
|
||||||
Error(err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
viper.SetEnvPrefix(LuetEnvPrefix)
|
viper.SetEnvPrefix(LuetEnvPrefix)
|
||||||
viper.SetConfigType("yaml")
|
viper.SetConfigType("yaml")
|
||||||
viper.SetConfigName(".luet") // name of config file (without extension)
|
|
||||||
if cfgFile != "" { // enable ability to specify config file via flag
|
if cfgFile != "" { // enable ability to specify config file via flag
|
||||||
viper.SetConfigFile(cfgFile)
|
viper.SetConfigFile(cfgFile)
|
||||||
} else {
|
} else {
|
||||||
viper.AddConfigPath(dir)
|
// Retrieve pwd directory
|
||||||
viper.AddConfigPath(".")
|
pwdDir, err := os.Getwd()
|
||||||
viper.AddConfigPath("$HOME")
|
if err != nil {
|
||||||
viper.AddConfigPath("/etc/luet")
|
Error(err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
homeDir := helpers.GetHomeDir()
|
||||||
|
|
||||||
|
if helpers.Exists(filepath.Join(pwdDir, ".luet.yaml")) || (homeDir != "" && helpers.Exists(filepath.Join(homeDir, ".luet.yaml"))) {
|
||||||
|
viper.AddConfigPath(".")
|
||||||
|
if homeDir != "" {
|
||||||
|
viper.AddConfigPath(homeDir)
|
||||||
|
}
|
||||||
|
viper.SetConfigName(".luet")
|
||||||
|
} else {
|
||||||
|
viper.SetConfigName("luet")
|
||||||
|
viper.AddConfigPath("/etc/luet")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
viper.AutomaticEnv() // read in environment variables that match
|
viper.AutomaticEnv() // read in environment variables that match
|
||||||
|
@@ -16,7 +16,9 @@
|
|||||||
package helpers
|
package helpers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"os/user"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
@@ -30,3 +32,17 @@ func Exec(cmd string, args []string, env []string) error {
|
|||||||
}
|
}
|
||||||
return syscall.Exec(path, args, env)
|
return syscall.Exec(path, args, env)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetHomeDir() (ans string) {
|
||||||
|
// os/user doesn't work in from scratch environments
|
||||||
|
u, err := user.Current()
|
||||||
|
if err == nil {
|
||||||
|
ans = u.HomeDir
|
||||||
|
} else {
|
||||||
|
ans = ""
|
||||||
|
}
|
||||||
|
if os.Getenv("HOME") != "" {
|
||||||
|
ans = os.Getenv("HOME")
|
||||||
|
}
|
||||||
|
return ans
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user