Skip to content

Commit

Permalink
read configPath from env LETS_CONFIG
Browse files Browse the repository at this point in the history
  • Loading branch information
kindermax committed Feb 17, 2020
1 parent cfd7a66 commit 03aee26
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
41 changes: 28 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ var (
MIXINS = "mixins"
)

const defaultConfigPath = "lets.yaml"

var validConfigFields = strings.Join([]string{COMMANDS, SHELL, ENV, MIXINS}, " ")
var validMixinConfigFields = strings.Join([]string{COMMANDS, ENV}, " ")

Expand Down Expand Up @@ -71,31 +73,44 @@ func newMixinConfig() *Config {
return cfg
}

func GetConfigPathFromEnv() string {
return os.Getenv("LETS_CONFIG")
}

func GetDefaultConfigPath() string {
return defaultConfigPath
}

// Load a config from file
func Load(filename string, rootDir string) (*Config, error) {
failedLoadErr := func(err error) error {
return fmt.Errorf("failed to load config file %s: %s", filename, err)
}

workDir, err := os.Getwd()
if err != nil {
return nil, failedLoadErr(err)
}
if rootDir != "" {
workDir = rootDir
}
absPath, err := filepath.Abs(filepath.Join(workDir, filename))
if err != nil {
return nil, failedLoadErr(err)
configAbsPath := ""
if filepath.IsAbs(filename) {
configAbsPath = filename
} else {
workDir, err := os.Getwd()
if err != nil {
return nil, failedLoadErr(err)
}
if rootDir != "" {
workDir = rootDir
}
configAbsPath, err = filepath.Abs(filepath.Join(workDir, filename))
if err != nil {
return nil, failedLoadErr(err)
}
}

config, err := loadConfig(absPath)
config, err := loadConfig(configAbsPath)
if err != nil {
return nil, failedLoadErr(err)
}

config.WorkDir = filepath.Dir(absPath)
config.FilePath = absPath
config.WorkDir = filepath.Dir(configAbsPath)
config.FilePath = configAbsPath

if err = Validate(config); err != nil {
return nil, failedLoadErr(err)
Expand Down
6 changes: 5 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import (
)

func main() {
conf, err := config.Load("lets.yaml", "")
configPath := config.GetConfigPathFromEnv()
if configPath == "" {
configPath = config.GetDefaultConfigPath()
}
conf, err := config.Load(configPath, "")
if err != nil {
fmt.Printf("Error: %s\n", err)
os.Exit(1)
Expand Down

0 comments on commit 03aee26

Please sign in to comment.