1
- use std:: process:: { Command , Stdio } ;
1
+ use std:: {
2
+ mem,
3
+ process:: { Command , Stdio } ,
4
+ str,
5
+ } ;
2
6
3
7
use eyre:: Result ;
4
8
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
+
5
64
pub fn handle_docker_start ( compose_path : String , env_path : String ) -> Result < ( ) > {
6
65
println ! ( "Starting Commit-Boost with compose file: {}" , compose_path) ;
7
66
@@ -10,18 +69,8 @@ pub fn handle_docker_start(compose_path: String, env_path: String) -> Result<()>
10
69
11
70
println ! ( "Loaded env file: {:?}" , env_file) ;
12
71
13
- // TODO: if permission denied, print warning to run as sudo
14
-
15
72
// 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" ) ;
25
74
26
75
Ok ( ( ) )
27
76
}
@@ -32,17 +81,8 @@ pub fn handle_docker_stop(compose_path: String, env_path: String) -> Result<()>
32
81
// load env file
33
82
dotenvy:: from_filename_override ( env_path) ?;
34
83
35
- // TODO: if permission denied, print warning to run as sudo
36
-
37
84
// 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" ) ;
46
86
47
87
Ok ( ( ) )
48
88
}
@@ -51,18 +91,8 @@ pub fn handle_docker_stop(compose_path: String, env_path: String) -> Result<()>
51
91
pub fn handle_docker_logs ( compose_path : String ) -> Result < ( ) > {
52
92
println ! ( "Querying Commit-Boost with compose file: {}" , compose_path) ;
53
93
54
- // TODO: if permission denied, print warning to run as sudo
55
-
56
94
// 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" ) ;
66
96
67
97
Ok ( ( ) )
68
98
}
0 commit comments