fun-kommand

Simple command-line wrapper for Kotlin

License

License

GroupId

GroupId

id.jasoet
ArtifactId

ArtifactId

fun-kommand
Last Version

Last Version

1.1.0
Release Date

Release Date

Type

Type

jar
Description

Description

fun-kommand
Simple command-line wrapper for Kotlin
Project URL

Project URL

https://github.com/jasoet/fun-kommand
Source Code Management

Source Code Management

https://github.com/jasoet/fun-kommand

Download fun-kommand

How to add to project

<!-- https://jarcasting.com/artifacts/id.jasoet/fun-kommand/ -->
<dependency>
    <groupId>id.jasoet</groupId>
    <artifactId>fun-kommand</artifactId>
    <version>1.1.0</version>
</dependency>
// https://jarcasting.com/artifacts/id.jasoet/fun-kommand/
implementation 'id.jasoet:fun-kommand:1.1.0'
// https://jarcasting.com/artifacts/id.jasoet/fun-kommand/
implementation ("id.jasoet:fun-kommand:1.1.0")
'id.jasoet:fun-kommand:jar:1.1.0'
<dependency org="id.jasoet" name="fun-kommand" rev="1.1.0">
  <artifact name="fun-kommand" type="jar" />
</dependency>
@Grapes(
@Grab(group='id.jasoet', module='fun-kommand', version='1.1.0')
)
libraryDependencies += "id.jasoet" % "fun-kommand" % "1.1.0"
[id.jasoet/fun-kommand "1.1.0"]

Dependencies

compile (7)

Group / Artifact Type Version
org.jetbrains.kotlin : kotlin-reflect jar 1.3.0
org.jetbrains.kotlin : kotlin-stdlib-jdk8 jar 1.3.0
org.jetbrains.kotlinx : kotlinx-coroutines-core jar 1.0.0
commons-io : commons-io jar 2.6
ch.qos.logback : logback-core jar 1.2.3
ch.qos.logback : logback-classic jar 1.2.3
io.arrow-kt : arrow-core jar 0.7.2

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.1.0
1.0.5