-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
172 lines (150 loc) · 3.04 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"fmt"
"os"
"strings"
)
var (
debug = os.Getenv("DEBUG")
args = os.Args[1:]
arg0 string
arg1 string
detach bool
timeout string
removeOrphans bool
signal string
tty bool
user string
quiet bool
)
const (
binaryName = "podman-compose"
binaryVersion = "1.0.13"
fileYML = "docker-compose.yml"
fileYAML = "docker-compose.yaml"
fileYMLOverride = "docker-compose.override.yml"
fileYAMLOverride = "docker-compose.override.yaml"
fileEnv = ".env"
)
const (
colorBlue = "\033[1;34m%s\033[0m"
colorCyan = "\033[1;36m%s\033[0m"
colorGreen = "\033[1;32m%s\033[0m"
colorPurple = "\033[1;35m%s\033[0m"
colorRed = "\033[1;31m%s\033[0m"
colorYellow = "\033[1;33m%s\033[0m"
colorWhite = "\033[1;37m%s\033[0m"
)
type Command struct {
OutputStatus bool
OutputNewlines bool
Tasks []CommandTask
}
func init() {
setArgs()
if err := runUsage(); err != nil {
fmt.Println(err)
os.Exit(0)
}
}
func main() {
l := loadEnvironmentVariables()
e := parseYAML(l.Environment)
g := buildCommand(e, l.Environment)
for _, field := range g.Tasks {
e := executeCommand(
field.Command,
field.OutputCustomMessage,
field.OutputSingleline,
field.OutputQuiet,
)
output := strings.TrimSpace(e.OutputCustomMessage)
switch e.OutputStatusCode {
case 0:
if !field.OutputQuiet {
if g.OutputStatus {
fmt.Printf(colorGreen, "[OK] ")
}
if g.OutputNewlines {
fmt.Println(output)
} else {
fmt.Print(output)
}
}
case 1:
m := strings.Split(output, "\n")
for i, s := range m {
if i == 0 {
if s != "" {
fmt.Printf(colorRed, "[**] ")
fmt.Println(s)
}
} else {
fmt.Printf(colorRed, "|**| "+s+"\n")
}
}
}
}
}
func buildCommand(e *Yaml, l []EnvironmentVariable) Command {
g := Command{}
switch arg0 {
case "build":
return newComposeBuildCommand(e, l)
case "up", "start":
return newComposeUpStartCommand(e, l)
case "down":
return newComposeDownCommand(e, l)
case "exec":
return newComposeExecCommand(e, l)
case "kill":
return newComposeKillCommand(e, l)
case "restart":
return newComposeRestartCommand(e, l)
case "stop":
return newComposeStopCommand(e, l)
case "ps":
return newComposePsCommand(e, l)
case "pull":
return newComposePullCommand(e, l)
case "logs":
return newComposeLogsCommand(e, l)
}
return g
}
func setArgs() {
for i, v := range args {
if i == 0 {
arg0 = v
}
if i == 1 {
arg1 = v
}
}
for i, f := range args {
if f == "-d" || f == "--detach" {
detach = true
}
if f == "-t" || f == "--timeout" {
timeout = args[(i + 1)]
}
if f == "--remove-orphans" {
removeOrphans = true
}
if f == "-q" || f == "--quiet" {
quiet = true
}
if f == "-s" || f == "--signal" {
signal = args[(i + 1)]
}
if f == "-u" || f == "--user" {
user = args[(i + 1)]
}
}
}
func getContainerName(v Services, name string) string {
if v.ContainerName != "" {
name = v.ContainerName
}
return name
}