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 # generated by dagger
dagger.lock dagger.lock
alpha.dagger.io alpha.dagger.io
universe.dagger.io dagger.io
universe.dagger.io

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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