From 299a38c6b12d76962e436685309ecae21a22a03e Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 6 Apr 2021 01:37:31 +0000 Subject: [PATCH] Simplify "dev dagger with dagger" example Signed-off-by: Solomon Hykes --- dev.cue | 71 ++++++++++++----------------------- docker.cue | 81 ++++++++++++++++++++++------------------ stdlib/docker/docker.cue | 2 + 3 files changed, 70 insertions(+), 84 deletions(-) diff --git a/dev.cue b/dev.cue index a5dc46f6..02f9ec66 100644 --- a/dev.cue +++ b/dev.cue @@ -1,8 +1,6 @@ package main import ( - "strings" - "dagger.io/dagger" "dagger.io/alpine" ) @@ -10,13 +8,12 @@ import ( // Dagger source code source: dagger.#Artifact -// Go environment -goenv: #Container & { - image: #ImageFromRef & { - ref: "docker.io/golang:1.16-alpine" - } +build: #Container & { + image: #ImageFromRef & {ref: "docker.io/golang:1.16-alpine"} - setup: "apk add --no-cache file" + setup: [ + "apk add --no-cache file", + ] volume: { daggerSource: { @@ -28,53 +25,31 @@ goenv: #Container & { dest: "/root/.cache/gocache" } } + + // Add go to search path (FIXME: should be inherited from image metadata) + shell: search: "/usr/local/go/bin": true + env: { GOMODCACHE: volume.goCache.dest CGO_ENABLED: "0" + } - let pathPrefixes = ["/", "/usr/", "/usr/local/", "/usr/local/go/"] - PATH: strings.Join([ - for prefix in pathPrefixes {prefix + "sbin"}, - for prefix in pathPrefixes {prefix + "bin"}, - ], ":") - } - command: { - debug: { - args: ["env"] - always: true - } - test: { - dir: "/src" - args: ["go", "test", "-v", "/src/..."] - } - build: { - dir: "/src" - args: ["go", "build", "-o", "/binaries/", "/src/cmd/..."] - outputDir: "/binaries" - } - } + dir: "/src" + outputDir: "/binaries" + command: """ + go test -v ./... + go build -o /binaries/ ./cmd/... + """ } -runner: #Container & { - image: alpine.#Image & { - package: make: true - } +usage: #Container & { + image: alpine.#Image - volume: daggerBinaries: { - from: goenv.command.build - dest: "/usr/local/dagger/bin" + volume: binaries: { + from: build + dest: "/usr/local/dagger/bin/" } - env: PATH: "/bin:/usr/bin:/usr/local/bin:/usr/local/dagger/bin" + shell: search: "/usr/local/dagger/bin": true - command: { - // Run `dagger help` - usage: args: ["dagger", "help"] - // FIXME: run integration tests - - // Just a debug command to check that this config works - debug: { - args: ["env"] - env: FOO: "BAR" - } - } + command: "dagger help" } diff --git a/docker.cue b/docker.cue index 424241d8..a33b8e69 100644 --- a/docker.cue +++ b/docker.cue @@ -1,6 +1,8 @@ package main import ( + "strings" + "dagger.io/dagger" "dagger.io/dagger/op" ) @@ -41,8 +43,8 @@ import ( image: dagger.#Artifact - // Optional setup script - setup: string | *null + // Optional setup scripts + setup: [...string] // Environment variables shared by all commands env: [string]: string @@ -62,36 +64,44 @@ import ( } } - command: [name=string]: { - args: [...string] - dir: string | *"/" - "env": env & { - [string]: string + shell: { + path: string | *"/bin/sh" + args: [...string] | *["-c"] + search: [string]: bool + search: { + "/sbin": true + "/bin": true + "/usr/sbin": true + "/usr/bin": true + "/usr/local/sbin": true + "/usr/local/bin": true } - outputDir: string | *"/" - always: true | *false + } + env: PATH: string | *strings.Join([ for p, v in shell.search if v {p}], ":") - // Execute each command in a pristine filesystem state - // (commands do not interfere with each other's changes) - #up: [ - op.#Load & {from: image}, - // Copy volumes with type=copy - for _, v in volume if v.type == "copy" { - op.#Copy & { - from: v.from - dest: v.dest - src: v.source - } - }, - // Execute setup script - if setup != null { - op.#Exec & { - "env": env - args: ["/bin/sh", "-c", setup] - } - }, + command: string + + dir: string | *"/" + + env: [string]: string + + outputDir: string | *"/" + always: true | *false + + #up: [ + op.#Load & {from: image}, + // Copy volumes with type=copy + for _, v in volume if v.type == "copy" { + op.#Copy & { + from: v.from + dest: v.dest + src: v.source + } + }, + // Execute setup commands, then main command + for cmd in setup + [command] { op.#Exec & { - "args": args + args: [shell.path] + shell.args + [cmd] "env": env "dir": dir "always": always @@ -109,11 +119,10 @@ import ( } } } - }, - op.#Subdir & { - dir: outputDir - }, - ] - } - + } + }, + op.#Subdir & { + dir: outputDir + }, + ] } diff --git a/stdlib/docker/docker.cue b/stdlib/docker/docker.cue index 6e3b94a1..d5cc450f 100644 --- a/stdlib/docker/docker.cue +++ b/stdlib/docker/docker.cue @@ -5,6 +5,8 @@ import ( "dagger.io/dagger/op" ) +#Image: dagger.#Artifact + #Ref: string // Build a docker container image