From 8594c13811b66de03d08f4757c9a6e320de148a7 Mon Sep 17 00:00:00 2001 From: Kotaro Suto Date: Fri, 3 Nov 2023 17:12:02 -0700 Subject: [PATCH] Add cd() (#13) * Add cd() * Fixes Examples/cd.swift * Change to public --- Examples/cd.swift | 13 +++++++++++++ Scripts/ExamplesTest.sh | 4 +++- Sources/ScriptSwift/ExeternalCommands.swift | 12 ++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 Examples/cd.swift create mode 100644 Sources/ScriptSwift/ExeternalCommands.swift diff --git a/Examples/cd.swift b/Examples/cd.swift new file mode 100644 index 0000000..adbfc55 --- /dev/null +++ b/Examples/cd.swift @@ -0,0 +1,13 @@ +#!/usr/bin/swift sh + +import ScriptSwift // KS1019/Script.swift ~> main + +let currentDir = Script().exec("pwd").asString() + +let d = Script() + .cd(to: "../") + .cd(to: currentDir) + .exec("pwd") + .match(currentDir) + .exec("echo 'Test Success'") + .stdout() diff --git a/Scripts/ExamplesTest.sh b/Scripts/ExamplesTest.sh index 7682092..2696d1a 100644 --- a/Scripts/ExamplesTest.sh +++ b/Scripts/ExamplesTest.sh @@ -72,4 +72,6 @@ html=" try "$html" "../Examples/curl.swift" -try "Received input: hh. Test is working. Yay." "../Examples/stdin.swift" "hh" \ No newline at end of file +try "Received input: hh. Test is working. Yay." "../Examples/stdin.swift" "hh" + +try "Test Success" "../Examples/cd.swift" diff --git a/Sources/ScriptSwift/ExeternalCommands.swift b/Sources/ScriptSwift/ExeternalCommands.swift new file mode 100644 index 0000000..fc143cd --- /dev/null +++ b/Sources/ScriptSwift/ExeternalCommands.swift @@ -0,0 +1,12 @@ +import ShellOut + +public extension Script { + func cd(to path: String) -> Script { + do { + try shellOut(to: "cd " + path) + return self + } catch { + return .init(failure: error) + } + } +}