GoLang项目CMD命令表现奇怪

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

GoLang project CMD command have strange behave

问题

我正在为Rosetta-API的GoLang项目工作。
有一个exec.Command的实现。

代码:

func StartBitcoind(ctx context.Context, configPath string, g *errgroup.Group) error {
    logger := utils.ExtractLogger(ctx, "eunod")
    cmd := exec.Command(
        "/app/eunod",
        fmt.Sprintf("--conf=%s", configPath),
    )

    stdout, err := cmd.StdoutPipe()
    if err != nil {
        return err
    }

    stderr, err := cmd.StderrPipe()
    if err != nil {
        return err
    }

    g.Go(func() error {
        return logPipe(ctx, stdout, bitcoindLogger)
    })

    g.Go(func() error {
        return logPipe(ctx, stderr, bitcoindStdErrLogger)
    })

    if err := cmd.Start(); err != nil {
        return fmt.Errorf("%w: unable to start eunod", err)
    }

    g.Go(func() error {
        <-ctx.Done()

        logger.Warnw("sending interrupt to eunod")
        return cmd.Process.Signal(os.Interrupt)
    })

    return cmd.Wait()
}

从现在开始,我将链接到GitHub。

在这一行:https://github.com/ScArFaCe2020/rosetta-euno/blob/master/bitcoin/node.go#L68-L71 是exec.Command。

fmt.Sprintf("--conf=%s", configPath)可以正常工作。
现在我需要在其中添加--daemon。

但是当我将其更改为

fmt.Sprintf("--daemon --conf=%s", configPath),

它不起作用。

有人有什么主意如何添加--daemon吗?

英文:

I'm working on a GoLang Project for Rosetta-API.
There is a implementation for exec.Command

Code:

    func StartBitcoind(ctx context.Context, configPath string, g *errgroup.Group) error {
	logger := utils.ExtractLogger(ctx, &quot;eunod&quot;)
	cmd := exec.Command(
		&quot;/app/eunod&quot;,
		fmt.Sprintf(&quot;--conf=%s&quot;, configPath),
	)

	stdout, err := cmd.StdoutPipe()
	if err != nil {
		return err
	}

	stderr, err := cmd.StderrPipe()
	if err != nil {
		return err
	}

	g.Go(func() error {
		return logPipe(ctx, stdout, bitcoindLogger)
	})

	g.Go(func() error {
		return logPipe(ctx, stderr, bitcoindStdErrLogger)
	})

	if err := cmd.Start(); err != nil {
		return fmt.Errorf(&quot;%w: unable to start eunod&quot;, err)
	}

	g.Go(func() error {
		&lt;-ctx.Done()

		logger.Warnw(&quot;sending interrupt to eunod&quot;)
		return cmd.Process.Signal(os.Interrupt)
	})

	return cmd.Wait()
}

From now i will linked to github.

On line: https://github.com/ScArFaCe2020/rosetta-euno/blob/master/bitcoin/node.go#L68-L71 is the exec.Command.

the fmt.Sprintf("--conf=%s", configPath), Works fine.
Now i need to add --daemon inside it.

But when i change this to

fmt.Sprintf(&quot;--daemon --conf=%s&quot;, configPath),

It doesn't work.

Does anyone have a idea how i can add the --daemon?

答案1

得分: 1

exec.Command接受可变参数的字符串参数。其签名如下所示。

func Command(name string, arg ...string) *Cmd

要么传递一个字符串切片,后跟...,要么传递多个字符串作为参数。

在你的情况下,你将整个字符串--daemon --conf=/some/path视为单个参数,而实际上你想将其视为两个不同的参数。

最简单的解决方法是这样做。

cmd := exec.Command(
    "/app/eunod",
    "--daemon",
    fmt.Sprintf("--conf=%s", configPath),
)

你可以在这里了解更多关于可变参数的信息,例如:https://golangdocs.com/variadic-functions-in-golang。

英文:

exec.Command takes variadic string arguments. Its signature looks like this.

func Command(name string, arg ...string) *Cmd

Either pass a slice of stings followed by ... or pass a number of strings as arguments.

In your case, you treat the whole string --daemon --conf=/some/path as a single argument, while you actually want to treat it as two distinct arguments.

The most simple way to solve this would be doing it like this.

cmd := exec.Command(
    &quot;/app/eunod&quot;,
    &quot;--daemon&quot;,
    fmt.Sprintf(&quot;--conf=%s&quot;, configPath),
)

You can learn more about variadic arguments here https://golangdocs.com/variadic-functions-in-golang for example.

huangapple
  • 本文由 发表于 2022年2月10日 19:25:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/71064239.html
匿名

发表评论

匿名网友

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

确定