Merge pull request #1600 from TomChv/europa/port-go-package

Port go package to universe
This commit is contained in:
Solomon Hykes 2022-02-18 19:34:24 -08:00 committed by GitHub
commit 97151b911d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 331 additions and 1 deletions

View File

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

View File

@ -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
}

View File

@ -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
}
}
}

View File

@ -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
}
}
}
},
]
}
}

View File

@ -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
}
}
}
}

View File

@ -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!"
}
}
}
}

View File

@ -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"]
}
}
}
}

View File

@ -0,0 +1,3 @@
module dagger.io/test
go 1.17

View File

@ -0,0 +1,7 @@
package greeting
import "fmt"
func Greeting(name string) string {
return fmt.Sprintf("Hi %s!", name)
}

View File

@ -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)
}
}

View File

@ -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))
}

View File

@ -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"
"""]
}
}
}
}
}

View File

@ -0,0 +1,10 @@
setup() {
load '../../bats_helpers'
common_setup
}
@test "bash" {
dagger up
}

View File

@ -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"
}
}