module:shell
- Description:
Shell command interface for
bkshRun
bksh -helpto see all registered shell commands.Special global command-line arguments:
-noexit- keep the shell running after executing the command-exit- exit with error if no shell command found-exit-timeout MS- will be set to ms to wait before exit for async actions to finish-shell-delay MS- will wait before running the command to allow initialization complete
Shell functions must be defined in
shell.commandsobject, wheremyCommandis the command name in camel case for-my-commandThe function may return special values:
stop- stop processing commands and create REPLcontinue- do not exit and continue processing other commands or end with REPL
all other values will result in returning from the run assuming the command will decide what to do, exit or continue running, no REPL is created
- Source:
Example
const { shell } = require("backendjs");
shell.commands.myCommand = function(options) {
console.log("hello");
return "continue"
}
// Calling `bksh -my-command` it will run this command.
Methods
(static) runFile()
- Description:
Load a module and optionally execute it
- Source:
Example
var { app } = require("backendjs")
app.test = 123;
exports.run = function() {
console.log("run");
}
exports.newMethod = function() {
console.log(app.version, "version");
}
// Save into a file a.js and run
bksh -run-file a.js
// In the shell now it new methods can be executed
> shell.newMethod()
(static) sendRequest(options)
- Description:
Shell command:
sendRequestSends an HTTP request using lib.fetch with optional BackendJS-style API signing, CSRF token bootstrap, custom headers/cookies, and response shaping/formatting.
Request inputs:
- Request data is taken from shell.getQuery:
- If
-methodis provided (i.e. non-empty), the query becomespostdata - If
-methodis not provided, the query becomes URLqueryparams
- If
Options:
-url <url>: Target URL to request.-method <verb>: HTTP method (e.g.POST,PUT). When set, query data is sent as body (postdata).-user <id>: Load user record viaapi.users.get(id)and use itslogin/secretfor request signing.-hdr k=v(repeatable): Adds custom request headers.-cookie k=v(repeatable): Adds custom request cookies.
Response output controls:
-raw: print raw response body (rc.data) and include response headers (rc.resheaders) in output.-json: print parsed response object (rc.obj) as a JSON string.-select a,b.c,...: reduce the parsed response object to only the specified fields/paths.-flatten: flatten the parsed response object into dot-notation keys.
Signing behavior:
- When a user is loaded (
-user) and haslogin/secret, asignerhook is provided tolib.fetch:- Sets
contentTypefrom outgoingcontent-typeheader - Calls
api.signature.create(login, secret, request)and adds the signature header to the request
- Sets
Control flow:
- Runs as a 3-step async series:
- Optional user lookup
- Optional CSRF bootstrap fetch
- Main request fetch + output formatting
- Terminates via shell.exit on completion or error.
- Request data is taken from shell.getQuery:
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
options |
Array.<string> | Object | CLI options/args for the command. |
(static) stats(options, callback)
- Description:
Shell command:
statsQueries Elasticsearch-backed metrics via stats.queryElasticsearch and prints results.
Options parsing:
-pool <name>: Elasticsearch pool to use (default:elasticsearch)-groups <list>: Group-by fields (default:tag)-tags <list>: Tag filters-tsize <n>: Terms aggregation size (top N groups)-ssize <n>: Sub-aggregation size-trim <n>: Trim result sets (implementation-specific to stats module)-timedelta <sec>: Bucket size / time step in seconds (converted to ms)-age <sec>: How far back to query (default: 300s, converted to ms)-interval <sec>: Query/rollup interval in seconds (default: 60s, converted to ms)-fields <list>: Metric fields to query (default:host_cpu_util)-count <n>: Limit number of points/rows returned-since <ts>: Start timestamp (epoch seconds)-before <ts>: End timestamp (epoch seconds)-columns <json>: Optional columns configuration (JSON). Parsed withlib.jsonParse
Output modes:
-raw: prints the raw response object as JSON.- default: formats each numeric series:
- rounds each value
- sorts values descending
- joins them into a comma-separated string
-flatten: flattens nestedrc.datausinglib.flattenbefore printing.- prints a header line with relative time labels derived from
rc.timestampsusinglib.toDuration(..., { age: 1, short: 1, round: 2 }). -info: includesrc.infoin the formatted output.
Exits the shell process via
shell.exit(err)when done.
- Source:
Parameters:
| Name | Type | Description |
|---|---|---|
options |
Array.<string> | Object | Command-line args/options passed to the shell command. |
callback |
function | Unused (kept for command signature compatibility). |