英文:
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, "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()
}
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("--daemon --conf=%s", 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(
"/app/eunod",
"--daemon",
fmt.Sprintf("--conf=%s", configPath),
)
You can learn more about variadic arguments here https://golangdocs.com/variadic-functions-in-golang for example.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论