#how to get a directory path
1 messages · Page 1 of 1 (latest)
You can't get the path of that directory because there's no guarantee that it even has a path anywhere. It might have been fetched from a git repo, or copied from a container, or dynamically created.
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
I think you can pass the contents of the file as a stream:
-
Get the contents of the file with
.file(".env").contents() -
Pass the contents to load-env:
load_dotenv(stream=StringIO(contents))
Assuming this is the lib you're using: https://pypi.org/project/python-dotenv/
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?
@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
yes, but it'll get printed masked since dagger doesn't allow printing secrets for security reasons
will try, thanks
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
thanks, let me try maybe im doing something wrong
fyi @ocean olive @foggy lintel
make sure you're passing env:NAME in the cli
not sure to follow you @severe anvil