flowmarkup:
  title: SSH Commands
  version: 1
  description: "Remote command execution: stdout parsing, stdin, env vars, multi-server orchestration."
  requires:
    SSH:
      prod_server: [df, rsync, deploy.sh]
      staging: [deploy.sh]
    SECRET: [deploy_token]
    ENV: [DEPLOY_VERSION]

  input:
    target_env:
      $kind: TEXT
      $format: '^(staging|production)$'

  output:
    deploy_status: TEXT
    disk_usage_pct: INTEGER

  do:

    # Check disk space — auto-decode stdout as TEXT (default)
  - ssh:
      _label_: Check disk space
      host: prod_server
      command: df
      args: [-h, /opt/app]
      timeout: 30s
      result:
        disk_info: =RESULT.stdout
        exitCode: =RESULT.exitCode

  - log: ="Disk info: " + disk_info

    # JSON stdout from remote command
  - ssh:
      _label_: Get deployment status
      host: prod_server
      command: deploy.sh
      args: [status, --format, json]
      parseAs: JSON
      timeout: 1m
      result: { deploy_status: =RESULT.stdout }

  - log: ="Current version: " + deploy_status.version

    # Environment variables and secrets
  - ssh:
      _label_: Deploy to staging
      host: staging
      command: deploy.sh
      args: [deploy, =ENV.DEPLOY_VERSION]
      env:
        DEPLOY_TOKEN: =SECRET.deploy_token
        DEPLOY_ENV: staging
      timeout: 10m
      retry: "3/30s/EXPONENTIAL"
      result:
        staging_output: =RESULT.stdout
      errors: [SshError, TimeoutError]

  - log: ="Staging deploy output: " + staging_output

    # Conditional production deploy
  - if:
      condition: =target_env == "production"
      then:
      - ssh:
          _label_: Deploy to production
          host: prod_server
          command: deploy.sh
          args: [deploy, =ENV.DEPLOY_VERSION]
          env:
            DEPLOY_TOKEN: =SECRET.deploy_token
            DEPLOY_ENV: production
          timeout: 15m
          result:
            prod_output: =RESULT.stdout

    # Sync files between servers using rsync
  - ssh:
      _label_: Sync static assets
      host: prod_server
      command: rsync
      args: [-avz, --delete, /opt/app/static/, /opt/backup/static/]
      timeout: 5m
      result:
        sync_output: =RESULT.stdout

  - log: ="Rsync complete: " + sync_output

  - return: {deploy_status: =deploy_status.version, disk_usage_pct: 40}
