+ helper StrConcat & EnvMultiSet & EnvMultiGet
This commit is contained in:
parent
9c936cab4c
commit
53b63557f8
5 changed files with 73 additions and 0 deletions
|
|
@ -1,6 +1,8 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/yaoapp/gou"
|
||||
|
|
@ -20,3 +22,31 @@ func ProcessEnvSet(process *gou.Process) interface{} {
|
|||
value := process.ArgsString(1)
|
||||
return os.Setenv(name, value)
|
||||
}
|
||||
|
||||
// ProcessEnvMultiGet xiang.helper.MultiGet 读取ENV
|
||||
func ProcessEnvMultiGet(process *gou.Process) interface{} {
|
||||
process.ValidateArgNums(1)
|
||||
res := map[string]string{}
|
||||
for i := range process.Args {
|
||||
name := fmt.Sprintf("%v", process.Args[i])
|
||||
res[name] = os.Getenv(name)
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
// ProcessEnvMultiSet xiang.helper.MultiSet 设置ENV
|
||||
func ProcessEnvMultiSet(process *gou.Process) interface{} {
|
||||
process.ValidateArgNums(1)
|
||||
envs := process.ArgsMap(0)
|
||||
message := ""
|
||||
for name, value := range envs {
|
||||
err := os.Setenv(name, fmt.Sprintf("%v", value))
|
||||
if err != nil {
|
||||
message = message + ";" + err.Error()
|
||||
}
|
||||
}
|
||||
if message != "" {
|
||||
return errors.New(message)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ import (
|
|||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yaoapp/gou"
|
||||
"github.com/yaoapp/kun/maps"
|
||||
)
|
||||
|
||||
func TestProcessEnv(t *testing.T) {
|
||||
|
|
@ -13,3 +14,11 @@ func TestProcessEnv(t *testing.T) {
|
|||
test := gou.NewProcess("xiang.helper.EnvGet", "XIANG_UNIT_TEST").Run().(string)
|
||||
assert.Equal(t, "FOO", test)
|
||||
}
|
||||
|
||||
func TestProcessEnvMulti(t *testing.T) {
|
||||
err := gou.NewProcess("xiang.helper.EnvMultiSet", maps.Map{"XIANG_UNIT_TEST": "FOO", "XIANG_UNIT_TEST2": "BAR"}).Run()
|
||||
assert.Nil(t, err)
|
||||
test := gou.NewProcess("xiang.helper.EnvMultiGet", "XIANG_UNIT_TEST", "XIANG_UNIT_TEST2").Run().(map[string]string)
|
||||
assert.Equal(t, "FOO", test["XIANG_UNIT_TEST"])
|
||||
assert.Equal(t, "BAR", test["XIANG_UNIT_TEST2"])
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,6 +20,8 @@ func init() {
|
|||
gou.RegisterProcessHandler("xiang.helper.MapGet", ProcessMapGet)
|
||||
gou.RegisterProcessHandler("xiang.helper.MapSet", ProcessMapSet)
|
||||
|
||||
gou.RegisterProcessHandler("xiang.helper.StrConcat", ProcessStrConcat)
|
||||
|
||||
gou.RegisterProcessHandler("xiang.helper.Captcha", ProcessCaptcha)
|
||||
gou.RegisterProcessHandler("xiang.helper.CaptchaValidate", ProcessCaptchaValidate)
|
||||
|
||||
|
|
@ -35,6 +37,8 @@ func init() {
|
|||
|
||||
gou.RegisterProcessHandler("xiang.helper.EnvSet", ProcessEnvSet)
|
||||
gou.RegisterProcessHandler("xiang.helper.EnvGet", ProcessEnvGet)
|
||||
gou.RegisterProcessHandler("xiang.helper.EnvMultiSet", ProcessEnvMultiSet)
|
||||
gou.RegisterProcessHandler("xiang.helper.EnvMultiGet", ProcessEnvMultiGet)
|
||||
|
||||
gou.RegisterProcessHandler("xiang.helper.Print", ProcessPrint)
|
||||
|
||||
|
|
|
|||
17
helper/string.process.go
Normal file
17
helper/string.process.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/yaoapp/gou"
|
||||
)
|
||||
|
||||
// ProcessStrConcat xiang.helper.StrConcat 连接字符串
|
||||
func ProcessStrConcat(process *gou.Process) interface{} {
|
||||
process.ValidateArgNums(2)
|
||||
res := ""
|
||||
for i := range process.Args {
|
||||
res = fmt.Sprintf("%v%v", res, process.Args[i])
|
||||
}
|
||||
return res
|
||||
}
|
||||
13
helper/string_test.go
Normal file
13
helper/string_test.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package helper
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/yaoapp/gou"
|
||||
)
|
||||
|
||||
func TestProcessStrConcat(t *testing.T) {
|
||||
res := gou.NewProcess("xiang.helper.StrConcat", "FOO", 20, "BAR").Run().(string)
|
||||
assert.Equal(t, "FOO20BAR", res)
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue