use sapphire_sync_core::CTX;
use sapphire_sync_core::framework::backend::protocol as proto;
use sapphire_sync_core::framework::ipc::{ClientInfo, Endpoint, connect_or_absent};
use crate::cli::SyncCommand;
pub async fn dispatch(command: SyncCommand) -> anyhow::Result<i32> {
match command.command {
crate::cli::SyncSubcommand::Disable => disable().await,
}
}
async fn disable() -> anyhow::Result<i32> {
let endpoint = Endpoint::for_app(CTX.app_name)?;
let info = ClientInfo {
kind: "cli".to_owned(),
version: sapphire_sync_server::VERSION.to_owned(),
api: proto::API_VERSION,
app_api: Some(sapphire_sync_api::API_VERSION),
pid: std::process::id(),
};
let Some((client, _)) = connect_or_absent(&endpoint, CTX.app_name, info).await? else {
println!("no {} server is running", CTX.app_name);
return Ok(1);
};
let current: proto::WorkspaceCurrentResult = client
.call(proto::WORKSPACE_CURRENT, serde_json::json!({}))
.await?;
let Some(current) = current.workspace else {
anyhow::bail!(
"the {} server has no workspace selected; run `{} workspace select <dir>`",
CTX.app_name,
CTX.app_name
);
};
let _: proto::Ack = client
.call(proto::SYNC_DISABLE, serde_json::json!({}))
.await?;
println!("sync disabled for {}", current.root.display());
Ok(0)
}