Apply Solomon comments on universe.dagger.io/go

- Improve #Container with a better docker.#Run integration
- Supports concurrency caching in #Container
- Simplify code maintainability and readability
- Simplify binary export in #Build
- Support multi binary building
- External #Version management

Signed-off-by: Vasek - Tom C <tom.chauveau@epitech.eu>
This commit is contained in:
Tom Chauveau 2022-02-17 15:56:19 +01:00 committed by Vasek - Tom C
parent e49bb34e87
commit 2887139bf7
No known key found for this signature in database
GPG Key ID: 175D82E572427960
8 changed files with 45 additions and 42 deletions

View File

@ -1,4 +1,5 @@
# generated by dagger
dagger.lock
alpha.dagger.io
universe.dagger.io
dagger.io
universe.dagger.io

View File

@ -2,14 +2,10 @@ package go
import (
"dagger.io/dagger"
"dagger.io/dagger/engine"
)
// Build a go binary
#Build: {
// Go version to use
version: *#Image.version | string
// Source code
source: dagger.#FS
@ -28,24 +24,27 @@ import (
// LDFLAGS to use for linking
ldflags: *"" | string
// Target binary output
output: string
env: [string]: string
_build: #Container & {
"version": version
"source": source
"env": env
args: ["build", "-v", "-tags", tags, "-ldflags", ldflags, "-o", output, package]
container: #Container & {
"source": source
"env": env
command: {
args: [package]
flags: {
build: true
"-v": true
"-tags": tags
"-ldflags": ldflags
"-o": "/output/"
}
}
}
_copy: engine.#Copy & {
input: engine.#Scratch
contents: _build.output.rootfs
source: output
dest: output
_binary: dagger.#Subdir & {
input: container.output.rootfs
path: "/output"
}
binary: _copy.output
binary: _binary.output
}

View File

@ -3,20 +3,21 @@ package go
import (
"dagger.io/dagger"
"dagger.io/dagger/engine"
"universe.dagger.io/docker"
)
// A standalone go environment to run go command
#Container: {
// Go version to use
version: *#Image.version | string
version: *#DefaultVersion | string
// Source code
source: dagger.#FS
// Arguments
args: [...string]
// Configure caching
cache: {
id: *"go_build" | string
}
// Use go image
_image: #Image & {
@ -29,18 +30,15 @@ import (
docker.#Run & {
input: _image.output
workdir: "/src"
command: {
name: "go"
"args": args
}
command: name: "go"
mounts: {
"source": {
dest: _sourcePath
contents: source
}
"go assets cache": {
contents: engine.#CacheDir & {
id: "\(_cachePath)_assets"
contents: dagger.#CacheDir & {
id: "\(cache.id)_assets"
}
dest: _cachePath
}

View File

@ -4,9 +4,12 @@ import (
"universe.dagger.io/docker"
)
// Go image default version
#DefaultVersion: "1.16"
// Build a go base image
#Image: {
version: *"1.16" | string
version: *#DefaultVersion | string
packages: [pkgName=string]: version: string | *""

View File

@ -6,6 +6,12 @@ package go
package: *"." | string
#Container & {
args: ["test", "-v", package]
command: {
args: [package]
flags: {
test: true
"-v": true
}
}
}
}

View File

@ -2,7 +2,6 @@ package go
import (
"dagger.io/dagger"
"dagger.io/dagger/engine"
"universe.dagger.io/go"
"universe.dagger.io/docker"
"universe.dagger.io/alpine"
@ -17,24 +16,23 @@ dagger.#Plan & {
simple: {
build: go.#Build & {
source: inputs.directories.testhello.contents
output: "/bin/hello"
}
exec: docker.#Run & {
input: _baseImage.output
command: {
name: "/bin/sh"
args: ["-c", "hello >> /output.txt"]
args: ["-c", "/bin/hello >> /output.txt"]
}
env: NAME: "dagger"
mounts: binary: {
dest: build.output
dest: "/bin/hello"
contents: build.binary
source: "/bin/hello"
source: "/test"
}
}
verify: engine.#ReadFile & {
verify: dagger.#ReadFile & {
input: exec.output.rootfs
path: "/output.txt"
} & {

View File

@ -2,17 +2,16 @@ package go
import (
"dagger.io/dagger"
"dagger.io/dagger/engine"
"universe.dagger.io/go"
)
dagger.#Plan & {
actions: tests: container: {
_source: engine.#Scratch & {}
_source: dagger.#Scratch & {}
simple: go.#Container & {
source: _source
args: ["version"]
command: args: ["version"]
}
}
}

View File

@ -2,14 +2,13 @@ package go
import (
"dagger.io/dagger"
"dagger.io/dagger/engine"
"universe.dagger.io/go"
"universe.dagger.io/docker"
)
dagger.#Plan & {
actions: tests: image: {
_source: engine.#Scratch & {}
_source: dagger.#Scratch & {}
simple: {
_image: go.#Image & {}