shapi
, or shell API, is a Lua module which implements some kind of eDSL [1] to work with the shell/CLI [2] environment. It depends on luaposix.
The incentive is to have an alternative to a shell language like bash, as an API.
E.g. for scripting purposes, if one already uses Lua at the heart of its methodology, or to be embedded into an existing application.
local sh = require "shapi"
print("HEAD: "..sh:git("rev-parse", "HEAD"))
print(sh:__in("foo/bar.txt"):md5sum())
sh:__p("my-command", "--foo", "bar"):__out("foo/bar.txt")()
sh:sleep(2):__return()
sh:sleep(2)()
A command object is created and then chain methods are applied to it. This chaining pattern is similar to bash pipes, making :
behave like |
. For most methods, it means linking stdout from the previous step to stdin of the next one while keeping stderr of the parent process (unless __err
is used).
Methods will directly spawn sub-processes as they are called, there is no build phase of the command.
Special methods start with __
and other strings will be interpreted as shell process names.
Calling on the command object is an alias to __return(…)
. String conversion and concatenation will call __return()
.
ℹ️
|
The chain starts from the module, the first method is called on it, but the self parameter will be replaced with the command object.
|
Input a file into the chain.
If file
is a string, file, [mode]
is the path and mode for io.open()
. The mode
defaults to rb
.
If file
is a number, it indicates a file descriptor [3].
Output the chain to a file (create a new process).
If file
is a string, file, [mode]
is the path and mode for io.open()
. The mode
defaults to wb
.
If file
is a number, it indicates a file descriptor [3].
Setup stderr for subsequent processes of the chain.
If file
is a string, file, [mode]
is the path and mode for io.open()
. The mode
defaults to wb
.
If file
is a number, it indicates a file descriptor [3].
local cmd = sh:__err("/dev/null"):cat("foo/missing.txt")
local ok, out = pcall(cmd)
Chain a shell process.
Can be used to chain a shell process with a name which cannot be represented with a Lua name/identifier.
- name
-
shell process name/path (see luaposix execp)
- …
-
process arguments
Chain a Lua function (create a new process).
- fproc
-
Lua function
- …
-
function arguments
__str_in
-- __str_in is equivalent to:
sh:__lua(function() assert(io.stdout:write(data)) end)
Wait/end the command.
It waits on the command processes and returns the command internal state.
- output
-
unprocessed final output (stdout), string
- children
-
list of child
{}
(follows the chain order)- pid
- kind
-
"exited"
,"killed"
or"stopped"
- status
-
exit status, or signal number responsible for
"killed"
or"stopped"
Return/end the command.
It waits on the command processes, propagates exit errors or returns the final output (stdout) as a string.
By default, trailing new lines are removed, but this can be disabled using the mode parameter.
- mode
-
string,
"binary"
to prevent processing of the output