2.2 Functions Chaining
Functions Chaining
Dagger Functions can return either basic types or objects. Objects can define their own functions.
So when calling a Dagger Function that returns an object, the Dagger API lets you follow up by calling one of that object’s
functions, which itself can return another object, and so on.
This is called “function chaining”, and is a core feature of Dagger.
Dagger’s core types (Container , Directory , File , Service , …) are all objects. They each define various functions for interacting with their respective objects.
Let’s explore them step by step:
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
--help
show available 'module' functions
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
ssh-service --help
show available 'service' object functions
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
ssh-service up --help
show available 'up' function arguments
Now that we have got all the pieces together, let’s expose a Service returned by a Dagger Function on a specified host port,
by chaining a call to the Service objects up function:
Note
TheService object is returned by the modules ssh-service function.dagger call \
--mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
ssh-service up --ports=22022:22
Here we print the contents of a File returned by a Dagger Function, by chaining a call to the File objects contents function:
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
lint --source=https://github.com/puzzle/puzzle-radicale-auth-ldap report contents
Task 2.2.1: Chain calls
Display and return the contents of the /etc/os-release file in a container, by chaining additional calls to the Container
object, returned by the modules wolfi function:
show hint
Have a look at the WithExec() and Stout() functions.
show solution
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
wolfi with-exec --args="cat","/etc/os-release" stdout
Try an alternative approach using File() instead.
show hint
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
wolfi file --help
show solution
dagger call \
--mod github.com/puzzle/dagger-techlab/mod@v1.0.0 \
wolfi file --path=/etc/os-release contents