Browse Source

init

master v0.1.0
guzeng 3 years ago
commit
ac839a37de
2 changed files with 49 additions and 0 deletions
  1. +32
    -0
      terminal.go
  2. +17
    -0
      terminal_test.go

+ 32
- 0
terminal.go View File

@ -0,0 +1,32 @@
package terminal
import (
"bytes"
"log"
"os/exec"
)
/* 调用系统终端 */
func UseTerminal(name string, args ...string) map[string]interface{} {
cmd := exec.Command(name, args...)
var out bytes.Buffer
mRet := make(map[string]interface{})
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
log.Println("ERROR", "140133|系统命令", name, args, "执行错误", err.Error())
mRet["ReturnCode"] = "140133"
mRet["Msg"] = "系统终端命令执行出错"
return mRet
}
mRet["ReturnCode"] = "1"
mRet["Msg"] = "命令执行成功,获取输出成功"
mRet["Result"] = out.String()
return mRet
}

+ 17
- 0
terminal_test.go View File

@ -0,0 +1,17 @@
package terminal
import (
"testing"
)
func Test_UseTerminal(t *testing.T) {
name := "sudo"
a, b, c, d, e := "-u", "www-data", "php", "/www/web/nextcloud/occ", "trashbin:cleanup"
mRet := UseTerminal(name, a, b, c, d, e)
//mRet := UseTerminal("sudo -u www-data php occ --help")
t.Log(mRet)
}

Loading…
Cancel
Save