11use anyhow:: { anyhow, Result } ;
2- use tokio:: process:: Command as AsyncCommand ;
32use dialoguer:: Confirm ;
3+ use tokio:: process:: Command as AsyncCommand ;
44
55pub async fn cleanup ( dry_run : bool ) -> Result < ( ) > {
66 println ! ( "🐳 Docker Safe Cleanup" ) ;
@@ -12,13 +12,13 @@ pub async fn cleanup(dry_run: bool) -> Result<()> {
1212
1313 // Check for unused containers
1414 cleanup_containers ( dry_run) . await ?;
15-
15+
1616 // Check for unused images
1717 cleanup_images ( dry_run) . await ?;
18-
18+
1919 // Check for unused volumes
2020 cleanup_volumes ( dry_run) . await ?;
21-
21+
2222 // Check for unused networks
2323 cleanup_networks ( dry_run) . await ?;
2424
@@ -40,24 +40,34 @@ async fn is_docker_available() -> Result<bool> {
4040
4141async fn cleanup_containers ( dry_run : bool ) -> Result < ( ) > {
4242 println ! ( "\n 📦 Checking for stopped containers..." ) ;
43-
43+
4444 let output = AsyncCommand :: new ( "docker" )
45- . args ( [ "ps" , "-a" , "--filter" , "status=exited" , "--format" , "table {{.ID}}\\ t{{.Image}}\\ t{{.Status}}" ] )
45+ . args ( [
46+ "ps" ,
47+ "-a" ,
48+ "--filter" ,
49+ "status=exited" ,
50+ "--format" ,
51+ "table {{.ID}}\\ t{{.Image}}\\ t{{.Status}}" ,
52+ ] )
4653 . output ( )
4754 . await ?;
4855
4956 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
5057 let lines: Vec < & str > = stdout. lines ( ) . collect ( ) ;
51-
58+
5259 if lines. len ( ) <= 1 {
5360 println ! ( " No stopped containers found." ) ;
5461 return Ok ( ( ) ) ;
5562 }
5663
5764 println ! ( "{}" , stdout) ;
58-
65+
5966 if dry_run {
60- println ! ( " [DRY RUN] Would remove {} stopped containers" , lines. len( ) - 1 ) ;
67+ println ! (
68+ " [DRY RUN] Would remove {} stopped containers" ,
69+ lines. len( ) - 1
70+ ) ;
6171 return Ok ( ( ) ) ;
6272 }
6373
@@ -73,7 +83,10 @@ async fn cleanup_containers(dry_run: bool) -> Result<()> {
7383 if result. status . success ( ) {
7484 println ! ( " ✅ Stopped containers removed successfully" ) ;
7585 } else {
76- println ! ( " ❌ Failed to remove containers: {}" , String :: from_utf8_lossy( & result. stderr) ) ;
86+ println ! (
87+ " ❌ Failed to remove containers: {}" ,
88+ String :: from_utf8_lossy( & result. stderr)
89+ ) ;
7790 }
7891 }
7992
@@ -82,24 +95,33 @@ async fn cleanup_containers(dry_run: bool) -> Result<()> {
8295
8396async fn cleanup_images ( dry_run : bool ) -> Result < ( ) > {
8497 println ! ( "\n 🖼️ Checking for unused images..." ) ;
85-
98+
8699 let output = AsyncCommand :: new ( "docker" )
87- . args ( [ "images" , "--filter" , "dangling=true" , "--format" , "table {{.ID}}\\ t{{.Repository}}\\ t{{.Tag}}\\ t{{.Size}}" ] )
100+ . args ( [
101+ "images" ,
102+ "--filter" ,
103+ "dangling=true" ,
104+ "--format" ,
105+ "table {{.ID}}\\ t{{.Repository}}\\ t{{.Tag}}\\ t{{.Size}}" ,
106+ ] )
88107 . output ( )
89108 . await ?;
90109
91110 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
92111 let lines: Vec < & str > = stdout. lines ( ) . collect ( ) ;
93-
112+
94113 if lines. len ( ) <= 1 {
95114 println ! ( " No dangling images found." ) ;
96115 return Ok ( ( ) ) ;
97116 }
98117
99118 println ! ( "{}" , stdout) ;
100-
119+
101120 if dry_run {
102- println ! ( " [DRY RUN] Would remove {} dangling images" , lines. len( ) - 1 ) ;
121+ println ! (
122+ " [DRY RUN] Would remove {} dangling images" ,
123+ lines. len( ) - 1
124+ ) ;
103125 return Ok ( ( ) ) ;
104126 }
105127
@@ -115,7 +137,10 @@ async fn cleanup_images(dry_run: bool) -> Result<()> {
115137 if result. status . success ( ) {
116138 println ! ( " ✅ Dangling images removed successfully" ) ;
117139 } else {
118- println ! ( " ❌ Failed to remove images: {}" , String :: from_utf8_lossy( & result. stderr) ) ;
140+ println ! (
141+ " ❌ Failed to remove images: {}" ,
142+ String :: from_utf8_lossy( & result. stderr)
143+ ) ;
119144 }
120145 }
121146
@@ -124,24 +149,34 @@ async fn cleanup_images(dry_run: bool) -> Result<()> {
124149
125150async fn cleanup_volumes ( dry_run : bool ) -> Result < ( ) > {
126151 println ! ( "\n 💾 Checking for unused volumes..." ) ;
127-
152+
128153 let output = AsyncCommand :: new ( "docker" )
129- . args ( [ "volume" , "ls" , "--filter" , "dangling=true" , "--format" , "table {{.Name}}\\ t{{.Driver}}\\ t{{.Scope}}" ] )
154+ . args ( [
155+ "volume" ,
156+ "ls" ,
157+ "--filter" ,
158+ "dangling=true" ,
159+ "--format" ,
160+ "table {{.Name}}\\ t{{.Driver}}\\ t{{.Scope}}" ,
161+ ] )
130162 . output ( )
131163 . await ?;
132164
133165 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
134166 let lines: Vec < & str > = stdout. lines ( ) . collect ( ) ;
135-
167+
136168 if lines. len ( ) <= 1 {
137169 println ! ( " No unused volumes found." ) ;
138170 return Ok ( ( ) ) ;
139171 }
140172
141173 println ! ( "{}" , stdout) ;
142-
174+
143175 if dry_run {
144- println ! ( " [DRY RUN] Would remove {} unused volumes" , lines. len( ) - 1 ) ;
176+ println ! (
177+ " [DRY RUN] Would remove {} unused volumes" ,
178+ lines. len( ) - 1
179+ ) ;
145180 return Ok ( ( ) ) ;
146181 }
147182
@@ -157,7 +192,10 @@ async fn cleanup_volumes(dry_run: bool) -> Result<()> {
157192 if result. status . success ( ) {
158193 println ! ( " ✅ Unused volumes removed successfully" ) ;
159194 } else {
160- println ! ( " ❌ Failed to remove volumes: {}" , String :: from_utf8_lossy( & result. stderr) ) ;
195+ println ! (
196+ " ❌ Failed to remove volumes: {}" ,
197+ String :: from_utf8_lossy( & result. stderr)
198+ ) ;
161199 }
162200 }
163201
@@ -166,24 +204,34 @@ async fn cleanup_volumes(dry_run: bool) -> Result<()> {
166204
167205async fn cleanup_networks ( dry_run : bool ) -> Result < ( ) > {
168206 println ! ( "\n 🌐 Checking for unused networks..." ) ;
169-
207+
170208 let output = AsyncCommand :: new ( "docker" )
171- . args ( [ "network" , "ls" , "--filter" , "dangling=true" , "--format" , "table {{.ID}}\\ t{{.Name}}\\ t{{.Driver}}" ] )
209+ . args ( [
210+ "network" ,
211+ "ls" ,
212+ "--filter" ,
213+ "dangling=true" ,
214+ "--format" ,
215+ "table {{.ID}}\\ t{{.Name}}\\ t{{.Driver}}" ,
216+ ] )
172217 . output ( )
173218 . await ?;
174219
175220 let stdout = String :: from_utf8_lossy ( & output. stdout ) ;
176221 let lines: Vec < & str > = stdout. lines ( ) . collect ( ) ;
177-
222+
178223 if lines. len ( ) <= 1 {
179224 println ! ( " No unused networks found." ) ;
180225 return Ok ( ( ) ) ;
181226 }
182227
183228 println ! ( "{}" , stdout) ;
184-
229+
185230 if dry_run {
186- println ! ( " [DRY RUN] Would remove {} unused networks" , lines. len( ) - 1 ) ;
231+ println ! (
232+ " [DRY RUN] Would remove {} unused networks" ,
233+ lines. len( ) - 1
234+ ) ;
187235 return Ok ( ( ) ) ;
188236 }
189237
@@ -199,9 +247,12 @@ async fn cleanup_networks(dry_run: bool) -> Result<()> {
199247 if result. status . success ( ) {
200248 println ! ( " ✅ Unused networks removed successfully" ) ;
201249 } else {
202- println ! ( " ❌ Failed to remove networks: {}" , String :: from_utf8_lossy( & result. stderr) ) ;
250+ println ! (
251+ " ❌ Failed to remove networks: {}" ,
252+ String :: from_utf8_lossy( & result. stderr)
253+ ) ;
203254 }
204255 }
205256
206257 Ok ( ( ) )
207- }
258+ }
0 commit comments