From 2018ac99f0b5ace67cabd9b5c05ffd4713ba9a36 Mon Sep 17 00:00:00 2001 From: Chris Hunt <114173+cdhunt@users.noreply.github.com> Date: Thu, 24 Mar 2022 20:42:27 -0400 Subject: [PATCH] Add PowerShell package (#1783) Signed-off-by: Chris Hunt <114173+cdhunt@users.noreply.github.com> --- .../powershell/powershell.cue | 54 ++++++++++++++ .../powershell/test/data/hello.ps1 | 1 + .../powershell/test/test.bats | 10 +++ .../powershell/test/test.cue | 71 +++++++++++++++++++ 4 files changed, 136 insertions(+) create mode 100644 pkg/universe.dagger.io/powershell/powershell.cue create mode 100644 pkg/universe.dagger.io/powershell/test/data/hello.ps1 create mode 100644 pkg/universe.dagger.io/powershell/test/test.bats create mode 100644 pkg/universe.dagger.io/powershell/test/test.cue diff --git a/pkg/universe.dagger.io/powershell/powershell.cue b/pkg/universe.dagger.io/powershell/powershell.cue new file mode 100644 index 00000000..3500478e --- /dev/null +++ b/pkg/universe.dagger.io/powershell/powershell.cue @@ -0,0 +1,54 @@ +// Helpers to run PowerShell commands in containers +package powershell + +import ( + "dagger.io/dagger" + "universe.dagger.io/docker" +) + +// Run a PowerShell (pwsh) script in a Docker container +// This does not suppore Windows containers or Windows PowerShell. +// Since this is a thin wrapper over docker.#Run, we embed it. +// Whether to embed or wrap is a case-by-case decision, like in Go. +#Run: { + // The script to execute + script: { + // A directory containing one or more PowerShell scripts + directory: dagger.#FS + + // Name of the file to execute + filename: string + + _directory: directory + _filename: filename + } | { + // Script contents + contents: string + + _filename: "run.ps1" + _write: dagger.#WriteFile & { + input: dagger.#Scratch + path: _filename + "contents": contents + } + _directory: _write.output + } + + // Arguments to the script + args: [...string] + + // Where in the container to mount the scripts directory + _mountpoint: "/powershell/scripts" + + docker.#Run & { + command: { + name: "pwsh" + "args": args + flags: "-File": "\(_mountpoint)/\(script._filename)" + } + mounts: "Pwsh scripts": { + contents: script._directory + dest: _mountpoint + } + } +} diff --git a/pkg/universe.dagger.io/powershell/test/data/hello.ps1 b/pkg/universe.dagger.io/powershell/test/data/hello.ps1 new file mode 100644 index 00000000..83ca942a --- /dev/null +++ b/pkg/universe.dagger.io/powershell/test/data/hello.ps1 @@ -0,0 +1 @@ +Set-Content -Value "Hello world!" -Path "/out.txt" \ No newline at end of file diff --git a/pkg/universe.dagger.io/powershell/test/test.bats b/pkg/universe.dagger.io/powershell/test/test.bats new file mode 100644 index 00000000..0038d992 --- /dev/null +++ b/pkg/universe.dagger.io/powershell/test/test.bats @@ -0,0 +1,10 @@ +setup() { + load '../../bats_helpers' + + common_setup +} + +@test "powershell" { + dagger "do" -p ./test.cue test +} + diff --git a/pkg/universe.dagger.io/powershell/test/test.cue b/pkg/universe.dagger.io/powershell/test/test.cue new file mode 100644 index 00000000..37ad0cba --- /dev/null +++ b/pkg/universe.dagger.io/powershell/test/test.cue @@ -0,0 +1,71 @@ +package powershell + +import ( + "dagger.io/dagger" + + "universe.dagger.io/docker" + "universe.dagger.io/powershell" +) + +dagger.#Plan & { + actions: test: { + + _pull: docker.#Pull & { + source: "mcr.microsoft.com/powershell" + } + _image: _pull.output + + // Run a script from source directory + filename + runFile: { + + dir: _load.output + _load: dagger.#Source & { + path: "./data" + include: ["*.ps1"] + } + + run: powershell.#Run & { + input: _image + export: files: "/out.txt": _ + script: { + directory: dir + filename: "hello.ps1" + } + } + output: run.export.files."/out.txt" & "Hello world!\n" + } + + // Run a script from string + runString: { + run: powershell.#Run & { + input: _image + export: files: "/output.txt": _ + script: contents: "Set-Content -Value 'Hello inline world!' -Path '/output.txt'" + } + output: run.export.files."/output.txt" & "Hello inline world!\n" + } + + // Test args from string + runStringArg: { + run: powershell.#Run & { + input: _image + export: files: "/output.txt": _ + script: contents: "Set-Content -Value 'Hello arg world!' -Path $($args[0])" + args: ["/output.txt"] + } + output: run.export.files."/output.txt" & "Hello arg world!\n" + } + + // Test 2 args from string + runString2Arg: { + run: powershell.#Run & { + input: _image + export: files: "/output.txt": _ + script: contents: "Set-Content -Value \"Hello args $($args[0])\" -Path $($args[1])" + args: ["world!", "/output.txt"] + } + output: run.export.files."/output.txt" & "Hello args world!\n" + } + + } +}