diff --git a/pkg/universe.dagger.io/cue.mod/pkg/.gitignore b/pkg/universe.dagger.io/cue.mod/pkg/.gitignore index 5089911a..331f139d 100644 --- a/pkg/universe.dagger.io/cue.mod/pkg/.gitignore +++ b/pkg/universe.dagger.io/cue.mod/pkg/.gitignore @@ -1,4 +1,5 @@ # generated by dagger dagger.lock alpha.dagger.io -universe.dagger.io +dagger.io +universe.dagger.io \ No newline at end of file diff --git a/pkg/universe.dagger.io/go/build.cue b/pkg/universe.dagger.io/go/build.cue new file mode 100644 index 00000000..5b4e6e03 --- /dev/null +++ b/pkg/universe.dagger.io/go/build.cue @@ -0,0 +1,50 @@ +package go + +import ( + "dagger.io/dagger" +) + +// Build a go binary +#Build: { + // Source code + source: dagger.#FS + + // Target package to build + package: *"." | string + + // Target architecture + arch: *"amd64" | string + + // Target OS + os: *"linux" | string + + // Build tags to use for building + tags: *"" | string + + // LDFLAGS to use for linking + ldflags: *"" | string + + env: [string]: string + + container: #Container & { + "source": source + "env": { + env + GOOS: os + GOARCH: arch + } + command: { + args: [package] + flags: { + build: true + "-v": true + "-tags": tags + "-ldflags": ldflags + "-o": "/output/" + } + } + export: directories: "/output/": _ + } + + binary: container.export.directories."/output/".contents +} diff --git a/pkg/universe.dagger.io/go/container.cue b/pkg/universe.dagger.io/go/container.cue new file mode 100644 index 00000000..81e00f28 --- /dev/null +++ b/pkg/universe.dagger.io/go/container.cue @@ -0,0 +1,44 @@ +// Go operation +package go + +import ( + "dagger.io/dagger" + "universe.dagger.io/docker" +) + +// A standalone go environment to run go command +#Container: { + // Container app name + name: *"go_builder" | string + + // Source code + source: dagger.#FS + + // Use go image + _image: #Image + + _sourcePath: "/src" + _cachePath: "/root/.cache/gocache" + + docker.#Run & { + input: *_image.output | docker.#Image + workdir: "/src" + command: name: "go" + mounts: { + "source": { + dest: _sourcePath + contents: source + } + "go assets cache": { + contents: dagger.#CacheDir & { + id: "\(name)_assets" + } + dest: _cachePath + } + } + env: { + CGO_ENABLED: "0" + GOMODCACHE: _cachePath + } + } +} diff --git a/pkg/universe.dagger.io/go/image.cue b/pkg/universe.dagger.io/go/image.cue new file mode 100644 index 00000000..9edbd2da --- /dev/null +++ b/pkg/universe.dagger.io/go/image.cue @@ -0,0 +1,37 @@ +package go + +import ( + "universe.dagger.io/docker" +) + +// Go image default version +#DefaultVersion: "1.16" + +// Build a go base image +#Image: { + version: *#DefaultVersion | string + + packages: [pkgName=string]: version: string | *"" + + // FIXME Basically a copy of alpine.#Build with a different image + // Should we create a special definition? + docker.#Build & { + steps: [ + docker.#Pull & { + source: "index.docker.io/golang:\(version)-alpine" + }, + for pkgName, pkg in packages { + docker.#Run & { + command: { + name: "apk" + args: ["add", "\(pkgName)\(pkg.version)"] + flags: { + "-U": true + "--no-cache": true + } + } + } + }, + ] + } +} diff --git a/pkg/universe.dagger.io/go/test.cue b/pkg/universe.dagger.io/go/test.cue new file mode 100644 index 00000000..5de2b94b --- /dev/null +++ b/pkg/universe.dagger.io/go/test.cue @@ -0,0 +1,17 @@ +package go + +// Test a go package +#Test: { + // Package to test + package: *"." | string + + #Container & { + command: { + args: [package] + flags: { + test: true + "-v": true + } + } + } +} diff --git a/pkg/universe.dagger.io/go/test/build.cue b/pkg/universe.dagger.io/go/test/build.cue new file mode 100644 index 00000000..8f8dba2c --- /dev/null +++ b/pkg/universe.dagger.io/go/test/build.cue @@ -0,0 +1,43 @@ +package go + +import ( + "dagger.io/dagger" + "universe.dagger.io/go" + "universe.dagger.io/docker" + "universe.dagger.io/alpine" +) + +dagger.#Plan & { + inputs: directories: testhello: path: "./data/hello" + + actions: tests: build: { + _baseImage: alpine.#Build + + simple: { + build: go.#Build & { + source: inputs.directories.testhello.contents + } + + exec: docker.#Run & { + input: _baseImage.output + command: { + name: "/bin/sh" + args: ["-c", "/bin/hello >> /output.txt"] + } + env: NAME: "dagger" + mounts: binary: { + dest: "/bin/hello" + contents: build.binary + source: "/test" + } + } + + verify: dagger.#ReadFile & { + input: exec.output.rootfs + path: "/output.txt" + } & { + contents: "Hi dagger!" + } + } + } +} diff --git a/pkg/universe.dagger.io/go/test/container.cue b/pkg/universe.dagger.io/go/test/container.cue new file mode 100644 index 00000000..ac7056c7 --- /dev/null +++ b/pkg/universe.dagger.io/go/test/container.cue @@ -0,0 +1,30 @@ +package go + +import ( + "dagger.io/dagger" + "universe.dagger.io/go" + "universe.dagger.io/alpine" +) + +dagger.#Plan & { + actions: tests: container: { + _source: dagger.#Scratch & {} + + simple: go.#Container & { + source: _source + command: args: ["version"] + } + + overide: { + base: alpine.#Build & { + packages: go: _ + } + + command: go.#Container & { + input: base.output + source: _source + command: args: ["version"] + } + } + } +} diff --git a/pkg/universe.dagger.io/go/test/data/hello/go.mod b/pkg/universe.dagger.io/go/test/data/hello/go.mod new file mode 100644 index 00000000..496135e4 --- /dev/null +++ b/pkg/universe.dagger.io/go/test/data/hello/go.mod @@ -0,0 +1,3 @@ +module dagger.io/test + +go 1.17 diff --git a/pkg/universe.dagger.io/go/test/data/hello/greeting/greeting.go b/pkg/universe.dagger.io/go/test/data/hello/greeting/greeting.go new file mode 100644 index 00000000..b830aabf --- /dev/null +++ b/pkg/universe.dagger.io/go/test/data/hello/greeting/greeting.go @@ -0,0 +1,7 @@ +package greeting + +import "fmt" + +func Greeting(name string) string { + return fmt.Sprintf("Hi %s!", name) +} diff --git a/pkg/universe.dagger.io/go/test/data/hello/greeting/greeting_test.go b/pkg/universe.dagger.io/go/test/data/hello/greeting/greeting_test.go new file mode 100644 index 00000000..f2f2d812 --- /dev/null +++ b/pkg/universe.dagger.io/go/test/data/hello/greeting/greeting_test.go @@ -0,0 +1,13 @@ +package greeting + +import "testing" + +func TestGreeting(t *testing.T) { + name := "Dagger Test" + expect := "Hi Dagger Test!" + value := Greeting(name) + + if expect != value { + t.Fatalf("Hello(%s) = '%s', expected '%s'", name, value, expect) + } +} diff --git a/pkg/universe.dagger.io/go/test/data/hello/main.go b/pkg/universe.dagger.io/go/test/data/hello/main.go new file mode 100644 index 00000000..262cf293 --- /dev/null +++ b/pkg/universe.dagger.io/go/test/data/hello/main.go @@ -0,0 +1,16 @@ +package main + +import ( + "fmt" + "os" + + "dagger.io/test/greeting" +) + +func main() { + name := os.Getenv("NAME") + if name == "" { + name = "John Doe" + } + fmt.Printf(greeting.Greeting(name)) +} diff --git a/pkg/universe.dagger.io/go/test/image.cue b/pkg/universe.dagger.io/go/test/image.cue new file mode 100644 index 00000000..5a7ecbbb --- /dev/null +++ b/pkg/universe.dagger.io/go/test/image.cue @@ -0,0 +1,44 @@ +package go + +import ( + "dagger.io/dagger" + "universe.dagger.io/go" + "universe.dagger.io/docker" +) + +dagger.#Plan & { + actions: tests: image: { + _source: dagger.#Scratch & {} + + simple: { + _image: go.#Image & {} + + verify: docker.#Run & { + input: _image.output + command: { + name: "/bin/sh" + args: ["-c", """ + go version | grep "1.16" + """] + } + } + } + + custom: { + _image: go.#Image & { + version: "1.17" + packages: bash: _ + } + + verify: docker.#Run & { + input: _image.output + command: { + name: "/bin/bash" + args: ["-c", """ + go version | grep "1.17" + """] + } + } + } + } +} diff --git a/pkg/universe.dagger.io/go/test/test.bats b/pkg/universe.dagger.io/go/test/test.bats new file mode 100644 index 00000000..52dc4dc7 --- /dev/null +++ b/pkg/universe.dagger.io/go/test/test.bats @@ -0,0 +1,10 @@ +setup() { + load '../../bats_helpers' + + common_setup +} + +@test "bash" { + dagger up +} + diff --git a/pkg/universe.dagger.io/go/test/test.cue b/pkg/universe.dagger.io/go/test/test.cue new file mode 100644 index 00000000..e37c32e4 --- /dev/null +++ b/pkg/universe.dagger.io/go/test/test.cue @@ -0,0 +1,15 @@ +package go + +import ( + "dagger.io/dagger" + "universe.dagger.io/go" +) + +dagger.#Plan & { + inputs: directories: testhello: path: "./data/hello" + + actions: tests: test: simple: go.#Test & { + source: inputs.directories.testhello.contents + package: "./greeting" + } +}