Skip to content

Commit 677e87e

Browse files
fbrvfbrv
andauthored
fb: feat/support docker compose (Commit-Boost#60)
* support docker compose * lint * update println --------- Co-authored-by: fbrv <[email protected]>
1 parent 91137e9 commit 677e87e

File tree

1 file changed

+63
-33
lines changed

1 file changed

+63
-33
lines changed

crates/cli/src/docker_cmd.rs

Lines changed: 63 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,66 @@
1-
use std::process::{Command, Stdio};
1+
use std::{
2+
mem,
3+
process::{Command, Stdio},
4+
str,
5+
};
26

37
use eyre::Result;
48

9+
macro_rules! run_docker_compose {
10+
($compose_path:expr, $($arg:expr),*) => {{
11+
let cmd = determine_docker_compose_command();
12+
match cmd {
13+
Some(mut command) => {
14+
match command.arg("-f").arg($compose_path).args(&[$($arg),*]).output() {
15+
Ok(output) => {
16+
if !output.status.success() {
17+
let stderr = str::from_utf8(&output.stderr).unwrap_or("");
18+
if stderr.contains("permission denied") {
19+
println!("Warning: Permission denied. Try running with sudo.");
20+
} else {
21+
println!("Command failed with error: {}", stderr);
22+
}
23+
}
24+
}
25+
Err(e) => {
26+
println!("Failed to execute command: {}", e);
27+
}
28+
}
29+
}
30+
None => {
31+
println!("Neither `docker compose` nor `docker-compose` were found on your operating system.");
32+
}
33+
}
34+
}};
35+
}
36+
37+
fn determine_docker_compose_command() -> Option<Command> {
38+
if is_command_available("docker compose") {
39+
let mut docker: Command = Command::new("docker");
40+
Some(mem::replace(
41+
docker.arg("compose").stdout(Stdio::inherit()).stderr(Stdio::inherit()),
42+
Command::new("docker"),
43+
))
44+
} else if is_command_available("docker-compose") {
45+
println!("using docker-compose. the command is being deprecated, install docker compose plugin");
46+
let mut docker: Command = Command::new("docker-compose");
47+
Some(mem::replace(
48+
docker.stdout(Stdio::inherit()).stderr(Stdio::inherit()),
49+
Command::new("docker"),
50+
))
51+
} else {
52+
None
53+
}
54+
}
55+
56+
fn is_command_available(command: &str) -> bool {
57+
Command::new("sh")
58+
.arg("-c")
59+
.arg(format!("command -v {}", command))
60+
.output()
61+
.map_or(false, |output| output.status.success())
62+
}
63+
564
pub fn handle_docker_start(compose_path: String, env_path: String) -> Result<()> {
665
println!("Starting Commit-Boost with compose file: {}", compose_path);
766

@@ -10,18 +69,8 @@ pub fn handle_docker_start(compose_path: String, env_path: String) -> Result<()>
1069

1170
println!("Loaded env file: {:?}", env_file);
1271

13-
// TODO: if permission denied, print warning to run as sudo
14-
1572
// start docker compose
16-
Command::new("docker")
17-
.stdout(Stdio::inherit())
18-
.stderr(Stdio::inherit())
19-
.arg("compose")
20-
.arg("-f")
21-
.arg(compose_path)
22-
.arg("up")
23-
.arg("-d")
24-
.output()?;
73+
run_docker_compose!(compose_path, "up", "-d");
2574

2675
Ok(())
2776
}
@@ -32,17 +81,8 @@ pub fn handle_docker_stop(compose_path: String, env_path: String) -> Result<()>
3281
// load env file
3382
dotenvy::from_filename_override(env_path)?;
3483

35-
// TODO: if permission denied, print warning to run as sudo
36-
3784
// start docker compose
38-
Command::new("docker")
39-
.stdout(Stdio::inherit())
40-
.stderr(Stdio::inherit())
41-
.arg("compose")
42-
.arg("-f")
43-
.arg(compose_path)
44-
.arg("down")
45-
.output()?;
85+
run_docker_compose!(compose_path, "down");
4686

4787
Ok(())
4888
}
@@ -51,18 +91,8 @@ pub fn handle_docker_stop(compose_path: String, env_path: String) -> Result<()>
5191
pub fn handle_docker_logs(compose_path: String) -> Result<()> {
5292
println!("Querying Commit-Boost with compose file: {}", compose_path);
5393

54-
// TODO: if permission denied, print warning to run as sudo
55-
5694
// start docker compose
57-
Command::new("docker")
58-
.stdout(Stdio::inherit())
59-
.stderr(Stdio::inherit())
60-
.arg("compose")
61-
.arg("-f")
62-
.arg(compose_path)
63-
.arg("logs")
64-
.arg("-f")
65-
.output()?;
95+
run_docker_compose!(compose_path, "logs", "-f");
6696

6797
Ok(())
6898
}

0 commit comments

Comments
 (0)