mirror of
https://gitee.com/johng/gf
synced 2026-06-06 02:25:47 +08:00
add genv.SetMap
This commit is contained in:
@ -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)
|
||||
|
||||
@ -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{
|
||||
|
||||
Reference in New Issue
Block a user