forked from TrueCloudLab/frostfs-node
[#493] cmd/node: Add .env file for tests
Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
parent
db7a7f9bd3
commit
25a13d3611
3 changed files with 56 additions and 16 deletions
|
@ -1,15 +1,30 @@
|
|||
package configtest
|
||||
|
||||
import "github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/nspcc-dev/neofs-node/cmd/neofs-node/config"
|
||||
)
|
||||
|
||||
func fromFile(path string) *config.Config {
|
||||
var p config.Prm
|
||||
|
||||
os.Clearenv() // ENVs have priority over config files so we do this in tests
|
||||
|
||||
return config.New(p,
|
||||
config.WithConfigFile(path),
|
||||
)
|
||||
}
|
||||
|
||||
func fromEnvFile(path string) *config.Config {
|
||||
var p config.Prm
|
||||
|
||||
loadEnv(path) // github.com/joho/godotenv can do that as well
|
||||
return config.New(p)
|
||||
}
|
||||
|
||||
func forEachFile(paths []string, f func(*config.Config)) {
|
||||
for i := range paths {
|
||||
f(fromFile(paths[i]))
|
||||
|
@ -26,9 +41,37 @@ func ForEachFileType(pref string, f func(*config.Config)) {
|
|||
}, f)
|
||||
}
|
||||
|
||||
// ForEnvFileType creates config from `<pref>.env` file.
|
||||
func ForEnvFileType(pref string, f func(*config.Config)) {
|
||||
f(fromEnvFile(pref + ".env"))
|
||||
}
|
||||
|
||||
// EmptyConfig returns config without any values and sections.
|
||||
func EmptyConfig() *config.Config {
|
||||
var p config.Prm
|
||||
|
||||
return config.New(p)
|
||||
}
|
||||
|
||||
// loadEnv reads .env file, parses `X=Y` records and sets OS ENVs.
|
||||
func loadEnv(path string) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic("can't open .env file")
|
||||
}
|
||||
|
||||
defer f.Close()
|
||||
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
pair := strings.Split(scanner.Text(), "=")
|
||||
if len(pair) != 2 {
|
||||
continue
|
||||
}
|
||||
|
||||
err = os.Setenv(pair[0], pair[1])
|
||||
if err != nil {
|
||||
panic("can't set environment variable")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue