2021-11-04 08:05:24 +01:00
|
|
|
package docker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"list"
|
2022-01-27 20:15:32 +01:00
|
|
|
"strings"
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
"dagger.io/dagger"
|
2022-01-07 05:53:29 +01:00
|
|
|
"dagger.io/dagger/engine"
|
2021-11-04 08:05:24 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// Run a command in a container
|
|
|
|
#Run: {
|
2022-01-15 21:33:50 +01:00
|
|
|
_image: #Image
|
|
|
|
|
|
|
|
{
|
|
|
|
image: #Image
|
|
|
|
_image: image
|
|
|
|
} | {
|
|
|
|
// For compatibility with #Build
|
|
|
|
input: #Image
|
|
|
|
_image: input
|
|
|
|
}
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
always: bool | *false
|
|
|
|
|
|
|
|
// Filesystem mounts
|
|
|
|
mounts: [name=string]: engine.#Mount
|
|
|
|
|
|
|
|
// Expose network ports
|
2021-12-14 23:33:11 +01:00
|
|
|
// FIXME: investigate feasibility
|
2021-11-04 08:05:24 +01:00
|
|
|
ports: [name=string]: {
|
|
|
|
frontend: dagger.#Service
|
|
|
|
backend: {
|
|
|
|
protocol: *"tcp" | "udp"
|
|
|
|
address: string
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command to execute
|
2022-01-27 20:15:32 +01:00
|
|
|
cmd?: {
|
2021-11-04 08:05:24 +01:00
|
|
|
// Name of the command to execute
|
|
|
|
// Examples: "ls", "/bin/bash"
|
|
|
|
name: string
|
|
|
|
|
|
|
|
// Positional arguments to the command
|
|
|
|
// Examples: ["/tmp"]
|
|
|
|
args: [...string]
|
|
|
|
|
|
|
|
// Command-line flags represented in a civilized form
|
|
|
|
// Example: {"-l": true, "-c": "echo hello world"}
|
|
|
|
flags: [string]: (string | true)
|
|
|
|
|
|
|
|
_flatFlags: list.FlattenN([
|
|
|
|
for k, v in flags {
|
|
|
|
if (v & bool) != _|_ {
|
|
|
|
[k]
|
|
|
|
}
|
|
|
|
if (v & string) != _|_ {
|
|
|
|
[k, v]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
], 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optionally pass a script to interpret
|
|
|
|
// Example: "echo hello\necho world"
|
|
|
|
script?: string
|
|
|
|
if script != _|_ {
|
|
|
|
// Default interpreter is /bin/sh -c
|
|
|
|
cmd: *{
|
|
|
|
name: "/bin/sh"
|
|
|
|
flags: "-c": script
|
|
|
|
} | {}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Environment variables
|
|
|
|
// Example: {"DEBUG": "1"}
|
|
|
|
env: [string]: string
|
|
|
|
|
|
|
|
// Working directory for the command
|
|
|
|
// Example: "/src"
|
2022-01-27 21:21:08 +01:00
|
|
|
workdir: string
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
// Username or UID to ad
|
|
|
|
// User identity for this command
|
|
|
|
// Examples: "root", "0", "1002"
|
2022-01-27 20:15:32 +01:00
|
|
|
user: string
|
2021-11-04 08:05:24 +01:00
|
|
|
|
|
|
|
// Output fields
|
|
|
|
{
|
|
|
|
// Has the command completed?
|
|
|
|
completed: bool & (_exec.exit != _|_)
|
|
|
|
|
|
|
|
// Was completion successful?
|
|
|
|
success: bool & (_exec.exit == 0)
|
|
|
|
|
|
|
|
// Details on error, if any
|
|
|
|
error: {
|
|
|
|
// Error code
|
|
|
|
code: _exec.exit
|
|
|
|
|
|
|
|
// Error message
|
|
|
|
message: string | *null
|
|
|
|
}
|
|
|
|
|
2022-01-15 21:33:50 +01:00
|
|
|
export: {
|
|
|
|
rootfs: dagger.#FS & _exec.output
|
2021-11-04 08:05:24 +01:00
|
|
|
files: [path=string]: {
|
2022-01-15 21:33:50 +01:00
|
|
|
contents: string & _read.contents
|
|
|
|
_read: engine.#ReadFile & {
|
2021-11-04 08:05:24 +01:00
|
|
|
input: _exec.output
|
|
|
|
"path": path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
directories: [path=string]: {
|
2022-01-15 21:33:50 +01:00
|
|
|
contents: dagger.#FS & _subdir.output
|
|
|
|
_subdir: dagger.#Subdir & {
|
2021-11-04 08:05:24 +01:00
|
|
|
input: _exec.output
|
|
|
|
"path": path
|
2022-01-15 21:33:50 +01:00
|
|
|
}
|
2021-11-04 08:05:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 21:33:50 +01:00
|
|
|
// For compatibility with #Build
|
|
|
|
output: #Image & {
|
|
|
|
rootfs: _exec.output
|
|
|
|
config: _image.config
|
|
|
|
}
|
|
|
|
|
2021-11-04 08:05:24 +01:00
|
|
|
// Actually execute the command
|
|
|
|
_exec: engine.#Exec & {
|
2022-01-27 20:15:32 +01:00
|
|
|
input: _image.rootfs
|
|
|
|
"always": always
|
|
|
|
"mounts": mounts
|
|
|
|
|
|
|
|
if cmd != _|_ {
|
|
|
|
args: [cmd.name] + cmd._flatFlags + cmd.args
|
|
|
|
}
|
|
|
|
if cmd == _|_ {
|
|
|
|
args: list.Concat([
|
|
|
|
if _image.config.Entrypoint != _|_ {
|
|
|
|
_image.config.Entrypoint
|
|
|
|
},
|
|
|
|
if _image.config.Cmd != _|_ {
|
|
|
|
_image.config.Cmd
|
|
|
|
},
|
|
|
|
])
|
|
|
|
}
|
|
|
|
"env": env
|
|
|
|
if _image.config.Env != _|_ {
|
|
|
|
for _, envvar in _image.config.Env {
|
|
|
|
let split = strings.SplitN(envvar, "=", 2)
|
|
|
|
let k = split[0]
|
|
|
|
let v = split[1]
|
|
|
|
if env[k] == _|_ {
|
2022-01-27 21:27:28 +01:00
|
|
|
env: "\(k)": v
|
2022-01-27 20:15:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 08:05:24 +01:00
|
|
|
"workdir": workdir
|
2022-01-27 20:15:32 +01:00
|
|
|
if workdir == _|_ && _image.config.WorkingDir != _|_ {
|
2022-01-27 21:27:28 +01:00
|
|
|
workdir: _image.config.WorkingDir
|
2022-01-27 20:15:32 +01:00
|
|
|
}
|
|
|
|
"user": user
|
|
|
|
if user == _|_ && _image.config.User != _|_ {
|
2022-01-27 21:27:28 +01:00
|
|
|
user: _image.config.User
|
2022-01-27 20:15:32 +01:00
|
|
|
}
|
2021-11-04 08:05:24 +01:00
|
|
|
}
|
|
|
|
}
|