Skip to content

armgit / mso

Public Imported from GitHub
0

Browser-based graphical shell and control plane for a Linux server you own — real terminal, file manager, live metrics and BYOK AI in one mobile-first workspace. Next.js 16 · React 19 · node-pty · Tailwind v4 · shadcn/ui. Public alpha, self-hosted.

7.7 KB
Raw Blame
#!/usr/bin/env bash
# Devices, OAuth/MCP/audit, service/update, CRUD and escape-hatch commands.
# Usage variables are defined by scripts/cli/commands.sh before this sourced handler runs.
# shellcheck disable=SC2154
mso_cmd_admin() {
  local cmd="$1"; shift || true
case "$cmd" in
  passwd) source "$ROOT/scripts/cli/passwd.sh"; mso_passwd "$ROOT" "$@" ;;
  reset|uninstall) exec node "$ROOT/scripts/mso-maintenance.mjs" "$cmd" "$@" ;;
  # ── devices (local file, works even when the service is down) ────────────
  # Both shapes work: `mso devices` reads as a noun, `mso device list` matches the
  # subcommand-group habit every other area here uses (mapp/term/camoufox/service).
  devices|device)
    case "${1:-list}" in
      list)    dev_script --list ;;
      pending) dev_script --pending ;;
      approve) [ -n "${2-}" ] || die "usage: mso device approve <deviceId> [label] [--role viewer|operator|owner]"
               dev_script "$2" "${@:3}" ;;
      role)    [ -n "${2-}" ] && [ -n "${3-}" ] || die "usage: mso device role <deviceId> <viewer|operator|owner>"
               dev_script --set-role "$2" "$3" ;;
      revoke)  [ -n "${2-}" ] || die "usage: mso device revoke <deviceId> | all --yes"
               if [ "$2" = "all" ]; then dev_script --revoke-all "${3-}"
               else dev_script --revoke "$2"; fi ;;
      *) die "usage: mso $U_device" ;;
    esac ;;
  approve) [ -n "${1-}" ] || die "usage: mso approve <deviceId> [label] [--role viewer|operator|owner]  (ids: mso device list)"
           dev_script "$1" "${@:2}" ;;
  revoke)  [ -n "${1-}" ] || die "usage: mso revoke <deviceId>  (ids: mso device list)"
           dev_script --revoke "$1" ;;
  # ── provider OAuth (device-code, NOT a browser redirect) ─────────────────
  oauth)
    p="${1:?"provider (e.g. openai)"}"; sub="${2:-start}"
    case "$sub" in
      start|poll) jpost "/api/oauth/$(enc "$p")" "$(jq -n --arg a "$sub" '{action:$a}')" ;;
      *) die "usage: mso $U_oauth" ;;
    esac ;;

  # ── MCP tokens (the bearers that let ChatGPT et al. drive this host) ─────
  mcp)
    case "${1:-list}" in
      list)     jget "/api/mcp/tokens" ;;
      pat) label="${2-}"; scope="${3:-read}"; ttl_days="${4:-0}"
           [ -n "$label" ] || die "usage: mso mcp pat <label> [read|write|exec] [ttlDays]"
           case "$scope" in
             read|write|exec) ;;
             *) die "scope must be read, write, or exec" ;;
           esac
           [[ "$ttl_days" =~ ^[0-9]+$ ]] || die "ttlDays must be a non-negative integer"
           jpost "/api/mcp/tokens" "$(jq -cn --arg label "$label" --arg scope "$scope" --argjson ttlDays "$ttl_days" '{label:$label,scope:$scope,ttlDays:$ttlDays}')" ;;
      project) jpost "/api/v1/project-mcp" "${2:?project MCP JSON required}" ;;
      activity) jget "/api/mcp/activity?limit=${2:-80}" ;;
      service-token) bun "$ROOT/scripts/mcp-service-token.ts" "${@:2}" ;;
      revoke) [ -n "${2-}" ] || die "usage: mso mcp revoke <id|all>   (ids: mso mcp list)"
              jdel "/api/mcp/tokens?id=$(enc "$2")" ;;
      *) die "usage: mso $U_mcp" ;;
    esac ;;

  # ── audit trail (the forensic record; MCP writes land here too) ──────────
  audit)
    q="limit=${1:-50}"; [ -n "${2-}" ] && q="$q&prefix=$(enc "$2")"
    jget "/api/v1/sys/audit?$q" ;;

  whoami)  echo "cli device: $(cli_device)"; jget "/api/auth/me" ;;
  login)   mso_private_state_remove_file "$JAR" || die "refusing unsafe cookie jar: $JAR"
           JAR=$(mso_private_state_ensure_file "$JAR") || die "refusing unsafe cookie jar: $JAR"
           login; echo "logged in as device $(cli_device)" ;;
  logout)  if [ -s "$JAR" ]; then jpost "/api/auth/logout" >/dev/null 2>&1 || true; fi
           mso_private_state_remove_file "$JAR" || die "refusing unsafe cookie jar: $JAR"
           echo "signed out (jar cleared)" ;;
  pair)    source "$ROOT/scripts/cli/pair.sh"; mso_pair "$@" ;;

  # ── service (local systemd) ──────────────────────────────────────────────
  service)
    case "${1:-status}" in
      status)  systemctl status --no-pager mso.service ;;
      start)   service_start_safe ;;
      stop)    sudo systemctl stop mso.service ;;
      restart) service_restart_safe ;;
      logs)    journalctl -u mso.service -n "${2:-50}" --no-pager ;;
      *) die "usage: mso $U_service" ;;
    esac ;;
  # NOT `next build` — this is mso.service's WorkingDirectory, and a build here
  # deletes .next under the running process, 404ing every chunk until a restart.
  # verify-build.sh compiles HEAD in a temp dir instead. `deploy` uses the same
  # checkout-wide quiesce/build/restart/restore lifecycle as service self-update.
  build)  (cd "$ROOT" && bash scripts/verify-build.sh) ;;
  deploy) deploy_safe ;;
  # The same self-update Settings → Update MSO drives, so both surfaces get the same
  # preflight and the same transient-unit handoff (a build spawned from the service
  # would be killed by the restart it performs).
  # `--auto` / `--channel` are SETTINGS, not a run: they go to the same authority the
  # Settings toggle uses. Keeping them out of scripts/mso-update means a value can
  # never be mistaken for a target ref.
  update)
    case "${1-}" in
      --auto)
        case "${2-}" in
          on|off) bun "$ROOT/scripts/mso-update-automation.ts" auto "${2}" ;;
          *) die "usage: mso update --auto on|off" ;;
        esac ;;
      --channel)
        case "${2-}" in
          stable|main) bun "$ROOT/scripts/mso-update-automation.ts" channel "${2}" ;;
          *) die "usage: mso update --channel stable|main" ;;
        esac ;;
      *)
        local_url="$(gateway_local_url)"; MSO_UPDATE_ROOT="$ROOT" MSO_UPDATE_LOCAL_URL="$local_url" "$ROOT/scripts/mso-update" "$@" ;;
    esac ;;

  # ── editor-document CRUD (one pattern, all editor features) ──────────────
  crud)
    sub="${1:-help}"; shift || true
    case "$sub" in
      list) jget "/api/v1/fs/list?path=$(enc "${1:?path}")" ;;
      get)  jget "/api/v1/fs/read?path=$(enc "${1:?path}")" | jq -r . ;;
      del)  jdel "/api/v1/fs/delete" "$(jq -n --arg p "${1:?path}" '{path:$p}')" ;;
      cmds) jget "/api/v1/editor/exec" | jq -r '.tools[] | "  \(.name)\t\(.description)"' ;;
      set)
        path="${1:?path}"; shift; op="${1:?command-or-content}"; shift || true
        if [[ "$op" =~ ^[a-z]+\.[a-zA-Z]+$ ]]; then
          # Editor-doc command → ONE atomic server op: the route reads the doc
          # (or seeds it), applies the command, and writes it back in place.
          a=$(kv_args "$@")
          resp=$(jpost "/api/v1/editor/exec" "$(jq -n --arg p "$path" --arg n "$op" --argjson a "$a" '{path:$p,commands:[{name:$n,args:$a}]}')")
          jq -e '.dimsFallback' >/dev/null 2>&1 <<<"$resp" && echo "  ! image dims unknown → defaulted (AVIF/HEIC/TIFF not probed)" >&2
          jq -r '.results[]? | "  \(.name) \(if .ok then "ok" else "ERR" end) — \(.result)"' <<<"$resp"
        else
          jpost "/api/v1/fs/write" "$(jq -n --arg p "$path" --arg c "$op${*:+ $*}" '{path:$p,content:$c}')"
        fi ;;
      *) die "usage: mso $U_crud" ;;
    esac ;;

  # ── escape hatch: any endpoint at all ────────────────────────────────────
  api)
    m="${1:?METHOD}"; p="${2:?/api/...}"; body="${3-}"
    if [ -n "$body" ]; then req -X "$m" -H 'content-type: application/json' -d "$body" "$B$p"
    else req -X "$m" "$B$p"; fi ;;

  # Verb list comes from verbs(), subcommands from U_<verb> usage strings.
  completion) source "$ROOT/scripts/cli/completion.sh"; mso_completion "$@"; exit 0 ;;
  *) die "internal CLI routing error: $cmd reached the wrong command family" ;;
esac
}