Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
310 views
in Technique[技术] by (71.8m points)

go - pass ENV VAR to exec.Command?

I'm writing a go wrapper for a popular command line tool (ansible-playbook) and I need to pass a parameter through with the exec.Command call. The bash equivalent would be:

MY_VAR=some_value ansible-playbook -i custom-inventory playbook.yml

Previously I was just exporting MY_VAR using os.Setenv, but that causes problems for parallel executions of the playbook. So I want to pass the var in front of the command so that each call has it's own value for this var.

I'm not really sure how to do this with exec.Command since the first parameter to that function is "command". Any tips?

edit: I have tried using the Env field of the Cmd struct but that overrides all environment variables. I have a significant amount of configuration set and I would just like to override this one specific environment variable. Is this not possible??

question from:https://stackoverflow.com/questions/41133115/pass-env-var-to-exec-command

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...