Re: .env file handling

2017-10-24 Thread Casper Ti. Vector
See also rc(1) :)

On Tue, Oct 24, 2017 at 10:55:28PM +0200, Guillaume Perréal wrote:
> The scoping might be a bit challenging, but the variable substitution system
> is royal. I do not constantly ask myself "what will happen if this variable
> contains a space, or a quote ?".

-- 
My current OpenPGP key:
RSA4096/0x227E8CAAB7AA186C (expires: 2020.10.19)
7077 7781 B859 5166 AE07 0286 227E 8CAA B7AA 186C



Re: .env file handling

2017-10-24 Thread Guillaume Perréal

Le 24/10/2017 à 08:28, Colin Booth a écrit :

I would say that is one of the two difficulties. The other one being
that execline also tries hard not to carry any overhead, which means
that often times you can end up in situations where the aggressive
scoping that it does makes things challenging at best (assuming you're
trying to stay with the spirit of the language).
The scoping might be a bit challenging, but the variable substitution 
system is royal. I do not constantly ask myself "what will happen if 
this variable contains a space, or a quote ?".


--
Guillaume.


Re: .env file handling

2017-10-24 Thread Monty Zukowski
Thank you for the explanations and solutions, things are starting to make more 
sense now. I can live with the envdir approach for my docker containers, or 
provide a tool to parse .env files and create the envdir when loading in the 
settings but before launching a program.

Thanks,

Monty

On Oct 23, 2017, 11:22 PM -0700, wrote:
>
> The underlying difficulty with Monty's question is that execline tries
> to avoid parsing as much as possible, and a file full of key=value
> lines,
> as simple as it is, still requires some parsing. execline wasn't made
> for this; the idiomatic way to store key-value pairs in the filesystem
> for use by an execline script is, as Monty found out, s6-envdir.
> If you really want a shell-like syntax, the best way to handle it is
> with tools that already understand this syntax, such as a shell, or as
> Casper suggested, env.


Re: .env file handling

2017-10-24 Thread Colin Booth
On Tue, Oct 24, 2017 at 06:22:46AM +, Laurent Bercot wrote:
> > I tried rewriting the whole thing in execline and while I'm pretty sure
> > it's doable it's not easy.
> 
>  A direct translation of Casper's script to execline could be:
> 
> #!/command/execlineb -P
> backtick LIST { cat /path/to/xyz.env }
> importas -nsd"\n" LIST LIST
> env -i ${LIST}
> /path/to/xyz
> 
>  It's not as idiomatic as other ways to handle variables in execline,
> but it should work.
I regularly forget that env does its work by chainloading as opposed to
being a built-in that manipulates the shells environment directly. 
> 
>  The underlying difficulty with Monty's question is that execline tries
> to avoid parsing as much as possible, and a file full of key=value lines,
> as simple as it is, still requires some parsing. execline wasn't made
> for this; the idiomatic way to store key-value pairs in the filesystem
> for use by an execline script is, as Monty found out, s6-envdir.
> If you really want a shell-like syntax, the best way to handle it is
> with tools that already understand this syntax, such as a shell, or as
> Casper suggested, env.
> 
I would say that is one of the two difficulties. The other one being
that execline also tries hard not to carry any overhead, which means
that often times you can end up in situations where the aggressive
scoping that it does makes things challenging at best (assuming you're
trying to stay with the spirit of the language).

-- 
Colin Booth


Re: .env file handling

2017-10-24 Thread Laurent Bercot

I tried rewriting the whole thing in execline and while I'm pretty sure
it's doable it's not easy.


 A direct translation of Casper's script to execline could be:

#!/command/execlineb -P
backtick LIST { cat /path/to/xyz.env }
importas -nsd"\n" LIST LIST
env -i ${LIST}
/path/to/xyz

 It's not as idiomatic as other ways to handle variables in execline,
but it should work.

 The underlying difficulty with Monty's question is that execline tries
to avoid parsing as much as possible, and a file full of key=value 
lines,

as simple as it is, still requires some parsing. execline wasn't made
for this; the idiomatic way to store key-value pairs in the filesystem
for use by an execline script is, as Monty found out, s6-envdir.
If you really want a shell-like syntax, the best way to handle it is
with tools that already understand this syntax, such as a shell, or as
Casper suggested, env.

--
 Laurent



Re: .env file handling

2017-10-23 Thread Colin Booth
On Tue, Oct 24, 2017 at 09:25:37AM +0800, Casper Ti. Vector wrote:
> > #!/bin/sh
> > exec env -i $(cat /path/to/xyz.env) /path/to/xyz
> And of course you should be careful with the contents in `xyz.env'.
> 
You can do the same with 
#!/bin/sh
while read A ; do 
  export "$A"
done < "$1"
exec prog...

If you don't want to use cat.

I tried rewriting the whole thing in execline and while I'm pretty sure
it's doable it's not easy. The problem is that scope in execline doesn't
extend past the execution context of a given program, so while the
following program looks like it should work, it doesn't:

#!/command/execlineb
elgetpositionals
redirfd -r 0 $1
withstdinas -n VAR
prog...

since prog will get run with the environmental variable VAR set to the
entire contents of your file. 

Changing it to the following to unwrap VAR ends up with you setting the
variables correctly each loop, but not getting everything pulled into
the environment at once due to the aforementioned scoping:

#!?command/execlineb 
elgetpositionals
redirfd -r 0 $1
foreground { 
  forstdin VAR
  importas -usd= VAR VAR
  export ${VAR}
  s6-echo ""
}
prog...

You can test that it's setting the variables and then losing them by
change `s6-echo ""' to `env'. And setting prog... to 
`foreground { s6-echo "" } env'

Hope is however not all lost. You can do it with execline and s6 as long
as you have a tmpfs laying around somewhere:

#!/command/execlineb 
elgetpositionals
foreground { mkdir -p /run/envdir }
redirfd -r 0 $1
foreground {
  forstdin -Cd"\n" VAR
  importas -u VAR VAR
  multidefine -d= ${VAR} { K V }
  redirfd -w 1 /run/envdir/${k}
  s6-echo ${V}
}
s6-envdir /run/envdir
prog...

That does create a directory somewhere, but it parses a multi-line K=V
style file into something that s6-envdir can handle.

Oh, and it should go without saying, but all these script snippets
assume that you're calling them as `script /path/to/envfile'

Cheers!

--
Colin Booth


Re: .env file handling

2017-10-23 Thread Casper Ti. Vector
> #!/bin/sh
> exec env -i $(cat /path/to/xyz.env) /path/to/xyz
And of course you should be careful with the contents in `xyz.env'.

On Mon, Oct 23, 2017 at 01:30:57PM -0700, Monty Zukowski wrote:
> I've been learning about s6 through the s6-overlay. What I would like
> to do is take a .env file full of x=y lines and have that define env
> vars before running a program. I suspect there is a clever and easy
> way to do that. However, I'm not so clever. The best I've found so
> far is the s6-envdir program.

-- 
My current OpenPGP key:
RSA4096/0x227E8CAAB7AA186C (expires: 2020.10.19)
7077 7781 B859 5166 AE07 0286 227E 8CAA B7AA 186C