From 037d2729ce25cbc946356d96fa84c90421638318 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Fri, 9 Dec 2022 19:20:15 +0800 Subject: [PATCH] doc: update doc for ExecCommand --- docs/system.md | 24 +++++++++++++++++++----- docs/system_zh-CN.md | 24 +++++++++++++++++++----- system/os.go | 6 +++--- 3 files changed, 41 insertions(+), 13 deletions(-) diff --git a/docs/system.md b/docs/system.md index 377be3b..4499487 100644 --- a/docs/system.md +++ b/docs/system.md @@ -211,7 +211,7 @@ func main() { ### CompareOsEnv -

Use shell /bin/bash -c(linux) or cmd (windows) to execute command.

+

Execute shell command, return the stdout and stderr string of command, and error if error occur. param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1. In linux, use /bin/bash -c to execute command, In windows, use powershell.exe to execute command.

Signature: @@ -227,10 +227,24 @@ import ( ) func main() { - out, errout, err := system.ExecCommand("ls") - fmt.Println(out) - fmt.Println(errout) - fmt.Println(err) + // linux or mac + stdout, stderr, err := system.ExecCommand("ls") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + assert.Equal("", stderr) + + // windows + stdout, stderr, err = system.ExecCommand("dir") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + + // error command + stdout, stderr, err = system.ExecCommand("abc") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + if err != nil { + fmt.Println(err.Error()) + } } ``` diff --git a/docs/system_zh-CN.md b/docs/system_zh-CN.md index 3ac5987..c739cb2 100644 --- a/docs/system_zh-CN.md +++ b/docs/system_zh-CN.md @@ -212,7 +212,7 @@ func main() { ### ExecCommand -

使用shell /bin/bash -c(linux) 或 cmd (windows) 执行shell命令

+

执行shell命令,返回命令的stdout和stderr字符串,如果出现错误,则返回错误。参数`command`是一个完整的命令字符串,如ls-a(linux),dir(windows),ping 127.0.0.1。在linux中,使用/bin/bash-c执行命令,在windows中,使用powershell.exe执行命令。

Signature: @@ -228,10 +228,24 @@ import ( ) func main() { - out, errout, err := system.ExecCommand("ls") - fmt.Println(out) - fmt.Println(errout) - fmt.Println(err) + // linux or mac + stdout, stderr, err := system.ExecCommand("ls") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + assert.Equal("", stderr) + + // windows + stdout, stderr, err = system.ExecCommand("dir") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + + // error command + stdout, stderr, err = system.ExecCommand("abc") + fmt.Println("std out: ", stdout) + fmt.Println("std err: ", stderr) + if err != nil { + fmt.Println(err.Error()) + } } ``` diff --git a/system/os.go b/system/os.go index 5146c2c..b975392 100644 --- a/system/os.go +++ b/system/os.go @@ -55,9 +55,9 @@ func CompareOsEnv(key, comparedEnv string) bool { } // ExecCommand execute command, return the stdout and stderr string of command, and error if error occur -// param `command` is a complete command strinig, like, ls -a (linux), dir(windows), ping 127.0.0.1 -// for linux, use /bin/bash -c to execute command -// for windows, use powershell.exe to execute command +// param `command` is a complete command string, like, ls -a (linux), dir(windows), ping 127.0.0.1 +// in linux, use /bin/bash -c to execute command +// in windows, use powershell.exe to execute command func ExecCommand(command string) (stdout, stderr string, err error) { var out bytes.Buffer var errOut bytes.Buffer