Skip to content

Commit

Permalink
Add functions part
Browse files Browse the repository at this point in the history
  • Loading branch information
RetGal committed Aug 21, 2024
1 parent e7d5160 commit a86d615
Showing 1 changed file with 73 additions and 47 deletions.
120 changes: 73 additions & 47 deletions content/en/docs/01/_index.md
Original file line number Diff line number Diff line change
@@ -1,88 +1,114 @@
---
title: "1. Dagger Training Sample Chapter"
title: "1. Functions and Chaining "
weight: 1
sectionnumber: 1
---

## Title 1
## 1. Functions and Chaining

{{% alert title="Note" color="primary" %}}
Sample Note
{{% /alert %}}
### Function Calls from the CLI

The most common way to call Dagger Functions is using the `dagger` CLI:

Sample code block:
```bash
echo "Hello World!"
dagger -m github.com/shykes/daggerverse/[email protected] call hello
```

{{% onlyWhen variant1 %}}
This is only rendered when `enabledModule` in `config.toml` contains `variant1`.
{{% /onlyWhen %}}
The `dagger` CLI is first loads a `hello` module directly from its [GitHub repository](https://github.com/shykes/daggerverse/tree/main/hello) and then executes the `Hello()` function from that module.

{{% onlyWhen variant2 %}}
This is only rendered when `enabledModule` in `config.toml` contains `variant2`.
{{% /onlyWhen %}}
After a while you should see:

{{% onlyWhen variant1 variant2 %}}
This is only rendered when `enabledModule` in `config.toml` contains `variant1` or `variant2`.
{{% /onlyWhen %}}
```
hello, world!
```

{{% onlyWhen variant9 %}}
This is only rendered when `enabledModule` in `config.toml` contains `variant9`.
{{% /onlyWhen %}}
{{% alert title="Note" color="primary" %}}
Due to Daggers caching mechanism, subsequent calls will be executed much faster!
{{% /alert %}}

{{% onlyWhenNot variant1 %}}
This is only rendered when `enabledModule` in `config.toml` **does not** contain `variant1`.
{{% /onlyWhen %}}

{{% onlyWhenNot variant2 %}}
This is only rendered when `enabledModule` in `config.toml` **does not** contain `variant2`.
{{% /onlyWhen %}}
### Exploring Modules and Functions

{{% onlyWhenNot variant1 variant2 %}}
This is only rendered when `enabledModule` in `config.toml` **does not** contain `variant1` **nor** `variant2`.
{{% /onlyWhen %}}
If you are curious, what other functions are available on this module, you can either have a look at its [source code](https://github.com/shykes/daggerverse/blob/main/hello/main.go)
or you can explore its functions using:

{{% onlyWhenNot variant9 %}}
This is only rendered when `enabledModule` in `config.toml` **does not** contain `variant9`.
{{% /onlyWhen %}}
```bash
dagger -m github.com/shykes/daggerverse/[email protected] functions
```

In this particular case, there aren't any other functions :( - but what about additional arguments of the `Hello()` function?
Let's find out:

## Title 2
```bash
dagger -m github.com/shykes/daggerverse/[email protected] call hello --help
```

{{% alert title="Note" color="primary" %}}
Additional to the available arguments, this often also shows you the type of value a particular argument expects.
{{% /alert %}}

```yaml
foo: bar
```

### Function Arguments

## Task 1.1: Fix Deployment
Dagger Functions can accept arguments. In addition to basic types (string, boolean, integer, array...),
Dagger also defines powerful core types: Directory, File, Container, Service, and Secret.

#### String Arguments

```yaml
foo: bar
To pass a string argument to a Dagger Function, append the corresponding flag to the dagger call command, followed by the string value:

```bash
dagger -m github.com/shykes/daggerverse/[email protected] call hello --name=sun
```

#### Boolean Arguments

To pass a boolean argument to a Dagger Function, simply add the corresponding flag:

- To set the argument to true: `--foo=true`, or simply `--foo`
- To set the argument to false: `--foo=false`

#### Directory Arguments
You can also pass a directory argument. To do so, add the corresponding flag, followed by a local filesystem path
**or** a remote Git reference.

## Task 1.2: Fix Release
In **both** cases, the `dagger` CLI will convert it to an object referencing the contents of that filesystem path or Git repository location,
and pass the resulting `Directory` object as argument to the Dagger Function.

#### Container Arguments

```yaml
foo: bar
Same as directories, you can pass a container argument. To do so, add the corresponding flag, followed by the address of an OCI image.

The CLI will dynamically pull the image, and pass the resulting `Container` object as argument to the Dagger Function.

```bash
dagger -m github.com/jpadams/daggerverse/[email protected] call scan-container --ctr=alpine:latest
```
{{% alert title="Note" color="primary" %}}
It is important to know that in Dagger, a `Container` object is not merely a string referencing an image on a remote registry.
It is the **actual state of a container**, managed by the Dagger Engine, and passed to a Dagger Function's code as if it were just another variable!
{{% /alert %}}

#### Secret Arguments

Dagger allows you to use confidential information, such as passwords, tokens, etc., in your Dagger Functions, without exposing them in plaintext logs,
writing them into the filesystem of containers you're building, or inserting them into the cache.

## Task 1.3: Fix Release again
To pass a secret to a Dagger Function, source it from a host environment variable `env:`, the host filesystem `file:`, or a host command `cmd:`.

Here is an example of passing a GitHub access token from an environment variable named `GITHUB_TOKEN` to a Dagger Function.
The Dagger Function uses the token to query the GitHub CLI for a list of issues in the Dagger open-source repository:

```yaml
foo: bar
```bash
dagger -m github.com/aweris/daggerverse/gh@99a1336f8091ff43bf833778a324de1cadcf25ac call run --token=env:GITHUB_TOKEN --cmd="issue list --repo=dagger/dagger"
```


## Task 1.4: Fix Release again and again
### Task 1.1: Make use of arguments

Call the `Hello()` function so that it returns the string `Welcome, sunshine!` in ASCII-art.

```yaml
foo: bart
```bash
dagger -m github.com/shykes/daggerverse/[email protected] call hello --giant --greeting=Welcome --name=sunshine
```

0 comments on commit a86d615

Please sign in to comment.