golang
A collection of functions for building, formatting, testing, linting and scanningyour Go project for vulnerabilities.
Installation
dagger install github.com/purpleclay/daggerverse/golang@v0.5.0
Entrypoint
Return Type
Golang !
Arguments
Name | Type | Description |
---|---|---|
base | Container ! | A custom base image containing an installation of golang. If no image is provided, one is resolved based on the Go version defined within the projects go.mod file. The official Go image is pulled from DockerHub using either the bullseye (< 1.20) or bookworm (> 1.20) variants. |
src | Directory ! | a path to a directory containing the source code |
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH
func (m *myModule) example(base *Container, src *Directory) *Golang {
return dag.
Golang(base, src)
}
@function
def example(base: dagger.Container, src: dagger.Directory) -> dag.Golang:
return (
dag.golang(base, src)
)
@func()
example(base: Container, src: Directory): Golang {
return dag
.golang(base, src)
}
Types
Golang 🔗
Golang dagger module
modVersion() 🔗
Echoes the version of go defined within a projects go.mod file. It expects the go.mod file to be located within the root of the project
Return Type
String !
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH mod-version
func (m *myModule) example(ctx context.Context, base *Container, src *Directory) string {
return dag.
Golang(base, src).
ModVersion(ctx)
}
@function
async def example(base: dagger.Container, src: dagger.Directory) -> str:
return await (
dag.golang(base, src)
.mod_version()
)
@func()
async example(base: Container, src: Directory): Promise<string> {
return dag
.golang(base, src)
.modVersion()
}
withPrivate() 🔗
Enable private Go module support by dynamically constructing a .netrc auto-login configuration file. Each call will append a new auto-login configuration
Return Type
Golang !
Arguments
Name | Type | Default Value | Description |
---|---|---|---|
machine | String ! | - | the remote machine name |
username | Secret ! | - | a user on the remote machine that can login |
password | Secret ! | - | a token (or password) used to login into a remote machine by the identified user |
modules | [String ! ] ! | - | a list of Go module paths that will be treated as private by Go through the GOPRIVATE environment variable |
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH with-private --machine string --username env:MYSECRET --password env:MYSECRET --modules string1 --modules string2
func (m *myModule) example(base *Container, src *Directory, machine string, username *Secret, password *Secret, modules []string) *Golang {
return dag.
Golang(base, src).
WithPrivate(machine, username, password, modules)
}
@function
def example(base: dagger.Container, src: dagger.Directory, machine: str, username: dagger.Secret, password: dagger.Secret, modules: List[str]) -> dag.Golang:
return (
dag.golang(base, src)
.with_private(machine, username, password, modules)
)
@func()
example(base: Container, src: Directory, machine: string, username: Secret, password: Secret, modules: string[]): Golang {
return dag
.golang(base, src)
.withPrivate(machine, username, password, modules)
}
withPrivateLoad() 🔗
Enable private Go module support by loading an existing .netrc auto-login configuration file. Each call will append a new auto-login configuration
Return Type
Golang !
Arguments
Name | Type | Default Value | Description |
---|---|---|---|
cfg | File ! | - | a path to a .netrc auto-login configuration file |
modules | [String ! ] ! | - | a list of Go module paths that will be treated as private by Go through the GOPRIVATE environment variable |
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH with-private-load --cfg file:path --modules string1 --modules string2
func (m *myModule) example(base *Container, src *Directory, cfg *File, modules []string) *Golang {
return dag.
Golang(base, src).
WithPrivateLoad(cfg, modules)
}
@function
def example(base: dagger.Container, src: dagger.Directory, cfg: dagger.File, modules: List[str]) -> dag.Golang:
return (
dag.golang(base, src)
.with_private_load(cfg, modules)
)
@func()
example(base: Container, src: Directory, cfg: File, modules: string[]): Golang {
return dag
.golang(base, src)
.withPrivateLoad(cfg, modules)
}
build() 🔗
Build a static binary from a Go project using the provided configuration. A directory is returned containing the built binary.
Return Type
Directory !
Arguments
Name | Type | Default Value | Description |
---|---|---|---|
main | String | - | the path to the main.go file of the project |
out | String | - | the name of the built binary |
os | String | - | the target operating system |
arch | String | - | the target architecture |
ldflags | [String ! ] | ["-s", "-w"] | flags to configure the linking during a build, by default sets flags for generating a release binary |
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH build
func (m *myModule) example(base *Container, src *Directory) *Directory {
return dag.
Golang(base, src).
Build()
}
@function
def example(base: dagger.Container, src: dagger.Directory) -> dagger.Directory:
return (
dag.golang(base, src)
.build()
)
@func()
example(base: Container, src: Directory): Directory {
return dag
.golang(base, src)
.build()
}
test() 🔗
Execute tests defined within the target project, ignores benchmarks by default
Return Type
String !
Arguments
Name | Type | Default Value | Description |
---|---|---|---|
short | Boolean | true | if only short running tests should be executed |
shuffle | Boolean | true | if the tests should be executed out of order |
run | String | - | run select tests only, defined using a regex |
skip | String | - | skip select tests, defined using a regex |
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH test
func (m *myModule) example(ctx context.Context, base *Container, src *Directory) string {
return dag.
Golang(base, src).
Test(ctx)
}
@function
async def example(base: dagger.Container, src: dagger.Directory) -> str:
return await (
dag.golang(base, src)
.test()
)
@func()
async example(base: Container, src: Directory): Promise<string> {
return dag
.golang(base, src)
.test()
}
bench() 🔗
Execute benchmarks defined within the target project, excludes all other tests
Return Type
String !
Arguments
Name | Type | Default Value | Description |
---|---|---|---|
memory | Boolean | true | print memory allocation statistics for benchmarks |
time | String | "5s" | the time.Duration each benchmark should run for |
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH bench
func (m *myModule) example(ctx context.Context, base *Container, src *Directory) string {
return dag.
Golang(base, src).
Bench(ctx)
}
@function
async def example(base: dagger.Container, src: dagger.Directory) -> str:
return await (
dag.golang(base, src)
.bench()
)
@func()
async example(base: Container, src: Directory): Promise<string> {
return dag
.golang(base, src)
.bench()
}
vulncheck() 🔗
Scans the target project for vulnerabilities using govulncheck
Return Type
String !
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH vulncheck
func (m *myModule) example(ctx context.Context, base *Container, src *Directory) string {
return dag.
Golang(base, src).
Vulncheck(ctx)
}
@function
async def example(base: dagger.Container, src: dagger.Directory) -> str:
return await (
dag.golang(base, src)
.vulncheck()
)
@func()
async example(base: Container, src: Directory): Promise<string> {
return dag
.golang(base, src)
.vulncheck()
}
lint() 🔗
Lint the target project using golangci-lint
Return Type
String !
Arguments
Name | Type | Default Value | Description |
---|---|---|---|
format | String | "colored-line-number" | the type of report that should be generated |
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH lint
func (m *myModule) example(ctx context.Context, base *Container, src *Directory) string {
return dag.
Golang(base, src).
Lint(ctx)
}
@function
async def example(base: dagger.Container, src: dagger.Directory) -> str:
return await (
dag.golang(base, src)
.lint()
)
@func()
async example(base: Container, src: Directory): Promise<string> {
return dag
.golang(base, src)
.lint()
}
format() 🔗
Format the source code within a target project using gofumpt. Formatted code must be copied back onto the host.`
Return Type
Directory !
Example
dagger -m github.com/purpleclay/daggerverse/golang@9f87a8c41f0edc2766602b75a5c52a681001d5b5 call \
--base IMAGE:TAG --src DIR_PATH format
func (m *myModule) example(base *Container, src *Directory) *Directory {
return dag.
Golang(base, src).
Format()
}
@function
def example(base: dagger.Container, src: dagger.Directory) -> dagger.Directory:
return (
dag.golang(base, src)
.format()
)
@func()
example(base: Container, src: Directory): Directory {
return dag
.golang(base, src)
.format()
}