Non-Interactive Mode and Automation¶
When running moi-cli in shell scripts, scheduled jobs, or CI/CD pipelines, combine -o JSON output, jq -r field extraction, MOI_CLI_CONFIG_FILE isolation, and bounded status polling so pipelines remain deterministic and never hang indefinitely. Before writing automation scripts, confirm your credential setup in Configuration and Authentication.
JSON output and error stream separation¶
Passing -o before the command group separates standard output from error diagnostics:
Success output: Business responses are serialized as valid JSON to
stdout, ready to pipe directly intojqor redirect to a file.Error output: When a Product API call fails under
-o,moi-cliwrites a structured error JSON object (code,message,status,request_id) tostderrand exits with a non-zero code.
Enable set -euo pipefail in shell scripts so non-zero exits halt downstream write steps immediately while keeping stdout clean for JSON parsing:
set -euo pipefail
moi-cli -auth pat -o -no-interactive workspace list > workspaces.json
Chain multi-step commands with jq¶
The following script resolves a workspace ID and published workflow ID by name, triggers an execution, and extracts the resulting execution_id:
#!/usr/bin/env bash
set -euo pipefail
WORKSPACE_NAME="analytics-prod"
WORKFLOW_NAME="daily-order-etl"
WORKSPACE_ID="$(
moi-cli -auth pat -o -no-interactive workspace list \
| jq -er --arg name "$WORKSPACE_NAME" 'first(.workspaces[] | select(.name == $name) | .id)'
)"
WORKFLOW_ID="$(
moi-cli -auth pat -o -no-interactive -workspace-id "$WORKSPACE_ID" \
workflow list --name-search "$WORKFLOW_NAME" \
| jq -er --arg name "$WORKFLOW_NAME" 'first(.workflows[] | select(.name == $name) | .id)'
)"
RUN_JSON="$(
moi-cli -auth pat -o -no-interactive -workspace-id "$WORKSPACE_ID" \
workflow run --workflow-id "$WORKFLOW_ID"
)"
EXECUTION_ID="$(printf '%s\n' "$RUN_JSON" | jq -er '.workflow_run.execution_id')"
printf 'Started workflow=%s execution=%s in workspace=%s\n' "$WORKFLOW_ID" "$EXECUTION_ID" "$WORKSPACE_ID" >&2
jq -er exits non-zero whenever a required field is null or missing, stopping the pipeline immediately instead of passing an empty ID into a write command.
Poll asynchronous jobs with a bounded timeout¶
Starting a workflow run, structured import, or asynchronous SQL query only confirms that the server accepted the request. Poll the execution resource by ID and enforce a maximum attempt budget:
#!/usr/bin/env bash
set -euo pipefail
MAX_ATTEMPTS=60
SLEEP_SECONDS=5
for ((attempt = 1; attempt <= MAX_ATTEMPTS; attempt++)); do
STATUS="$(
moi-cli -auth pat -o -no-interactive -workspace-id "$WORKSPACE_ID" \
workflow get-run --workflow-id "$WORKFLOW_ID" --execution-id "$EXECUTION_ID" \
| jq -er '.execution.status'
)"
case "$STATUS" in
completed|succeeded|success)
printf 'Execution %s finished with status=%s\n' "$EXECUTION_ID" "$STATUS" >&2
exit 0
;;
failed|cancelled|canceled)
printf 'Execution %s terminated with status=%s\n' "$EXECUTION_ID" "$STATUS" >&2
exit 1
;;
*)
sleep "$SLEEP_SECONDS"
;;
esac
done
printf 'Timed out waiting for execution %s after %d attempts\n' "$EXECUTION_ID" "$MAX_ATTEMPTS" >&2
exit 124
Isolate CI configuration files¶
When multiple CI jobs share a runner, point MOI_CLI_CONFIG_FILE to a job-scoped temporary file and register an EXIT cleanup trap. This avoids mutating ~/.moi-cli.json on the runner host and keeps -api-key out of per-command process arguments:
#!/usr/bin/env bash
set -euo pipefail
CI_CFG_DIR="$(mktemp -d)"
trap 'rm -rf "$CI_CFG_DIR"' EXIT
export MOI_CLI_CONFIG_FILE="${CI_CFG_DIR}/moi-cli.json"
moi-cli config set-endpoint "${MOI_API_ENDPOINT:-https://api.moi.matrixorigin.cn/v5}"
moi-cli config set-api-key "$MOI_PAT_SECRET"
moi-cli config set-workspace "$MOI_WORKSPACE_ID"
moi-cli -auth pat -o -no-interactive catalog list
If mktemp -d resolves inside a symlinked temporary directory, normalize the path first (for example via cd "$CI_CFG_DIR" && pwd -P) so MOI_CLI_CONFIG_FILE receives a canonical absolute path.
Timeouts and -no-interactive scope¶
-request-timeoutsets the per-call HTTP deadline: Pass a positive duration such as-request-timeout 2mto cover both request transmission and response body reads. When a write command times out, check whether the resource or run already exists on the server before issuing a new create request.-no-interactivedisables terminal pagination prompts only: It suppresses interactive terminal pagers, does not act as a--yesconfirmation bypass for destructive writes, and does not turn asynchronous jobs into synchronous blocking calls. Validate target workspace IDs and resource IDs explicitly in your script before running delete, truncate, unsubscribe, or full-refresh operations.
Credential and log hygiene¶
Inject personal access tokens from protected CI secrets, and never commit tokens or
~/.moi-cli.jsonto version control.Keep
set -x(shell xtrace) disabled around credential setup commands so raw secrets are never echoed into build logs.Log non-sensitive identifiers (
workspace_id,workflow_id,execution_id,request_id, and errorcode), and never print credential-bearing responses such asaccount db-connectionto shared CI logs.
Continue with full flag lookup and domain guides:
Global options and offline schema validation: Command Reference
Domain task guides: Common Tasks