Skip to content

Commit e65418f

Browse files
committed
Make code more modular
Abstract running container and getting container IP as this helps make sure that the container is running before the container ip is checked. Signed-off-by: Chihurumnaya Ibiam <[email protected]>
1 parent d5c6180 commit e65418f

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

internals.go

+24-8
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,36 @@ func Run(name, tag string) containerDetails {
1818
arg := []string{name, tag}
1919
args := strings.Join(arg, ":")
2020
log.Print(args)
21-
out, err := exec.Command("docker", "run", "-d", args).Output()
2221

23-
if err != nil {
24-
log.Fatal(err)
25-
}
22+
id := runAndReturnID(args)
23+
ip := getIP(id)
2624

27-
_out := string(out)
28-
ip, err := exec.Command("docker", "inspect", "--format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'", _out).Output()
2925
detailedContainer := containerDetails {
30-
ID: _out[:12],
26+
ID: id,
3127
Name: name,
32-
IP: string(ip),
28+
IP: ip,
3329
Tag: tag,
3430
}
3531

3632
return detailedContainer
3733
}
34+
35+
func runAndReturnID(args string) string {
36+
out, err := exec.Command("docker", "run", "-d", args).Output()
37+
if err != nil {
38+
log.Fatal(err)
39+
}
40+
41+
return string(out)[:12]
42+
}
43+
44+
func getIP(id string) string {
45+
format := "--format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'"
46+
ip, err := exec.Command("docker", "inspect", format, id).Output()
47+
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
52+
return string(ip)
53+
}

0 commit comments

Comments
 (0)