Code Monkey home page Code Monkey logo

cmdtool's Introduction

CmdTool

Continuous Integration

Build Status codecov Maven Central

Quick Overview

Tiny, pure object-oriented, declarative and immutable wrapper of zt-exec with additional features around a process execution.

Java 8+ required.

Motivation

When we call external programs from Java, we certainly need to harvest the output files and output stream. It is ok, but what if we have thousands of calls? They will pollute a disk space if some of them produce files we don't need. So, we have to do a clean up of disk space if the files don't need anymore, just like Java GC frees a RAM automatically.

To be honest, initial motivation was selfish. I wanted to practice the design principles behind Cactoos. And as a result, it grew into the tiny project here.

Features

All features of zt-exec are supported plus the following:

  • Execute command or script
  • Save output stream of the process into a file
  • Create and clean up work directory automatically

Download

Include the dependency into your pom.xml

<dependency>
  <groupId>io.github.alekseysotnikov</groupId>
  <artifactId>CmdTool</artifactId>
  <version>1.0.1</version>
</dependency>

Or

  1. Get the latest version here with or without dependencies
  2. Project uses the following dependencies
<dependency>
  <groupId>org.zeroturnaround</groupId>
  <artifactId>zt-exec</artifactId>
  <version>1.10</version>
</dependency>

<dependency>
  <groupId>org.cactoos</groupId>
  <artifactId>cactoos</artifactId>
  <version>0.30</version>
</dependency>

Examples

Execute command

new Cmd().command("echo", "Hello").execute();

Execute script in a Shell (Unix-like, Mac OS)

new Cmd()
       .interpreter("sh") // specify command interpreter
       .command("-c", "s='Hello'; echo $s;")
       .execute();

or even shorter

new Cmd().command("sh", "-c", "s='Hello'; echo $s;").execute();

... and read output

String output = new Cmd()
                     .configuring(c -> c.readOutput(true)) // configure zt-exec's executor
                     .command("sh", "-c", "s='Hello'; echo $s;")
                     .execute()
                     .outputUTF8();
System.out.println(output);

// output> Hello

Save an output stream into a file, even if the process stopped unexpectedly

new Cmd()
      .configuring(
              RedirectToFile.fromOutputStream("./output.txt"),
              RedirectToFile.fromErrorStream("./errOutput.txt"))
      .command("echo", "Hello")
      .execute();

Execute command within custom work directory

new Cmd()
        .configuring(
                new WorkDir("./foo"), // specify work directory ./foo (will be created automatically)
                new CleanUp() // delete work directory after process stop
        ) 
        .listening((Listening.AfterStop) process -> {
            System.out.println(new File("./foo").exists()); //true
        })
        .command("echo", "Hello")
        .execute();

System.out.println(new File("./foo").exists()); // false

Run command in a background

StartedProcess startedProcess = new Cmd().command("echo", "Hello").start();
startedProcess.getFuture().get(); //wait result

cmdtool's People

Contributors

alekseysotnikov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cmdtool's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.