使用Go的exec包收集git命令的输出。

huangapple go评论82阅读模式
英文:

Collect git command's output with Go exec

问题

我对Go语言非常陌生;作为一部分创新时间,我决定稍微尝试一下。我想做的是运行一些命令并处理它们的输出。

我写了这个函数来运行命令:

func IssueCommand(command string, args []string) ([]string, error) {
    cmd := exec.Command(command, args[0:len(args)]...)

    stdout, err := cmd.StdoutPipe()
    err = cmd.Start()

    if err != nil {
        return nil, err
    }

    defer cmd.Wait()
    buff := bufio.NewScanner(stdout)

    var returnText []string

    for buff.Scan() {
        returnText = append(returnText, buff.Text())
    }

    return returnText, nil
}

我想运行这个git命令:

git -C /something/something rev-list --count --date=local --all --no-merges

然而,我一直得到一个空数组作为结果。我尝试像这样调用函数:

args := [7]string{"-C", path, "rev-list", "--count", "--date=local", "--all", "--no-merges"}
result, err := IssueCommand("git", args[0:len(args)])

我还尝试修改IssueCommand函数以接受一个字符串作为参数;我这样调用它:

cmd := "-C " + path + " rev-list --count --date=local --all --no-merges"
result, err := IssueCommand("git", cmd)

两次我都得到了一个空数组。它确实捕获了像ls或pwd这样的命令的输出。

再次强调,我只是想对Go有所了解,我会阅读文档的,但目前时间有限。

英文:

I am extremely new to Go; as part of some innovation time, I decided to play around a bit with it. What I'd like to do is run some commands, and process their output.

I came up with this function to run commands:

func IssueCommand(command string, args []string) ([]string, error) {
	cmd := exec.Command(command, args[0:len(args)]...)

	stdout, err := cmd.StdoutPipe()
	err = cmd.Start()

	if err != nil {
		return nil, err
	}

	defer cmd.Wait()
	buff := bufio.NewScanner(stdout)

	var returnText []string

	for buff.Scan() {
		returnText = append(returnText, buff.Text())
	}

	return returnText, nil
}

I wanted to run this git command:

 git -C /something/something rev-list --count --date=local --all --no-merges

However, I keep getting an empty array as a result. I tried calling the function like this:

args := [7]string{"-C ", path, "rev-list", "--count", "--date=local", "--all", "--no-merges"}
result, err := IssueCommand("git", args[0:len(args)])

Also tried modifying the IssueCommand function to take a string for arguments; I called it like this:

cmd := "-C " + path + " rev-list --count --date=local --all --no-merges"
result, err := IssueCommand("git", cmd)

I got an empty array both times. It did capture output from commands like ls or pwd.

Again I am just trying to get a feel for Go, I will RTFM, but I have limited time for now.

答案1

得分: 4

如果您的意图是同步运行该命令并获取其输出,最简单的方法是使用cmd.Output()(运行命令并收集标准输出)或cmd.CombinedOutput()(运行命令并收集标准错误和标准输出)。

func IssueCommand(command string, args []string) ([]string, error) {
    cmd := exec.Command(command, args...)

    out, err := cmd.Output()
    if err != nil {
        return nil, err
    }

    lines := strings.Split(string(out), "\n")
    return lines, nil
}
英文:

If your intention is to run that command synchronously and get its output, the easiest way is to use cmd.Output() (run command and collect stdout) or cmd.CombinedOutput() (run command and collect stderr+stdout)

func IssueCommand(command string, args []string) ([]string, error) {
    cmd := exec.Command(command, args...)

    out, err := cmd.Output()
    if err != nil {
        return nil, err
    }

    lines := strings.Split(string(out), "\n")
    return lines, nil
}

huangapple
  • 本文由 发表于 2022年8月2日 20:16:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/73207401.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定