Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(configloader): fix nil pointer err #547

Merged
merged 2 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, "\n"))
daniel-hutao marked this conversation as resolved.
Show resolved Hide resolved
}

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