#Anyone got a good example of taking `.

1 messages ยท Page 1 of 1 (latest)

hoary cargo
#

/cc @willow schooner @stoic epoch @hollow frigate

willow schooner
#

haven't seen anything around ๐Ÿ™ . Should be straightforward to implement.

hoary cargo
#

@willow schooner I can use PHP to grab the .env and split on "\n" and loop over it

stoic epoch
#

Yeah we just did this @woven prism do you have the code sample handy somewhere. (from the legend work)

hoary cargo
#

๐Ÿ˜„ thanks! looking fwd to seeing it

woven prism
hoary cargo
#

@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

woven prism
#

Sounds like a good PR or issue ๐Ÿ™‚ Module was made by @pliant drift

pliant drift
#

Ohh yeah! I need to update it because I made it loog time ago

stoic epoch
#

@woven prism wasn't there some other thing we did using current_module - or am I completely hallucinating?

woven prism
#

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

GitHub

Contribute to jpadams/legend-daggerize-minimal-maven development by creating an account on GitHub.

hoary cargo
#

@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?

stoic epoch
#

I believe you must do stdout()

#

Yes and then you woul dalso need to say WithWorkDir($dir)

woven prism
#

contents(), right?

#

Oh I see , withExec in there

stoic epoch
#

contents would tell you the files -- but I assume you want the value of a file, right Paul?

woven prism
#

entries() is like ls

#

contents() returns a string

stoic epoch
#

Oh -- sorry I am confused!

woven prism
#

Afkb for a quick walk, will weigh in in a little bit

hoary cargo
#

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' ?

stoic epoch
#

Yes it should be something like

->file(".env)->contents()

hoary cargo
#

testing

#

is this on Directory $source?

#

or is this on Container?

stoic epoch
#

Its on directory

hoary cargo
#

$envContents = $source->file('.env')->contents();

#

testing

#

WORKED โค๏ธโ€๐Ÿ”ฅ

stoic epoch
#

nice!

hoary cargo
#

@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

#

๐Ÿ™‚

hoary cargo
#

Now we're cookin'

gnight

woven prism
dull ravine
#

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))

hoary cargo
#

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

hollow frigate
# hoary cargo Hey <@163822683799158784> - no you can't modify the Container class, as that's o...

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:

hoary cargo
#

Oh