use std::{path::Path, process::Command};
use super::{
command::{add_debug_arg, command_display},
error::IcpCommandError,
model::IcpCli,
run::{run_json, run_output_with_stderr, run_status_inherit, run_success},
};
impl IcpCli {
pub fn local_replica_start(
&self,
background: bool,
debug: bool,
) -> Result<String, IcpCommandError> {
let mut command = self.local_replica_command("start");
run_local_replica_start_command(&mut command, background, debug)
}
pub fn local_replica_start_in(
&self,
cwd: &Path,
background: bool,
debug: bool,
) -> Result<String, IcpCommandError> {
let mut command = self.local_replica_command_in("start", cwd);
run_local_replica_start_command(&mut command, background, debug)
}
pub fn local_replica_status(&self, debug: bool) -> Result<String, IcpCommandError> {
let mut command = self.local_replica_command("status");
add_debug_arg(&mut command, debug);
run_output_with_stderr(&mut command)
}
pub fn local_replica_status_in(
&self,
cwd: &Path,
debug: bool,
) -> Result<String, IcpCommandError> {
let mut command = self.local_replica_command_in("status", cwd);
add_debug_arg(&mut command, debug);
run_output_with_stderr(&mut command)
}
pub fn local_replica_status_json(
&self,
debug: bool,
) -> Result<serde_json::Value, IcpCommandError> {
let mut command = self.local_replica_command("status");
add_debug_arg(&mut command, debug);
command.arg("--json");
run_json(&mut command)
}
pub fn local_replica_status_json_in(
&self,
cwd: &Path,
debug: bool,
) -> Result<serde_json::Value, IcpCommandError> {
let mut command = self.local_replica_command_in("status", cwd);
add_debug_arg(&mut command, debug);
command.arg("--json");
run_json(&mut command)
}
pub fn local_replica_project_running(&self, debug: bool) -> Result<bool, IcpCommandError> {
let mut command = self.local_replica_command("status");
add_debug_arg(&mut command, debug);
run_success(&mut command)
}
pub fn local_replica_project_running_in(
&self,
cwd: &Path,
debug: bool,
) -> Result<bool, IcpCommandError> {
let mut command = self.local_replica_command_in("status", cwd);
add_debug_arg(&mut command, debug);
run_success(&mut command)
}
pub fn local_replica_ping(&self, debug: bool) -> Result<bool, IcpCommandError> {
let mut command = self.local_replica_command("ping");
add_debug_arg(&mut command, debug);
run_success(&mut command)
}
pub fn local_replica_stop(&self, debug: bool) -> Result<String, IcpCommandError> {
let mut command = self.local_replica_command("stop");
add_debug_arg(&mut command, debug);
run_output_with_stderr(&mut command)
}
pub fn local_replica_stop_in(
&self,
cwd: &Path,
debug: bool,
) -> Result<String, IcpCommandError> {
let mut command = self.local_replica_command_in("stop", cwd);
add_debug_arg(&mut command, debug);
run_output_with_stderr(&mut command)
}
#[must_use]
pub fn local_replica_start_display(&self, background: bool, debug: bool) -> String {
let mut command = self.local_replica_command("start");
add_debug_arg(&mut command, debug);
if background {
command.arg("--background");
}
command_display(&command)
}
#[must_use]
pub fn local_replica_status_display(&self, debug: bool) -> String {
let mut command = self.local_replica_command("status");
add_debug_arg(&mut command, debug);
command_display(&command)
}
#[must_use]
pub fn local_replica_stop_display(&self, debug: bool) -> String {
let mut command = self.local_replica_command("stop");
add_debug_arg(&mut command, debug);
command_display(&command)
}
fn local_replica_command(&self, action: &str) -> Command {
let mut command = self.command();
command.args(["network", action]);
self.add_local_network_target(&mut command);
command
}
fn local_replica_command_in(&self, action: &str, cwd: &Path) -> Command {
let mut command = self.command_in(cwd);
command.args(["network", action]);
self.add_local_network_target(&mut command);
command
}
}
fn run_local_replica_start_command(
command: &mut Command,
background: bool,
debug: bool,
) -> Result<String, IcpCommandError> {
add_debug_arg(command, debug);
if background {
command.arg("--background");
return run_output_with_stderr(command);
}
run_status_inherit(command)?;
Ok(String::new())
}