-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (114 loc) · 2.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"bytes"
"flag"
"fmt"
"os/exec"
)
const (
volumesFlag = "skip-volumes"
networksFlag = "skip-networks"
imagesFlag = "skip-images"
containersFlag = "skip-containers"
pruneFlag = "skip-prune"
forceFlag = "force"
helpFlag = "help"
)
var (
volumes bool
networks bool
images bool
containers bool
prune bool
force bool
help bool
)
func init() {
flag.BoolVar(&volumes, volumesFlag, false, "Skip the deletion of volumes")
flag.BoolVar(&networks, networksFlag, false, "Skip the deletion of networks")
flag.BoolVar(&images, imagesFlag, false, "Skip the remove of images")
flag.BoolVar(&containers, containersFlag, false, "Skip the remove of containers")
flag.BoolVar(&prune, pruneFlag, false, "Skip the prune")
flag.BoolVar(&force, forceFlag, false, "Force remove of containers and images")
flag.BoolVar(&help, helpFlag, false, "Help")
flag.Parse()
}
func main() {
if help {
flag.PrintDefaults()
return
}
deleteVolumes()
deleteNetworks()
removeImages()
removeContainers()
prunes()
}
func deleteVolumes() {
if volumes {
fmt.Println(">> Volumes skipped")
}
command := "docker volume rm $(docker volume ls -qf dangling=true)"
fmt.Printf(">> %s\n", command)
executor(command)
}
func deleteNetworks() {
if networks {
fmt.Println(">> Networks skipped")
}
command := `docker network rm $(docker network ls | grep "bridge" | awk '/ / { print $1 }')`
fmt.Printf(">> %s\n", command)
executor(command)
}
func removeImages() {
if images {
fmt.Println(">> Images skipped")
}
var command string
if force {
command = "docker rmi -f $(docker images -aq)"
} else {
command = `docker rmi $(docker images --filter "dangling=true" -q --no-trunc)`
}
fmt.Printf(">> %s\n", command)
executor(command)
}
func removeContainers() {
if containers {
fmt.Println(">> Containers skipped")
}
var command string
if force {
command = "docker rm -f $(docker ps -aq)"
} else {
command = `docker rm $(docker ps -qa --no-trunc --filter "status=exited")`
}
fmt.Printf(">> %s\n", command)
executor(command)
}
func prunes() {
if prune {
fmt.Println(">> Prune skipped")
}
commandNetwork := `docker network prune -f`
fmt.Printf(">> %s\n", commandNetwork)
executor(commandNetwork)
commandSystem := `docker system prune -f`
fmt.Printf(">> %s\n", commandSystem)
executor(commandSystem)
}
func executor(command string) {
cmd := exec.Command("bash", "-c", command)
var outb, errb bytes.Buffer
cmd.Stdout = &outb
cmd.Stderr = &errb
cmd.Run()
if errb.Len() > 0 {
fmt.Printf("error: \n")
fmt.Printf("%s \n", errb.String())
} else {
fmt.Printf("out: \n")
fmt.Printf("%s \n", outb.String())
}
fmt.Println("----------------------------")
}