From 0e32bc68f830fe939c61a16321061535bbfe7794 Mon Sep 17 00:00:00 2001 From: Tony Worm Date: Mon, 15 Mar 2021 08:23:24 -0400 Subject: [PATCH] add file ops Signed-off-by: Tony Worm --- stdlib/file/file.cue | 72 ++++++++++++++++++++ tests/stdlib/file/file.cue | 131 +++++++++++++++++++++++++++++++++++++ tests/test.sh | 2 + 3 files changed, 205 insertions(+) create mode 100644 stdlib/file/file.cue create mode 100644 tests/stdlib/file/file.cue diff --git a/stdlib/file/file.cue b/stdlib/file/file.cue new file mode 100644 index 00000000..05c556f8 --- /dev/null +++ b/stdlib/file/file.cue @@ -0,0 +1,72 @@ +package file + +import ( + "strings" + "dagger.io/dagger" +) + +#Create: { + filename: !="" + permissions: int | *0o644 + contents: string | bytes + + #compute: [ + dagger.#WriteFile & { dest: filename, content: contents, mode: permissions} + ] +} + +#Append: { + filename: !="" + permissions: int | *0o644 + contents: string | bytes + from: dagger.#Artifact + + orig: (#read & { path: filename, "from": from }).data + + #compute: [ + dagger.#WriteFile & { dest: filename, content: "\(orig)\(contents)", mode: permissions} + ] +} + +#Read: { + filename: !="" + from: dagger.#Artifact + contents: (#read & { path: filename, "from": from }).data +} + +#read: { + path: !="" + from: dagger.#Artifact + data: { + string + #compute: [ + dagger.#Load & {"from": from}, + dagger.#Export & { source: path } + ] + } +} + +#Glob: { + glob: !="" + filenames: [...string] + from: dagger.#Artifact + files: (_#glob & { "glob": glob, "from": from }).data + // trim suffix because ls always ends with newline + filenames: strings.Split(strings.TrimSuffix(files, "\n"), "\n") +} + +_#glob: { + glob: !="" + from: dagger.#Artifact + data: { + string + _tmppath: "/tmp/ls.out" + #compute: [ + dagger.#Load & {"from": from}, + dagger.#Exec & { + args: ["sh", "-c", "ls \(glob) > \(_tmppath)"] + }, + dagger.#Export & { source: _tmppath } + ] + } +} diff --git a/tests/stdlib/file/file.cue b/tests/stdlib/file/file.cue new file mode 100644 index 00000000..2358e5ff --- /dev/null +++ b/tests/stdlib/file/file.cue @@ -0,0 +1,131 @@ +package f + +import ( + "dagger.io/dagger" + "dagger.io/alpine" + "dagger.io/file" +) + +TestCreate: { + _content: "hello world" + + write: file.#Create & { + filename: "/file.txt" + contents: _content + } + + test: #compute: [ + dagger.#Load & {from: alpine.#Image}, + dagger.#Exec & { + args: [ + "sh", + "-ec", + """ + test "$(cat /file.txt)" = "hello world" + """, + ] + mount: "/file.txt": { + from: write + path: "/file.txt" + } + }, + ] +} + +TestRead: { + read: file.#Read & { + filename: "/etc/alpine-release" + from: alpine.#Image & { version: "3.10.6" } + } + test: #compute: [ + dagger.#Load & {from: alpine.#Image}, + dagger.#Exec & { + args: [ + "sh", + "-ec", + """ + test "\(read.contents)" = "3.10.6\n" + """, + ] + }, + ] +} + +TestRead2: { + write: file.#Create & { + _content: "hello world" + filename: "/file.txt" + contents: _content + } + + read: file.#Read & { + filename: "/file.txt" + from: write + } + + test: #compute: [ + dagger.#Load & {from: alpine.#Image}, + dagger.#Exec & { + args: [ + "sh", + "-ec", + """ + test "\(read.contents)" = "hello world" + """, + ] + }, + ] +} + +TestAppend: { + content1: "hello world" + content2: "foo bar" + + write: file.#Create & { + filename: "/file.txt" + contents: content1 + } + append: file.#Append & { + filename: "/file.txt" + contents: content2 + from: write + } + + orig: append.orig + + read: file.#Read & { + filename: "/file.txt" + from: append + } + + new: read.contents + + test: new & "hello worldfoo bar" + + //test: #compute: [ + //dagger.#Load & {from: alpine.#Image}, + //dagger.#Exec & { + //args: [ + //"sh", + //"-ec", + //""" + //test "$(cat /file.txt)" = "hello worldfoo bar" + //""", + //] + //mount: "/file.txt": { + //from: append + //path: "/file.txt" + //} + //}, + //] +} + +TestGlob: { + list: file.#Glob & { + glob: "/etc/r*" + from: alpine.#Image + } + test: list.filenames & ["/etc/resolv.conf"] +} + + diff --git a/tests/test.sh b/tests/test.sh index 8a156e54..35a56593 100755 --- a/tests/test.sh +++ b/tests/test.sh @@ -27,6 +27,8 @@ test::stdlib() { "$dagger" "${DAGGER_BINARY_ARGS[@]}" compute "$d"/stdlib/yarn --input-dir TestData="$d"/stdlib/yarn/testdata test::one "stdlib: go" \ "$dagger" "${DAGGER_BINARY_ARGS[@]}" compute "$d"/stdlib/go --input-dir TestData="$d"/stdlib/go/testdata + test::one "stdlib: file" \ + "$dagger" "${DAGGER_BINARY_ARGS[@]}" compute "$d"/stdlib/file } test::compute(){