#how to get a directory path

1 messages · Page 1 of 1 (latest)

shut arch
#

I want to be able to get the path of a given directory. If i run my module passing parameter dir of type Directory how to get that directory system path as string?

severe anvil
shut arch
#

ok here its is my user case:
when running my module i pass variable working_dir as Directory all inside dagger work ok with it. but i have a .env file which i like to parse using load_dotenv lib that requiers a path to the given .env file. Which is the best way to do that with dagger? i have the working_dir property and i know i can do .file(".env") to get to the file, but i require the files path

severe anvil
#

I think you can pass the contents of the file as a stream:

  1. Get the contents of the file with .file(".env").contents()

  2. Pass the contents to load-env:

load_dotenv(stream=StringIO(contents))
shut arch
#

got it will try that way. that said for new developers its kind of confusing/missing feature not been able to get a dir or file paths on the host. there is a big switch of paradigm when it comes to dagger. My advice is that the doc manage to explain this in a better way to easy the learning curve

#

in another unrelated quesiton, and sorry if i should not do this in this thread; if i have a container ctr = dag.container().from_(address="alpine") and do ctr = ctr.with_secret_variable(name="MY_ENV_VAR", secret=secret) then ctr = ctr.with_exec(["printenv"]) should print the new MY_ENV_VAR as well right?

glossy hare
#

@shut arch wondering if you've tried something like this:

    async def container_echo(self, dir: Directory) -> str | None:
        await dir.file(".env").export(".env")
        loaded = load_dotenv(".env")
        return os.getenv("FOO")

^ this seems to work and I don't seem to need to speciy the full path anywhere

glossy hare
shut arch
glossy hare
# shut arch for some reason does not get printed

just ran this:

    async def container_echo(self, dir: Directory) -> str | None:
        s = dag.set_secret("foo", "SECRET")
        return await dag.container().\
        from_("alpine:latest").with_secret_variable("FOO",s).with_exec(["printenv"]).stdout()

getting this:

246: dagger call container-echo
246: [2.82s] PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
246: [2.82s] FTP_PROXY={"parentClientIDs":["ii1h0eop2pz8enisozngu4ihu","9svauz22nuic68rivjr04dy7l"],"serverID":"i6y2pjgk5d36gt2djlstc5na5","progSockPath":"/run/dagger/server-progrock-7xwezh2rlcdzic4u50oa7jc1v.sock","progParent":"xxh3:1dd8c99c47f89edd"}
246: [2.82s] FOO=***
246: [2.82s] OTEL_TRACES_EXPORTER=otlp
246: [2.82s] OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=unix:///dev/otel-grpc.sock
246: [2.82s] OTEL_EXPORTER_OTLP_TRACES_PROTOCOL=grpc
246: [2.82s] _DAGGER_PARENT_CLIENT_IDS=ii1h0eop2pz8enisozngu4ihu 9svauz22nuic68rivjr04dy7l
246: [2.82s] HOME=/root
#

^ you can see FOO being masked there

shut arch
#

thanks, let me try maybe im doing something wrong

severe anvil
severe anvil
shut arch
#

not sure to follow you @severe anvil