-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
760c628
commit 4ec4583
Showing
7 changed files
with
164 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
version=0.17.1 | ||
version=0.18.0 | ||
group=io.cloudshiftdev.kprocess | ||
|
||
org.gradle.vfs.watch=true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/main/kotlin/io/cloudshiftdev/kprocess/session/ShellSession.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package io.cloudshiftdev.kprocess.session | ||
|
||
import io.cloudshiftdev.kprocess.ExecResult | ||
import io.cloudshiftdev.kprocess.ExecSpec | ||
import java.io.File | ||
|
||
public interface ShellSession { | ||
public suspend fun changeDirectory(dir: File) | ||
|
||
public suspend fun cd(dir: File): Unit = changeDirectory(dir) | ||
|
||
public suspend fun cd(dir: String): Unit = changeDirectory(File(dir)) | ||
|
||
public suspend fun currentDirectory(): File | ||
|
||
public suspend fun cwd(): File = currentDirectory() | ||
|
||
public suspend fun pwd(): File = currentDirectory() | ||
|
||
public suspend fun mkdir(vararg args: String) | ||
|
||
public suspend fun chmod(vararg args: String) | ||
|
||
public suspend fun <O> exec( | ||
vararg commandAndArgs: String, | ||
spec: (ExecSpec<O>.() -> Unit) = {} | ||
): ExecResult<O> | ||
} | ||
|
||
public suspend fun shellSession( | ||
workingDir: File? = null, | ||
environment: Map<String, String> = emptyMap(), | ||
session: suspend ShellSession.() -> Unit | ||
) { | ||
ShellSessionImpl(workingDir, environment).session() | ||
} | ||
|
||
private class ShellSessionImpl(initialWorkingDir: File?, environment: Map<String, String>) : | ||
ShellSession { | ||
private var workingDir: File = initialWorkingDir ?: File(System.getProperty("user.dir")) | ||
private val environment: MutableMap<String, String> = environment.toMutableMap() | ||
|
||
override suspend fun changeDirectory(dir: File) { | ||
workingDir = resolveDir(dir) | ||
} | ||
|
||
override suspend fun currentDirectory(): File = workingDir | ||
|
||
override suspend fun mkdir(vararg args: String) { | ||
exec<Unit>("mkdir", *args) { failOnNonZeroExit(false) } | ||
} | ||
|
||
override suspend fun chmod(vararg args: String) { | ||
exec<Unit>("chmod", *args) | ||
} | ||
|
||
override suspend fun <O> exec( | ||
vararg commandAndArgs: String, | ||
spec: ExecSpec<O>.() -> Unit | ||
): ExecResult<O> { | ||
return io.cloudshiftdev.kprocess.exec(*commandAndArgs) { | ||
workingDir(workingDir) | ||
environment(environment) | ||
apply(spec) | ||
} | ||
} | ||
|
||
private fun resolveDir(dir: File): File = | ||
if (dir.isAbsolute) dir else workingDir.resolve(dir).normalize() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package io.cloudshiftdev.kprocess.session | ||
|
||
public suspend fun ShellSession.zip(vararg args: String) { | ||
exec<Unit>("zip", *args) | ||
} | ||
|
||
public suspend fun ShellSession.unzip(vararg args: String) { | ||
exec<Unit>("unzip", *args) | ||
} |
19 changes: 19 additions & 0 deletions
19
src/test/kotlin/io/cloudshiftdev/kprocess/session/ShellSessionImplTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package io.cloudshiftdev.kprocess.session | ||
|
||
import io.kotest.core.spec.style.FunSpec | ||
import io.kotest.engine.spec.tempdir | ||
import org.junit.jupiter.api.Assertions.* | ||
|
||
class ShellSessionImplTest : FunSpec() { | ||
init { | ||
test("shell session works") { | ||
val tmpDir = tempdir() | ||
shellSession(workingDir = tmpDir) { | ||
mkdir("test") | ||
cd("test") | ||
val dir = pwd() | ||
assertEquals(tmpDir.resolve("test"), dir) | ||
} | ||
} | ||
} | ||
} |