sapphire_sync_cli/sync.rs
1//! The app's own `sync` verb: `sync disable`.
2//!
3//! Enabling, mapping and listing are the framework's `workspace` verbs; only
4//! *disabling* is app-owned, and even it is thin: connect to the running
5//! server (connect-only, nothing is ever started) and call the framework's
6//! `sync.disable` on the workspace it serves. Status is the framework's
7//! report; the app only injects rows (`sapphire_sync_server::status_rows`).
8
9use sapphire_sync_core::CTX;
10use sapphire_sync_core::framework::backend::protocol as proto;
11use sapphire_sync_core::framework::ipc::{ClientInfo, Endpoint, connect_or_absent};
12
13use crate::cli::SyncCommand;
14
15/// Run the parsed `sync` verb, returning the process exit code.
16///
17/// Any error — a server that answers but fails the call — propagates to
18/// the edition's `main`, which prints it in the app's own `sapphire-sync: {err}` shape and
19/// exits 1. An absent server is not an error: it is the one-line report below.
20pub async fn dispatch(command: SyncCommand) -> anyhow::Result<i32> {
21 match command.command {
22 crate::cli::SyncSubcommand::Disable => disable().await,
23 }
24}
25
26/// `sync disable`: stop syncing the server's workspace.
27///
28/// A server serves one workspace (the framework's `workspace.current`), and
29/// `sync.disable` names none: it stops syncing that one. The verb takes no
30/// workspace of its own — the current directory plays no part — so it acts
31/// on the server's workspace
32/// wherever it is typed; `workspace select` is how to point the server at
33/// another. Connect-only, nothing is ever started.
34///
35/// An absent server is the framework's one-line report — `no {app} server is
36/// running` on stdout, exit 1, the exact line the framework's own one-shot
37/// verbs print when nothing is listening (`run_status_into`) — not an error
38/// on stderr. Absence is the verb's answer, the same `connect_or_absent`
39/// `None` every other one-shot verb reports, and a half-open socket or a
40/// shutting-down server folds into it. Every other error — a server with no
41/// workspace selected, a probe failure, a failing call — propagates to
42/// the edition's `main`, which prints it in the app's `sapphire-sync: {err}` shape and
43/// exits 1.
44///
45/// Files and the sync id stay, and success is exit 0 whether or not the
46/// workspace was synced at all — the runtime's `disable` is idempotent, so a
47/// workspace that was never enabled is a no-op, not an error. Re-enabling via
48/// `workspace init --sync` rejoins the same workspace.
49async fn disable() -> anyhow::Result<i32> {
50 let endpoint = Endpoint::for_app(CTX.app_name)?;
51 let info = ClientInfo {
52 kind: "cli".to_owned(),
53 version: sapphire_sync_server::VERSION.to_owned(),
54 api: proto::API_VERSION,
55 app_api: Some(sapphire_sync_api::API_VERSION),
56 pid: std::process::id(),
57 };
58 // Connect-only, and the API gates ride the handshake: a live server of
59 // another framework or app API version is `ApiVersionMismatch` or
60 // `AppApiVersionMismatch`, which propagates like any other error. `None` is nothing listening — the report, exit 1.
61 let Some((client, _)) = connect_or_absent(&endpoint, CTX.app_name, info).await? else {
62 println!("no {} server is running", CTX.app_name);
63 return Ok(1);
64 };
65 let current: proto::WorkspaceCurrentResult = client
66 .call(proto::WORKSPACE_CURRENT, serde_json::json!({}))
67 .await?;
68 let Some(current) = current.workspace else {
69 anyhow::bail!(
70 "the {} server has no workspace selected; run `{} workspace select <dir>`",
71 CTX.app_name,
72 CTX.app_name
73 );
74 };
75 let _: proto::Ack = client
76 .call(proto::SYNC_DISABLE, serde_json::json!({}))
77 .await?;
78 println!("sync disabled for {}", current.root.display());
79 Ok(0)
80}