add genv.SetMap

This commit is contained in:
John Guo
2020-12-05 00:06:03 +08:00
parent d2b65f0f91
commit 2f741d3b24
2 changed files with 22 additions and 0 deletions

View File

@ -58,6 +58,16 @@ func Set(key, value string) error {
return os.Setenv(key, value)
}
// SetMap sets the environment variables using map.
func SetMap(m map[string]string) error {
for k, v := range m {
if err := os.Setenv(k, v); err != nil {
return err
}
}
return nil
}
// Contains checks whether the environment variable named <key> exists.
func Contains(key string) bool {
_, ok := os.LookupEnv(key)

View File

@ -7,6 +7,7 @@
package genv_test
import (
"github.com/gogf/gf/frame/g"
"github.com/gogf/gf/os/gcmd"
"os"
"testing"
@ -64,6 +65,17 @@ func Test_GEnv_Set(t *testing.T) {
})
}
func Test_GEnv_SetMap(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
err := genv.SetMap(g.MapStrStr{
"K1": "TEST1",
"K2": "TEST2",
})
t.Assert(err, nil)
t.AssertEQ(os.Getenv("K1"), "TEST1")
t.AssertEQ(os.Getenv("K2"), "TEST2")
})
}
func Test_GEnv_Build(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
s := genv.Build(map[string]string{