File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ import (
22
22
"errors"
23
23
"fmt"
24
24
"log"
25
+ "os"
26
+ "path/filepath"
25
27
goruntime "runtime"
26
28
"sort"
27
29
"strings"
@@ -326,6 +328,11 @@ var removeContainerCommand = &cli.Command{
326
328
Aliases : []string {"f" },
327
329
Usage : "Force removal of the container, disregarding if running" ,
328
330
},
331
+ & cli.BoolFlag {
332
+ Name : "keep-logs" ,
333
+ Aliases : []string {"k" },
334
+ Usage : "Preserve the container log file and its rotations" ,
335
+ },
329
336
& cli.BoolFlag {
330
337
Name : "all" ,
331
338
Aliases : []string {"a" },
@@ -385,6 +392,22 @@ var removeContainerCommand = &cli.Command{
385
392
logrus .Errorf ("removing container %q failed: %v" , id , err )
386
393
errored = true
387
394
continue
395
+ } else if ! ctx .Bool ("keep-logs" ) {
396
+ logPath := resp .GetStatus ().GetLogPath ()
397
+ if logPath != "" {
398
+ logRotations , err := filepath .Glob (logPath + ".*" )
399
+ if err != nil {
400
+ logRotations = []string {}
401
+ }
402
+ logRotations = append (logRotations , logPath )
403
+
404
+ for _ , logFile := range logRotations {
405
+ err = os .Remove (logFile )
406
+ if err != nil {
407
+ logrus .Errorf ("removing log file %s for container %q failed: %v" , logFile , id , err )
408
+ }
409
+ }
410
+ }
388
411
}
389
412
}
390
413
You can’t perform that action at this time.
0 commit comments