ctb_workspace_ipc/workspace_runner/
cli.rs

1// SPDX-License-Identifier: AGPL-3.0-or-later
2/*
3This file is part of Collective Toolbox, a database and document workspace and utilities.
4Copyright (C) 2026 Collective Toolbox Developers
5Contact: info@collectivetoolbox.com
6
7This program is free software: you can redistribute it and/or modify it under
8the terms of the GNU Affero General Public License as published by the Free
9Software Foundation, either version 3 of the License, or (at your option) any
10later version.
11
12This program is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.
15
16You should have received a copy of the GNU Affero General Public License along
17with this program.  If not, see <https://www.gnu.org/licenses/>.
18*/
19
20//! CLI argument parsing for child subprocess launcher modes.
21
22use std::env;
23
24#[allow(
25    unused_imports,
26    clippy::wildcard_imports,
27    reason = "Standard workspace module prelude"
28)]
29use crate::utilities::*;
30
31use ipc::ChildKind;
32
33pub async fn parse_subprocess_cli_and_maybe_start_service(
34    args: Vec<String>,
35) -> Result<(Option<ChildKind>, Vec<String>)> {
36    let (kind, remaining_args) =
37        ctb_cli::subprocess::parse_subprocess_cli(env::args().collect())?;
38
39    if let Some(kind) = kind {
40        start_service(&kind, &args).await?;
41        unreachable!();
42    }
43
44    Ok((kind, remaining_args))
45}
46
47pub async fn start_service(
48    kind: &ChildKind,
49    args: &Vec<String>,
50) -> Result<(Option<ChildKind>, Vec<String>)> {
51    let _process_kind = ipc::format_child_kind(kind);
52
53    let socket = ctb_cli::subprocess::parse_subprocess_socket(args)?
54        .ok_or_else(|| {
55            anyhow::anyhow!("--socket required for subprocess mode")
56        })?;
57    let token = ctb_cli::subprocess::read_token_from_stdin()?;
58    crate::services::run_service(*kind, &socket, &token).await?;
59    std::process::exit(0);
60}