diff --git a/g/os/genv/genv.go b/g/os/genv/genv.go index fb5825217..e3635cef1 100644 --- a/g/os/genv/genv.go +++ b/g/os/genv/genv.go @@ -1,4 +1,4 @@ -// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// Copyright 2017-2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. // // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, @@ -8,6 +8,7 @@ package genv import "os" +import "strings" // All returns a copy of strings representing the environment, // in the form "key=value". @@ -15,6 +16,17 @@ func All() []string { return os.Environ() } +// Map returns a copy of strings representing the environment as a map. +func Map() map[string]string { + m := make(map[string]string) + i := 0 + for _, s := range os.Environ() { + i = strings.IndexByte(s, '=') + m[s[0:i]] = s[i+1:] + } + return m +} + // Get returns the value of the environment variable named by the . // It returns given if the variable does not exist in the environment. func Get(key string, def ...string) string { @@ -31,6 +43,23 @@ func Set(key, value string) error { return os.Setenv(key, value) } +// Contains checks whether the environment variable named exists. +func Contains(key string) bool { + _, ok := os.LookupEnv(key) + return ok +} + +// Build builds a map to a environment variable slice. +func Build(m map[string]string) []string { + array := make([]string, len(m)) + index := 0 + for k, v := range m { + array[index] = k + "=" + v + index++ + } + return array +} + // Remove deletes a single environment variable. func Remove(key string) error { return os.Unsetenv(key) diff --git a/g/os/genv/genv_test.go b/g/os/genv/genv_test.go index 9f9a894e4..536b3a822 100644 --- a/g/os/genv/genv_test.go +++ b/g/os/genv/genv_test.go @@ -1,39 +1,82 @@ +// Copyright 2017-2019 gf Author(https://github.com/gogf/gf). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + package genv_test import ( "github.com/gogf/gf/g/os/genv" + "github.com/gogf/gf/g/os/gtime" "github.com/gogf/gf/g/test/gtest" + "github.com/gogf/gf/g/util/gconv" "os" "testing" ) -func Test_Genv_All(t *testing.T) { +func Test_GEnv_All(t *testing.T) { gtest.Case(t, func() { gtest.Assert(os.Environ(), genv.All()) }) } -func Test_Genv_Get(t *testing.T) { +func Test_GEnv_Map(t *testing.T) { gtest.Case(t, func() { - key := "TEST_GET_ENV" + value := gconv.String(gtime.Nanosecond()) + key := "TEST_ENV_" + value + err := os.Setenv(key, "TEST") + gtest.Assert(err, nil) + gtest.Assert(genv.Map()[key], "TEST") + }) +} + +func Test_GEnv_Get(t *testing.T) { + gtest.Case(t, func() { + value := gconv.String(gtime.Nanosecond()) + key := "TEST_ENV_" + value err := os.Setenv(key, "TEST") gtest.Assert(err, nil) gtest.AssertEQ(genv.Get(key), "TEST") }) } -func Test_Genv_Set(t *testing.T) { +func Test_GEnv_Contains(t *testing.T) { gtest.Case(t, func() { - key := "TEST_SET_ENV" + value := gconv.String(gtime.Nanosecond()) + key := "TEST_ENV_" + value + err := os.Setenv(key, "TEST") + gtest.Assert(err, nil) + gtest.AssertEQ(genv.Contains(key), true) + gtest.AssertEQ(genv.Contains("none"), false) + }) +} + +func Test_GEnv_Set(t *testing.T) { + gtest.Case(t, func() { + value := gconv.String(gtime.Nanosecond()) + key := "TEST_ENV_" + value err := genv.Set(key, "TEST") gtest.Assert(err, nil) gtest.AssertEQ(os.Getenv(key), "TEST") }) } -func Test_Genv_Remove(t *testing.T) { +func Test_GEnv_Build(t *testing.T) { gtest.Case(t, func() { - key := "TEST_REMOVE_ENV" + s := genv.Build(map[string]string{ + "k1":"v1", + "k2":"v2", + }) + gtest.AssertIN("k1=v1", s) + gtest.AssertIN("k2=v2", s) + }) +} + +func Test_GEnv_Remove(t *testing.T) { + gtest.Case(t, func() { + value := gconv.String(gtime.Nanosecond()) + key := "TEST_ENV_" + value err := os.Setenv(key, "TEST") gtest.Assert(err, nil) err = genv.Remove(key) diff --git a/g/os/gproc/gproc.go b/g/os/gproc/gproc.go index 0353c16e3..51ff3187b 100644 --- a/g/os/gproc/gproc.go +++ b/g/os/gproc/gproc.go @@ -9,13 +9,14 @@ package gproc import ( "bytes" - "github.com/gogf/gf/g/os/gfile" - "github.com/gogf/gf/g/util/gconv" "io" "os" "runtime" "strings" "time" + + "github.com/gogf/gf/g/os/gfile" + "github.com/gogf/gf/g/util/gconv" ) const ( @@ -91,9 +92,9 @@ func ShellRun(cmd string) error { } // 阻塞执行shell指令,并返回输出结果(如果需要异步,请使用goroutine) -func ShellExec(cmd string) (string, error) { +func ShellExec(cmd string, environment ...[]string) (string, error) { buf := bytes.NewBuffer(nil) - p := NewProcess(getShell(), []string{getShellOption(), cmd}) + p := NewProcess(getShell(), []string{getShellOption(), cmd}, environment...) p.Stdout = buf err := p.Run() return buf.String(), err