Skip to content

Commit

Permalink
Merge pull request #547 from aFlyBird0/fix-configloader
Browse files Browse the repository at this point in the history
fix(configloader): fix nil pointer err
  • Loading branch information
daniel-hutao authored May 19, 2022
2 parents 59706f7 + 3cf7512 commit 2b3b2f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions internal/pkg/configloader/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"runtime"
"strings"

"gopkg.in/yaml.v3"

Expand Down Expand Up @@ -94,19 +95,22 @@ func LoadConf(configFileName string) (*Config, error) {
return nil, err
}

cfg := LoadToolConf(toolFilePath, varFilePath)
cfg, err := LoadToolConf(toolFilePath, varFilePath)
if err != nil {
return nil, err
}
cfg.State = gConfig.State
return cfg, nil
}

// LoadToolConf reads tool file rendering by var file as a Config struct.
func LoadToolConf(toolFileName, varFileName string) *Config {
func LoadToolConf(toolFileName, varFileName string) (*Config, error) {
configFileBytes, err := ioutil.ReadFile(toolFileName)
if err != nil {
log.Error(err)
log.Info("Maybe the default file doesn't exist or you forgot to pass your config file to the \"-f\" option?")
log.Info("See \"dtm help\" for more information.")
return nil
return nil, err
}

log.Debugf("Original config: \n%s\n", string(configFileBytes))
Expand All @@ -115,7 +119,7 @@ func LoadToolConf(toolFileName, varFileName string) *Config {
configFileBytesWithVarsRendered, err := renderVariables(varFileName, configFileBytes)
if err != nil {
log.Error(err)
return nil
return nil, err
}

log.Debugf("Config file after rendering with variables: \n%s\n", string(configFileBytesWithVarsRendered))
Expand All @@ -125,18 +129,20 @@ func LoadToolConf(toolFileName, varFileName string) *Config {
if err != nil {
log.Error("Please verify the format of your config file.")
log.Errorf("Reading config file failed. %s.", err)
return nil
return nil, err
}

errs := validateConfig(&config)
if len(errs) != 0 {
var errStrings []string
for _, e := range errs {
log.Errorf("Config validation failed: %s.", e)
errStrings = append(errStrings, e.Error())
}
return nil
return nil, fmt.Errorf(strings.Join(errStrings, "; "))
}

return &config
return &config, nil
}

// genToolVarPath return the Abs path of tool file and var file, if var file is null, return variables.yaml
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/configloader/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func loadVariablesFilesIntoMap(varFileName string) (map[string]interface{}, erro
return variables, nil
}

// this is because our variables syntax is [[ varName ]]
// this is because our variables' syntax is [[ varName ]]
// while Go's template is [[ .varName ]]
func addDotForVariablesInConfig(s string) string {
// regex := `\[\[\s*(.*)\s*\]\]`
Expand Down

0 comments on commit 2b3b2f7

Please sign in to comment.