Execute command-line by spawning ProcessBuilder. Available on JCenter and Maven Central.
- Accept command as
String
orList<String>
. - Accept
File
,String
orInputStream
as standard input. - Redirect standard output to
File
orOutputStream
(includingSystem.out
). - Accept
Map
as environment variable. - Helper for accept standard input from main Java process as
Sequence<String>
. - Return
BufferedInputStream
if output is not redirected, null if otherwise. InputStream
from a command will be easily to be piped (as input) to other command.- Pipe command with other command
repositories {
jcenter()
}
compile 'id.jasoet:fun-kommand:<version>'
<dependency>
<groupId>id.jasoet</groupId>
<artifactId>fun-kommand</artifactId>
<version>[VERSION]</version>
<type>pom</type>
</dependency>
// Will throw IOException if command return non zero
val result:BufferedInputStream? = listOf("ls", "-alh").execute()
// Wrap command inside Try<BufferedInputStream?> monad
val result:Try<BufferedInputStream?> = "ls -alh".tryExecute()
// Execute command and redirect output to standard out
val result:BufferedInputStream? = "ls -alh".execute(output = System.out)
// Result will always be null if output is defined
val file = File("/var/log/filename.ext")
val result = "tail -f".execute(input = file, output = System.out)
// Result will always be null if output is defined
const val stringInput = """
Lorem Ipsum is simply dummy text of the printing and typesetting industry.
Lorem Ipsum has been the industry's standard dummy text ever since the 1500s,
when an unknown printer took a galley of type and scrambled it to make a type specimen book.
"""
val result:String = "cat".executeToString(input = stringInput)
val inputStream = FileInputStream("/home/root/input-text.txt")
val result:String = "cat".executeToString(input = inputStream)
val outputFile = Paths.get(tmpDir, UUID.randomUUID().toString()).toFile()
val result = "ls -alh".execute(output = outputFile)
// Result will always be null if output is defined
val byteOutputStream = ByteArrayOutputStream()
val result = "ls -alh".execute(output = byteOutputStream)
// Result will always be null if output is defined
val stringResult = byteOutputStream.use {
it.toString(Charsets.UTF_8.name())
}
val result:String = "ls -alh".executeToString()
val result = "cat".execute(input = inputFile)
.pipe("echo")
.pipe("wc")
.toString()
val result = "cat".execute(input = inputFile)
.pipe {
"echo".execute(input = it)
}
.pipe {
"wc".execute(it)
}