Skip to main content

Enumerations

Dagger supports custom enumeration (enum) types, which can be used to restrict possible values for a string argument.

note

Following the GraphQL specification, enums are represented as strings in the Dagger API GraphQL schema and follow these rules:

  • Enum names cannot start with digits, and can only be composed of alphabets, digits or _.
  • Enum values are case-sensitive, and by convention should be upper-cased.

Here is an example of a Dagger Function that takes two arguments: an image reference and a severity filter. The latter is defined as an enum named Severity:

package main

import (
"context"
)

type MyModule struct{}

// Vulnerability severity levels
type Severity string

const (
// Undetermined risk; analyze further.
Unknown Severity = "UNKNOWN"

// Minimal risk; routine fix.
Low Severity = "LOW"

// Moderate risk; timely fix.
Medium Severity = "MEDIUM"

// Serious risk; quick fix needed.
High Severity = "HIGH"

// Severe risk; immediate action.
Critical Severity = "CRITICAL"
)

func (m *MyModule) Scan(ctx context.Context, ref string, severity Severity) (string, error) {
ctr := dag.Container().From(ref)

return dag.Container().
From("aquasec/trivy:0.50.4").
WithMountedFile("/mnt/ctr.tar", ctr.AsTarball()).
WithMountedCache("/root/.cache", dag.CacheVolume("trivy-cache")).
WithExec([]string{
"trivy",
"image",
"--format=json",
"--no-progress",
"--exit-code=1",
"--vuln-type=os,library",
"--severity=" + string(severity),
"--show-suppressed",
"--input=/mnt/ctr.tar",
}).Stdout(ctx)
}

Enumeration choices will be displayed when calling --help on a Dagger Function:

dagger call scan --help

The result will be:

USAGE
dagger call scan [arguments]

ARGUMENTS
--ref string [required]
--severity UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL [required]

Here's an example of calling the Dagger Function with an invalid enum argument:

dagger call scan --ref=hello-world:latest --severity=FOO

This will result in an error that displays possible values, as follows:

Error: invalid argument "FOO" for "--severity" flag: value should be one of UNKNOWN,LOW,MEDIUM,HIGH,CRITICAL
Run 'dagger call scan --help' for usage.