@@ -2,19 +2,83 @@ package main
2
2
3
3
import (
4
4
"context"
5
+ "flag"
5
6
"fmt"
7
+ "github.com/docker/docker/api/types"
8
+ "github.com/docker/docker/client"
9
+ "github.com/fatih/color"
6
10
"os"
11
+ "os/exec"
7
12
"os/signal"
13
+ "path/filepath"
8
14
"strconv"
15
+ "strings"
9
16
"syscall"
10
17
"time"
18
+ )
11
19
12
- "github.com/docker/docker/api/types"
13
- "github.com/docker/docker/client"
14
- "github.com/fatih/color"
20
+ var (
21
+ // these vars will be set on build time
22
+ versionTag string
23
+ versionSha1 string
24
+ buildDate string
15
25
)
16
26
27
+ func isSameOrChildPath (base string , other string ) bool {
28
+ if base == other {
29
+ return true
30
+ }
31
+ if strings .HasPrefix (other , base + "/" ) {
32
+ return true
33
+ }
34
+ return false
35
+ }
36
+
37
+ func isDockerComposeDir (dir string ) bool {
38
+ cmd := exec .Command ("docker-compose" , "config" )
39
+ cmd .Dir = dir
40
+ if err := cmd .Run (); err != nil {
41
+ return false
42
+ }
43
+ return true
44
+ }
45
+
17
46
func main () {
47
+ var printHelp bool
48
+ var printVersion bool
49
+ var allContainer bool
50
+ execName := filepath .Base (os .Args [0 ])
51
+ flag .BoolVar (& printHelp , "help" , false , "Print help" )
52
+ flag .BoolVar (& printVersion , "version" , false , "Print version" )
53
+ flag .BoolVar (& allContainer , "all" , false , "Follow logs of any container, not just of current docker compose project" )
54
+ flag .Parse ()
55
+ if printHelp {
56
+ fmt .Printf ("Usage: %s [-all]\n " , execName )
57
+ flag .PrintDefaults ()
58
+ os .Exit (0 )
59
+ }
60
+ if printVersion {
61
+ fmt .Printf ("%s built %s, version %s (%s)\n " , execName , buildDate , versionTag , versionSha1 )
62
+ os .Exit (0 )
63
+ }
64
+
65
+ baseDir , err := os .Getwd ()
66
+ if err != nil {
67
+ panic (err )
68
+ }
69
+
70
+ onlyComposeContainer := true
71
+ if isDockerComposeDir (baseDir ) {
72
+ onlyComposeContainer = true
73
+ }
74
+ if allContainer {
75
+ onlyComposeContainer = false
76
+ }
77
+ if onlyComposeContainer {
78
+ bold := color .New (color .Bold )
79
+ bold .Printf ("Only following containers of current docker-compose project...\n " )
80
+ }
81
+
18
82
sigs := make (chan os.Signal , 1 )
19
83
newContainers := make (chan ContainerInfo , 1 )
20
84
done := make (chan bool , 1 )
@@ -59,6 +123,10 @@ func main() {
59
123
for event := range events {
60
124
//fmt.Printf("%s %s %s\n", event.Type, event.Status, event.Action)
61
125
if event .Type == "container" && event .Action == "start" {
126
+ compose_project_dir := event .Actor .Attributes ["com.docker.compose.project.working_dir" ]
127
+ if onlyComposeContainer && (len (compose_project_dir ) == 0 || ! isSameOrChildPath (baseDir , compose_project_dir )) {
128
+ continue
129
+ }
62
130
container_number := 1
63
131
if i , err := strconv .Atoi (event .Actor .Attributes ["com.docker.compose.container-number" ]); err == nil {
64
132
container_number = i
@@ -107,6 +175,10 @@ func main() {
107
175
}
108
176
for i := range containers {
109
177
container := containers [i ]
178
+ compose_project_dir := container .Labels ["com.docker.compose.project.working_dir" ]
179
+ if onlyComposeContainer && (len (compose_project_dir ) == 0 || ! isSameOrChildPath (baseDir , compose_project_dir )) {
180
+ continue
181
+ }
110
182
container_number := 1
111
183
if i , err := strconv .Atoi (container .Labels ["com.docker.compose.container-number" ]); err == nil {
112
184
container_number = i
0 commit comments