Skip to content

Commit 085a29c

Browse files
committed
Conditionally toggle whether to follow containers that don't belong to current docker-compose project
1 parent d3c17ec commit 085a29c

File tree

3 files changed

+92
-4
lines changed

3 files changed

+92
-4
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
*.iml
44

55
# built executable
6-
docker-log-watch
6+
build/

build.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash -e
2+
NOW=$(date -u +'%Y-%m-%d_%TZ')
3+
HEAD_SHA1=$(git rev-parse HEAD)
4+
HEAD_TAG=$(git tag --points-at HEAD | grep -e "^v" | sort | tail -1 | cut -b2-)
5+
6+
for platform in windows/amd64 linux/amd64 linux/arm64 darwin/amd64 darwin/arm64 ; do
7+
platform_split=(${platform//\// })
8+
GOOS=${platform_split[0]}
9+
GOARCH=${platform_split[1]}
10+
output_name="docker-log-watch.${GOOS}-${GOARCH}"
11+
if [ "${GOOS}" = "windows" ]; then
12+
output_name+='.exe'
13+
fi
14+
echo "Building for platform $platform..."
15+
env GOOS=${GOOS} GOARCH=${GOARCH} go build -ldflags "-X main.versionTag=$HEAD_TAG -X main.versionSha1=$HEAD_SHA1 -X main.buildDate=$NOW" -o build/$output_name
16+
done

docker-log-watch.go

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,83 @@ package main
22

33
import (
44
"context"
5+
"flag"
56
"fmt"
7+
"github.com/docker/docker/api/types"
8+
"github.com/docker/docker/client"
9+
"github.com/fatih/color"
610
"os"
11+
"os/exec"
712
"os/signal"
13+
"path/filepath"
814
"strconv"
15+
"strings"
916
"syscall"
1017
"time"
18+
)
1119

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
1525
)
1626

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+
1746
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+
1882
sigs := make(chan os.Signal, 1)
1983
newContainers := make(chan ContainerInfo, 1)
2084
done := make(chan bool, 1)
@@ -59,6 +123,10 @@ func main() {
59123
for event := range events {
60124
//fmt.Printf("%s %s %s\n", event.Type, event.Status, event.Action)
61125
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+
}
62130
container_number := 1
63131
if i, err := strconv.Atoi(event.Actor.Attributes["com.docker.compose.container-number"]); err == nil {
64132
container_number = i
@@ -107,6 +175,10 @@ func main() {
107175
}
108176
for i := range containers {
109177
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+
}
110182
container_number := 1
111183
if i, err := strconv.Atoi(container.Labels["com.docker.compose.container-number"]); err == nil {
112184
container_number = i

0 commit comments

Comments
 (0)