mirror of
https://github.com/httprunner/httprunner.git
synced 2026-07-09 06:23:52 +08:00
fix #1308: load .env file as environment variables
This commit is contained in:
@@ -294,12 +294,45 @@ func LoadFile(path string, structObj interface{}) (err error) {
|
||||
err = decoder.Decode(structObj)
|
||||
case ".yaml", ".yml":
|
||||
err = yaml.Unmarshal(file, structObj)
|
||||
case ".env":
|
||||
err = parseEnvContent(file, structObj)
|
||||
default:
|
||||
err = ErrUnsupportedFileExt
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func parseEnvContent(file []byte, obj interface{}) error {
|
||||
envMap := obj.(map[string]string)
|
||||
lines := strings.Split(string(file), "\n")
|
||||
for _, line := range lines {
|
||||
line = strings.TrimSpace(line)
|
||||
if line == "" || strings.HasPrefix(line, "#") {
|
||||
// empty line or comment line
|
||||
continue
|
||||
}
|
||||
var kv []string
|
||||
if strings.Contains(line, "=") {
|
||||
kv = strings.SplitN(line, "=", 2)
|
||||
}
|
||||
if strings.Contains(line, ":") {
|
||||
kv = strings.SplitN(line, ":", 2)
|
||||
}
|
||||
if len(kv) != 2 {
|
||||
return errors.New(".env format error")
|
||||
}
|
||||
|
||||
key := strings.TrimSpace(kv[0])
|
||||
value := strings.TrimSpace(kv[1])
|
||||
envMap[key] = value
|
||||
|
||||
// set env
|
||||
log.Info().Str("key", key).Msg("set env")
|
||||
os.Setenv(key, value)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadFromCSV(path string) []map[string]interface{} {
|
||||
log.Info().Str("path", path).Msg("load csv file")
|
||||
file, err := readFile(path)
|
||||
|
||||
Reference in New Issue
Block a user