Use env variables with System.cmd or Ports?

I’m trying to figure out how to be able to expand ENV variables in either Port or System.cmd. Porcelain seems to be capable of this, but I cannot do it with either Port or System.cmd:

iex(5)> System.cmd("/bin/echo", ["$GOPATH"])
{"$GOPATH\n", 0}
iex(6)> port = Port.open({:spawn_executable, "/bin/echo"}, [:binary, args: ["$GOPATH"]])
#Port<0.10>
iex(7)> flush()
{#Port<0.10>, {:data, "$GOPATH\n"}}
:ok
Porcelain.shell("echo $GOPATH")
%Porcelain.Result{err: nil, out: "/Users/rlthompson/go\n", status: 0}

The issue is Porcelain is in a state where I am uncomfortable using it in production (it’s basically abandonware in my personal opinion). I tried taking a look at how Porcelain is accomplishing this, but was unable to make heads or tails of the specific calls they make.

Any help, direction or docs to be pointed at would be greatly appreciated. Thank you in advance.

What makes you say this? It’s a well defined library that could be considered complete. It’s not like the *nix CLI is a moving target that the library needs to be updated to work with :slight_smile:

1 Like

That’s just my personal opinion. I’m uncomfortable using it in production for what it the app I would use it in does. This also isn’t just for *nix, the app I would use this in is cross platform. I’m not going to use Porcelain, which is why I’m trying to figure out how to do it with System.cmd or Ports.

Also, it’s a matter of getting this past management as well. I can tell you right now my managers wouldn’t approve the use of a library that isn’t actively kept up.

If you look at the documentation for opening ports in the erlang library, you will find an env option. That is where you would set the environment. You might just have to pass the current environment there to add the options you want.

@isaacsanders I saw that in the otp docs and in the elixir Ports docs, but that doesn’t look it’s usable for existing env variables, just for passing a env variable at the time of opening the port. I could be wrong though.

Also, I would advise just using the underlying port infrastructure in Erlang’s and Elixir’s standard libraries if Porcelain makes you uncomfortable.

If you are trying to get a subset of your environment sent to the port, I would do so explicitly. I am under the impression from what you have posted that it doesn’t look at parameters in your shell, but requires you to set the env for the shell it creates.

1 Like

@isaacsanders so if I’m understanding you correctly you mean this?:

iex(11)> System.cmd("/bin/echo", ["$GOPATH"], env: [{"$GOPATH", "/Users/rlthompson/go"}])
{"$GOPATH\n", 0}

However that doesn’t appear to work either.

Try:

iex(2)> System.cmd("env", [], env: [{"GOPATH", "/Users/rlthompson/go"}])

This shows that it exists in the env.

I think that to get further, you might need a shell script around it.

I tried:

System.cmd("/bin/echo", ["$GOPATH"], env: [{"GOPATH", "/Users/rlthompson/go"}])

and

System.cmd("/bin/echo $GOPATH", [], env: [{"GOPATH", "/Users/rlthompson/go"}])

Neither giving a desired result.

This works:

iex(1)> System.cmd("sh", ["test.sh"], env: [{"GOPATH", "/Users/rlthompson/go"}])
{"/Users/rlthompson/go\n", 0}

test.sh:

echo $GOPATH

Hrm. Maybe I just need to dig even deeper into how Porcelain is pulling this off because their shell/2 command can do this without the use of a script. good tip on the script though. Ideally my users would be grabbing env variables in their check and handler plugins rather than trying to pass them in the check command they are doing. (I know, no context. Sorry)

All I know is you work for GoDaddy (from your slack description), so I imagine there might a lot of sysadmin type features you wish to support.

Correct. This app is a monitoring service where they can create checks and a part of the check is the path to the check plugin and opts to pass to it. So allowing them to do as much as possible is the goal.

System.cmd/3

environment variables will not be interpolated

@peerreynders This must be accomplishable with Ports though is what I figured.

I was just running permutations for experimental benefit.

http://erlang.org/doc/man/erlang.html#open_port-2

Opt = 
    {packet, N :: 1 | 2 | 4} |
    stream |
    {line, L :: integer() >= 0} |
    {cd, Dir :: string() | binary()} |
    {env,
     Env ::
         [{Name :: os:env_var_name(),
           Val :: os:env_var_value() | false}]} |
    {args, [string() | binary()]} |
    {arg0, string() | binary()} |
    exit_status |
    use_stdio |
    nouse_stdio |
    stderr_to_stdout |
    in |
    out |
    binary |
    eof |
    {parallelism, Boolean :: boolean()} |
    hide

This was part of my advice in an earlier post.

@isaacsanders & @peerreynders

iex(16)> port = Port.open({:spawn, "/bin/echo $GOPATH"}, [:binary])
#Port<0.19>
iex(17)> flush
{#Port<0.19>, {:data, "/Users/rlthompson/go\n"}}

:spawn vs :spawn_executable. Now I feel dumb.

1 Like