#Anyone got a good example of taking `.
1 messages ยท Page 1 of 1 (latest)
haven't seen anything around ๐ . Should be straightforward to implement.
@willow schooner I can use PHP to grab the .env and split on "\n" and loop over it
Yeah we just did this @woven prism do you have the code sample handy somewhere. (from the legend work)
๐ thanks! looking fwd to seeing it
@hoary cargo how about this?
https://daggerverse.dev/mod/github.com/quartz-technology/daggerverse/magicenv@627fc4df7de8ce3bd8710fa08ea2db6cf16712b3
@woven prism looks decent, I'll try it out.
btw .. maybe you can add a 2nd user argument for pattern so if the key starts with a pattern, then include it
magicenv('.env', 'DB_')
As if I call this on the MySQL container, then I only want the DB_* ones .. whereas the webapp, I want them all. It's a good way to avoid conflicts IMO
Sounds like a good PR or issue ๐ Module was made by @pliant drift
Ohh yeah! I need to update it because I made it loog time ago
@woven prism wasn't there some other thing we did using current_module - or am I completely hallucinating?
We did. But not necessary, I think.
Iโm currently using this command to good effect to fill in templates with $FOO
https://github.com/jpadams/legend-daggerize-minimal-maven/blob/main/dagger/src/index.ts#L168
Install package with envsubst in it.
Use it:
https://github.com/jpadams/legend-daggerize-minimal-maven/blob/main/dagger/src/index.ts#L98
@stoic epoch let's move the chat to here ๐
@woven prism
I wanna grab the contents of a file, from a Directory and store it in a variable.. specifically the .env file
is there a way on the Directory class to grab the contents of a file? or so I have to do
function(Directory $dir) {
$this->container()->withExec(['cat', '.env'])->stdout();
}
Where do I use $dir ?
probably withMountedDirectory() first?
I believe you must do stdout()
Yes and then you woul dalso need to say WithWorkDir($dir)
contents would tell you the files -- but I assume you want the value of a file, right Paul?
Oh -- sorry I am confused!
Afkb for a quick walk, will weigh in in a little bit
build() = dockerBuld() btw ..
and then once I've got a container that's been built from a Dockerfile .. then I wanna load the .env vars into that container
(after I'm gonna do WithService('db') )
Is there a better way to get a file's contents, instead of 'cat' ?
Yes it should be something like
->file(".env)->contents()
Its on directory
nice!
@stoic epoch @woven prism here is the sexyness
PHP has builtin parse_ini_string ... which is what the .env file format is .. so i used file()->contents() and took it from there
I just added that $prefix arg .. .so I can say I want all the DB_ keys or all the REDIS_ keys or whatnot ..
Now .. this is how I load all env vars
$container = $this->loadContainerEnvVars($container, $source);
and this is how I only load the DB args
$container = $this->loadContainerEnvVars($container, $source, 'DB_');
but inside the code I can break out and call $this-getEnvVars($source, 'DB_') if I just want them as an array, rather than it auto-adding them to the running $container
๐
Now we're cookin'
gnight

I am not a php dev so forgive me if I'm off base. Does php support higher order functions? You could use With to maybe clean this up even further. Here's Go ref - https://pkg.go.dev/dagger.io/dagger#Container.With
Here's a very simple example
func loadEnvVars(d *Directory) WithContainerFunc {
return func(r *Container) *Container {
// Process env vars and add to container. Return it.
}
}
Then when you set up the container
myContainer.With(loadEnvVars(myDir))
Hey @dull ravine - no you can't modify the Container class, as that's owned by the Dagger SDK, and not the userland caller.
We actually do this a lot in PHP, by adding a with() method to things, so it'll modify and return a new instance.
Ways to achieve this are to sub-class it, and make your own child Container class, to add a 'with' method ... and then it'll work like Golang
He's talking about a feature that every SDK should have, including PHP. See SDK Contribution Guide > Helpers. This used to be documented in a guide. All of our SDKs work the same way here:
- Go
- Python
- TypeScript
This is done in codegen, by detecting if an object type has any fields that return the same object type, in which case the method is added to the resulting client binding of the object (example in Python). See original issue for this feature: https://github.com/dagger/dagger/issues/3885.
Oh