Skip to main content

Returning ok

Goal

Return ok (on its own, or with other properties) correctly in every language. Time: 5 minutes.

ok is an ordinary boolean property, but it’s an important convention. Shell blocks generated by session capture always end by printing {"ok":true} and declare ok: boolean. The editor’s insertable examples include Return a successful result and Return ok and a property for every language.

If a block declares ok, the last line must include it as a boolean. The string "true" fails.

outputs:
  type: object
  properties:
    ok: { type: boolean }
    summary: { type: string }
​
summary="3 files cleaned"
# jq -c keeps it on one line and escapes the string safely
jq -nc --arg summary "$summary" '{ok: true, summary: $summary}'
$summary = '3 files cleaned'
@{ ok = $true; summary = $summary } | ConvertTo-Json -Compress
import json
print(json.dumps({"ok": True, "summary": "3 files cleaned"}, separators=(",", ":")))
using System.Text.Json.Nodes;
Console.WriteLine(new JsonObject { ["ok"] = true, ["summary"] = "3 files cleaned" }.ToJsonString());
fn main() {
    // No JSON library without Cargo: only safe when summary has no quotes, backslashes, or newlines.
    let summary = "3 files cleaned";
    println!("{{\"ok\":true,\"summary\":\"{}\"}}", summary);
}
console.log(JSON.stringify({ ok: true, summary: "3 files cleaned" }));
const result: { ok: boolean; summary: string } = { ok: true, summary: "3 files cleaned" };
console.log(JSON.stringify(result));
Tip

Command Prompt isn’t one of the workflow languages. To run a cmd.exe command, use a PowerShell block with the editor’s Run a Command Prompt command example. It runs the command through %ComSpec% and exits with the command’s failure code.

Check your run

Each variant publishes ok: true and summary: "3 files cleaned".

Exercise

Make the Bash version publish ok: false when a directory called logs doesn’t exist in the run’s working directory, without failing the block.

Details
if [ -d logs ]; then ok=true; else ok=false; fi
jq -nc --argjson ok "$ok" '{ok: $ok, summary: "checked logs"}'

The script exits with 0, so the block passes. A later Decision can then branch on steps.<id>.ok == true.