Rename lush Lua API: command→capture, subcmd→run, getenv→envget, setenv→envset
Lua-facing names only; C internals unchanged.
lush.capture(str) — return {code, stdout, stderr} table
lush.run(str) — return stripped stdout string
lush.interactive(str) — run with TTY, set _, return result
lush.envget(name) — get environment variable
lush.envset(name,val) — set environment variable
This commit is contained in:
16
lcmd.c
16
lcmd.c
@@ -1270,11 +1270,11 @@ int lushCmd_setenv (lua_State *L) {
|
|||||||
TString *lushname[LUSH_OP_COUNT];
|
TString *lushname[LUSH_OP_COUNT];
|
||||||
|
|
||||||
static const luaL_Reg lushlib[] = {
|
static const luaL_Reg lushlib[] = {
|
||||||
{"command", lushCmd_command},
|
{"capture", lushCmd_command},
|
||||||
{"interactive", lushCmd_interactive},
|
{"interactive", lushCmd_interactive},
|
||||||
{"getenv", lushCmd_getenv},
|
{"run", lushCmd_subcmd},
|
||||||
{"setenv", lushCmd_setenv},
|
{"envget", lushCmd_getenv},
|
||||||
{"subcmd", lushCmd_subcmd},
|
{"envset", lushCmd_setenv},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -1287,11 +1287,11 @@ LUAMOD_API int luaopen_lush (lua_State *L) {
|
|||||||
lua_createtable(L, 0, 4);
|
lua_createtable(L, 0, 4);
|
||||||
lua_setfield(L, -2, "commands");
|
lua_setfield(L, -2, "commands");
|
||||||
/* intern function name strings for OP_LUSH VM access */
|
/* intern function name strings for OP_LUSH VM access */
|
||||||
lushname[LUSH_OP_COMMAND] = luaS_new(L, "command");
|
lushname[LUSH_OP_COMMAND] = luaS_new(L, "capture");
|
||||||
lushname[LUSH_OP_INTERACTIVE] = luaS_new(L, "interactive");
|
lushname[LUSH_OP_INTERACTIVE] = luaS_new(L, "interactive");
|
||||||
lushname[LUSH_OP_GETENV] = luaS_new(L, "getenv");
|
lushname[LUSH_OP_GETENV] = luaS_new(L, "envget");
|
||||||
lushname[LUSH_OP_SETENV] = luaS_new(L, "setenv");
|
lushname[LUSH_OP_SETENV] = luaS_new(L, "envset");
|
||||||
lushname[LUSH_OP_SUBCMD] = luaS_new(L, "subcmd");
|
lushname[LUSH_OP_SUBCMD] = luaS_new(L, "run");
|
||||||
/* store in registry for OP_LUSH access */
|
/* store in registry for OP_LUSH access */
|
||||||
lua_pushvalue(L, -1);
|
lua_pushvalue(L, -1);
|
||||||
lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_LUSH);
|
lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_LUSH);
|
||||||
|
|||||||
@@ -13,30 +13,30 @@ end
|
|||||||
-- === named functions are accessible ===
|
-- === named functions are accessible ===
|
||||||
|
|
||||||
do
|
do
|
||||||
assert(type(lush.command) == "function", "lush.command missing")
|
assert(type(lush.capture) == "function", "lush.capture missing")
|
||||||
assert(type(lush.interactive) == "function", "lush.interactive missing")
|
assert(type(lush.interactive) == "function", "lush.interactive missing")
|
||||||
assert(type(lush.getenv) == "function", "lush.getenv missing")
|
assert(type(lush.run) == "function", "lush.run missing")
|
||||||
assert(type(lush.setenv) == "function", "lush.setenv missing")
|
assert(type(lush.envget) == "function", "lush.envget missing")
|
||||||
assert(type(lush.subcmd) == "function", "lush.subcmd missing")
|
assert(type(lush.envset) == "function", "lush.envset missing")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- === lush.command works like backtick ===
|
-- === lush.capture works like backtick ===
|
||||||
|
|
||||||
do
|
do
|
||||||
local r = lush.command("echo hello")
|
local r = lush.capture("echo hello")
|
||||||
assert(r.code == 0, "lush.command failed")
|
assert(r.code == 0, "lush.capture failed")
|
||||||
assert(r.stdout == "hello\n", "expected 'hello\\n', got: " .. r.stdout)
|
assert(r.stdout == "hello\n", "expected 'hello\\n', got: " .. r.stdout)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- === lush.getenv / lush.setenv ===
|
-- === lush.envget / lush.envset ===
|
||||||
|
|
||||||
do
|
do
|
||||||
lush.setenv("_LUSH_TEST_VAR", "42")
|
lush.envset("_LUSH_TEST_VAR", "42")
|
||||||
assert(lush.getenv("_LUSH_TEST_VAR") == "42")
|
assert(lush.envget("_LUSH_TEST_VAR") == "42")
|
||||||
lush.setenv("_LUSH_TEST_VAR", nil)
|
lush.envset("_LUSH_TEST_VAR", nil)
|
||||||
assert(lush.getenv("_LUSH_TEST_VAR") == nil)
|
assert(lush.envget("_LUSH_TEST_VAR") == nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -72,7 +72,7 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- === user-defined builtin via lush.command ===
|
-- === user-defined builtin via lush.capture ===
|
||||||
|
|
||||||
do
|
do
|
||||||
lush.builtins.myecho = function(cmd, ...)
|
lush.builtins.myecho = function(cmd, ...)
|
||||||
@@ -84,7 +84,7 @@ do
|
|||||||
return t
|
return t
|
||||||
end
|
end
|
||||||
|
|
||||||
local r = lush.command("myecho foo bar")
|
local r = lush.capture("myecho foo bar")
|
||||||
assert(r.code == 0)
|
assert(r.code == 0)
|
||||||
assert(r.stdout == "foo bar\n",
|
assert(r.stdout == "foo bar\n",
|
||||||
"expected 'foo bar\\n', got: " .. r.stdout)
|
"expected 'foo bar\\n', got: " .. r.stdout)
|
||||||
@@ -162,11 +162,11 @@ do
|
|||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- === alias works with lush.command ===
|
-- === alias works with lush.capture ===
|
||||||
|
|
||||||
do
|
do
|
||||||
lush.aliases.myecho = "echo via-command"
|
lush.aliases.myecho = "echo via-command"
|
||||||
local r = lush.command("myecho")
|
local r = lush.capture("myecho")
|
||||||
assert(r.code == 0)
|
assert(r.code == 0)
|
||||||
assert(r.stdout == "via-command\n",
|
assert(r.stdout == "via-command\n",
|
||||||
"expected 'via-command\\n', got: " .. r.stdout)
|
"expected 'via-command\\n', got: " .. r.stdout)
|
||||||
@@ -182,10 +182,10 @@ do
|
|||||||
assert(r.stdout == "works\n")
|
assert(r.stdout == "works\n")
|
||||||
|
|
||||||
-- $VAR expansion
|
-- $VAR expansion
|
||||||
lush.setenv("_LUSH_LIB_TEST", "yes")
|
lush.envset("_LUSH_LIB_TEST", "yes")
|
||||||
local val = $_LUSH_LIB_TEST
|
local val = $_LUSH_LIB_TEST
|
||||||
assert(val == "yes", "expected 'yes', got: " .. tostring(val))
|
assert(val == "yes", "expected 'yes', got: " .. tostring(val))
|
||||||
lush.setenv("_LUSH_LIB_TEST", nil)
|
lush.envset("_LUSH_LIB_TEST", nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@@ -247,30 +247,32 @@ do
|
|||||||
assert(r.code == 127, "removed command should not be found")
|
assert(r.code == 127, "removed command should not be found")
|
||||||
end
|
end
|
||||||
|
|
||||||
-- user command via lush.command()
|
-- user command via lush.capture()
|
||||||
do
|
do
|
||||||
lush.commands.apicmd = function(name, ...)
|
lush.commands.apicmd = function(name, ...)
|
||||||
print("api " .. table.concat({...}, " "))
|
print("api " .. table.concat({...}, " "))
|
||||||
end
|
end
|
||||||
local r = lush.command("apicmd x y")
|
local r = lush.capture("apicmd x y")
|
||||||
assert(r.stdout == "api x y\n",
|
assert(r.stdout == "api x y\n",
|
||||||
"expected 'api x y\\n', got: " .. r.stdout)
|
"expected 'api x y\\n', got: " .. r.stdout)
|
||||||
lush.commands.apicmd = nil
|
lush.commands.apicmd = nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- === lush.subcmd() direct call ===
|
-- === lush.run() direct call ===
|
||||||
|
|
||||||
do
|
do
|
||||||
local result = lush.subcmd("echo hello")
|
local result = lush.run("echo hello")
|
||||||
assert(result == "hello", "subcmd should return stdout without trailing newline, got: " .. tostring(result))
|
assert(result == "hello", "run should return stdout without trailing newline, got: " .. tostring(result))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
-- === lush.interactive() direct call ===
|
-- === lush.interactive() direct call ===
|
||||||
|
|
||||||
do
|
do
|
||||||
lush.interactive("true")
|
local r = lush.interactive("true")
|
||||||
|
assert(type(r) == "table")
|
||||||
|
assert(r.code == 0)
|
||||||
assert(type(_) == "table")
|
assert(type(_) == "table")
|
||||||
assert(_.code == 0)
|
assert(_.code == 0)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user