69 lines
1.5 KiB
Rust
69 lines
1.5 KiB
Rust
use anyhow::Result;
|
|
use std::path::Path;
|
|
use tokio::process::Command;
|
|
|
|
use crate::node::error::StackError;
|
|
|
|
use super::utils::is_stack;
|
|
|
|
pub enum CommandStatus {
|
|
Success(String, String),
|
|
Failure(String, String),
|
|
}
|
|
|
|
pub(super) async fn run(
|
|
arion_bin: &Path,
|
|
path: &Path,
|
|
action: StackCommand,
|
|
) -> Result<CommandStatus> {
|
|
let output = Command::new(arion_bin)
|
|
.args(action.command())
|
|
.current_dir(path)
|
|
.output()
|
|
.await?;
|
|
|
|
// Convert stdout and stderr to String
|
|
let stdout_str = String::from_utf8_lossy(&output.stdout).to_string();
|
|
let stderr_str = String::from_utf8_lossy(&output.stderr).to_string();
|
|
|
|
if output.status.success() {
|
|
Ok(CommandStatus::Success(stdout_str, stderr_str))
|
|
} else {
|
|
Ok(CommandStatus::Failure(stdout_str, stderr_str))
|
|
}
|
|
}
|
|
|
|
pub async fn command(
|
|
base_dir: &Path,
|
|
stack_name: &str,
|
|
arion_bin: &Path,
|
|
action: StackCommand,
|
|
) -> Result<CommandStatus> {
|
|
let dir = base_dir.join(stack_name);
|
|
if !is_stack(&dir).await? {
|
|
return Err(StackError::NotFound.into());
|
|
}
|
|
|
|
run(arion_bin, &dir, action).await
|
|
}
|
|
|
|
pub enum StackCommand {
|
|
Down,
|
|
Start,
|
|
Stop,
|
|
Restart,
|
|
Test,
|
|
}
|
|
|
|
impl StackCommand {
|
|
fn command(&self) -> &[&str] {
|
|
match self {
|
|
StackCommand::Down => &["down"],
|
|
StackCommand::Start => &["up", "-d"],
|
|
StackCommand::Stop => &["stop"],
|
|
StackCommand::Restart => &["restart"],
|
|
StackCommand::Test => &["config"],
|
|
}
|
|
}
|
|
}
|