Giter Site home page Giter Site logo

clj-commons-exec's Introduction

clj-commons-exec

Apache Commons Exec wrapper for Clojure

Usage

(require '[clj-commons-exec :as exec])

(exec/sh ["echo" "hello"])   ; A promise is returned immediately.
;=> #<core$promise$reify__5727@12fa6824: :pending>

@(exec/sh ["echo" "hello"])  ; To get a result, deref it.
;=> {:exit 0, :out "hello\n", :err nil}

(exec/sh ["ls" "-l"] {:dir "/"}) ; Second argument is option map.

options

  • :dir (String or java.io.File) override the process dir.
  • :out (OutputStream) is used as the sub-process's stdout.
  • :err (OutputStream) is used as the sub-process's stderror.
  • :in (String or InputStream) is fed to the sub-process's stdin.
  • :close-out? (boolean) close or not a stream of stdout when sub-process complete. Default is false.
  • :close-err? (boolean)
  • :close-in? (boolean)
  • :flush-input? (boolean) flush or not input stream.
  • :handle-quoting? (boolean) Add the argument with/without handling quoting.
  • :watchdog (int or instance of ExecuteWatchdog) set watchdog timer in ms.
  • :env (Map) The environment for the new process. If null, the environment of the current process is used.
  • :add-env (Map) The added environment for the new process.
  • :shutdown (boolean) destroys sub-processes when the VM exits.
  • :as-success (int) is regarded as sucess exit value.
  • :as-successes (sequence) are regarded as sucess exit values.
  • :result-handler-fn (function) A function, which will be called with promise, in, out, err stream and option map, returns an instance which implements org.apache.commons.exec.ExecuteResultHandler.

If you want to have multiple processes piped to each other, you can use sh-pipe. Syntax is like sh :

(exec/sh-pipe ["cat"] ["wc"] {:in "hello world"})
;=> (#<Promise@11ba3c1f: {:exit 0, :out nil, :err nil}>
;    #<Promise@59c12050: {:exit 0, :out "       0       2      11\n", :err nil}>)

When piping commands using sh-pipe, the first command that does not exit successfully will additionally have the :exception key (and corresponding Exception) delivered in its result map:

(exec/sh-pipe ["cat" "abc.txt"] ["wc"])
;=> (#<Promise@563da1dc:
;     {:exit 1, :out nil,
;      :err "cat: abc.txt: No such file or directory\n",
;      :exception #<ExecuteException org.apache.commons.exec.ExecuteException:
;                   Process exited with an error: 1 (Exit value: 1)>}>
;    #<Promise@7bff88c3:
;     {:exit 0, :out "       0       0       0\n", :err nil}>)

Note: Commands that are waiting for piped input downstream of any such error might receive an empty stream. Most likely, they will finish execution, but their output will be logically incorrect.

Installation

Leiningen [org.clojars.hozumi/clj-commons-exec "1.2.0"]

License

Copyright (C) 2011 FIXME

Distributed under the Eclipse Public License, the same as Clojure.

clj-commons-exec's People

Contributors

echeran avatar hozumi avatar bilus avatar drtom avatar

Stargazers

 avatar  avatar zhangzhihong avatar Kyle Feng avatar Mehulkumar avatar Chris Andrews avatar kurrunk37 avatar Burin Choomnuan avatar Joël Kuiper avatar Roberto Oliveros avatar John Beppu avatar Programming is fun avatar Andre W. avatar  avatar Vladislav Bauer avatar C avatar  avatar Ivan Mikushin avatar shrek.wang avatar  avatar Terry Lewis avatar M. Tong avatar Matt Adereth avatar Prasad Chalasani avatar Daniel Turczański avatar  avatar  avatar  avatar Fabien Bourgeois avatar Danny Wilson avatar Steve Hill avatar

Watchers

 avatar James Cloos avatar

clj-commons-exec's Issues

Streams passed to sh should not be closed

Calling sh with { :out (System/out) } results in stdout being closed.

In general the entity that created the resource should be responsible for its lifetime management so i wouldn't have expected that sh would close the streams I passed in.

A work-around was to proxy my streams prior passing them to sh...something like:

(defn as-non-closeable [os]
"OutputStream that doesn't close its inner stream"
(let [p (proxy [FilterOutputStream] [os](close []))]
p))

BTW, thanks for this great library!

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.