flowmarkup:
  title: Exec Commands
  version: 1
  description: "System command execution: args, env, stdout types, error handling, stdin."
  requires:
    EXEC: [git, jq]

  vars:
    count: 10
    input_file: data.csv

  do:

    # Simple command — capture stdout
  - exec:
      _label_: Git status
      command: git
      args: [status, --porcelain]
      timeout: 10s
      result:
        git_status: =RESULT.stdout

    # Working directory + env vars
  - exec:
      _label_: Process CSV with jq
      command: jq
      args: [-R, -s, 'split("\n") | map(select(length > 0) | split(",")) | length', =input_file]
      workingDir: /opt/data
      env:
        JQ_COLORS: "0;90:0;37:0;37:0;37:0;32:1;37:1;37"
        DEBUG: '0'
      timeout: 2m
      result:
        jq_exit: =RESULT.exitCode
        jq_output: =RESULT.stdout
        jq_errors: =RESULT.stderr

  - logWarn: "jq exited {{jq_exit}}: {{jq_errors}}"
    condition: =jq_exit != 0

    # Error handling — catch ExecError, run compensating command
  - try:
      do:
      - exec:
          _label_: Merge feature branch
          command: git
          args: [merge, feature/my-feature, --no-ff]
          timeout: 30s
          result:
            merge_output: =RESULT.stdout
      catch:
        ExecError:
        - logError: "Merge failed (exit {{ERROR.DATA.exitCode}}): {{ERROR.DATA.stderr}}"
        - exec:
            command: git
            args: [merge, --abort]
            timeout: 10s

    # JSON stdout — auto-decoded to CEL map/list
  - exec:
      _label_: Structured output from jq
      command: jq
      args: [--argjson, n, =count, -n, '[range($n)]']
      parseAs: JSON
      timeout: 10s
      result:
        count_list: =RESULT.stdout

    # stdin — pass data to a process
  - exec:
      _label_: Process JSON via jq stdin
      command: jq
      args: [-r, .value]
      stdin: '{"value": {{count}}}'
      timeout: 5s
      result:
        processed_value: =RESULT.stdout
