Skip to content

jasoet/fun-kommand

Repository files navigation

Simple command-line wrapper written in Kotlin

Build Status codecov JCenter

Execute command-line by spawning ProcessBuilder. Available on JCenter and Maven Central.

Features

  • Accept command as String or List<String>.
  • Accept File, String or InputStream as standard input.
  • Redirect standard output to File or OutputStream (including System.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

Gradle

Add Maven Central or JCenter repository

repositories {
    jcenter()
}

Add dependency

compile 'id.jasoet:fun-kommand:<version>'

Maven

Add dependency

<dependency>
  <groupId>id.jasoet</groupId>
  <artifactId>fun-kommand</artifactId>
  <version>[VERSION]</version>
  <type>pom</type>
</dependency>

Usage

Execute simple command

// 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

Accept File Input

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

Accept String Input

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)

Accept InputStream input

val inputStream = FileInputStream("/home/root/input-text.txt")
val result:String = "cat".executeToString(input = inputStream)

Redirect Output to File

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

Redirect Output to OutputStream and convert it to String

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())
}

Execute Command and return String

val result:String = "ls -alh".executeToString()

Pipe several command and return as String

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)
    }    

About

Simple Command Line Wrapper written in Kotlin

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages