Last active
May 17, 2019 13:01
-
-
Save leafsummer/4f9eb8fbbc7a8df82dd9839de8d1da2b to your computer and use it in GitHub Desktop.
[exec the command in shell]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"os/exec" | |
"runtime" | |
"strings" | |
"github.com/axgle/mahonia" | |
) | |
func CmdExec(cmd string) (string, error) { | |
var c *exec.Cmd | |
var data string | |
systime := runtime.GOOS | |
if system == "windows" { | |
argArray := strings.Split("/c "+cmd, " ") | |
c = exec.Command("cmd", argArray...) | |
} else { | |
c = exec.Command("/bin/sh", "-c", cmd) | |
} | |
out, err := c.CombinedOutput() | |
if err != nil { | |
return data, err | |
} | |
data = string(out) | |
if system == "windows" { | |
dec := mahonia.NewDecoder("gbk") | |
data = dec.ConvertString(data) | |
} | |
return data, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment