atombender/ktail

Linux build documentation?

brc opened this issue ยท 2 comments

brc commented

Hey there ๐Ÿ‘‹
Neat project!

I've somehow avoided Go for a decade+, so I'm not familiar with its toolchain.

Issue

Is there more information needed for building on modern GNU/Linux systems?
Can't for the life of me build this project on Linux.
Spent an hour investigating and trying.

Troubleshooting Details

Distro: Arch Linux (kernel 5.19.5)
Go version: go1.19 linux/amd64

Per the README:

$ mkdir -p $GOPATH/src/github.com/atombender
$ cd $GOPATH/src/github.com/atombender
$ git clone https://github.com/atombender/ktail
$ cd ktail
$ go install .
go: downloading github.com/jpillora/backoff v1.0.0
go: downloading github.com/spf13/pflag v1.0.5
go: downloading github.com/alecthomas/chroma v0.10.0
go: downloading k8s.io/api v0.25.3
go: downloading github.com/fatih/color v1.7.0
go: downloading k8s.io/apimachinery v0.25.3
go: downloading github.com/go-logr/logr v1.2.3
go: downloading k8s.io/client-go v0.25.3
go: downloading k8s.io/klog/v2 v2.80.1
go: downloading github.com/mattn/go-colorable v0.1.12
go: downloading github.com/mattn/go-isatty v0.0.14
go: downloading github.com/gogo/protobuf v1.3.2
go: downloading github.com/google/gofuzz v1.1.0
go: downloading k8s.io/utils v0.0.0-20221012122500-cfd413dd9e85
go: downloading github.com/dlclark/regexp2 v1.4.0
go: downloading sigs.k8s.io/structured-merge-diff/v4 v4.2.3
go: downloading gopkg.in/inf.v0 v0.9.1
go: downloading golang.org/x/sys v0.1.0
go: downloading sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2
go: downloading golang.org/x/net v0.1.0
go: downloading github.com/json-iterator/go v1.1.12
go: downloading gopkg.in/yaml.v2 v2.4.0
go: downloading github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd
go: downloading github.com/modern-go/reflect2 v1.0.2
go: downloading golang.org/x/oauth2 v0.1.0
go: downloading github.com/golang/protobuf v1.5.2
go: downloading github.com/google/gnostic v0.5.7-v3refs
go: downloading github.com/imdario/mergo v0.3.6
go: downloading golang.org/x/term v0.1.0
go: downloading golang.org/x/time v0.0.0-20220210224613-90d013bbcef8
go: downloading sigs.k8s.io/yaml v1.2.0
go: downloading google.golang.org/protobuf v1.28.1
go: downloading k8s.io/kube-openapi v0.0.0-20221012153701-172d655c2280
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/google/go-cmp v0.5.8
go: downloading cloud.google.com/go v0.97.0
go: downloading golang.org/x/text v0.4.0
go: downloading gopkg.in/yaml.v3 v3.0.1
go: downloading github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822
go: downloading github.com/go-openapi/swag v0.19.14
go: downloading github.com/emicklei/go-restful/v3 v3.8.0
go: downloading github.com/go-openapi/jsonreference v0.19.5
go: downloading github.com/go-openapi/jsonpointer v0.19.5
go: downloading github.com/PuerkitoBio/purell v1.1.1
go: downloading github.com/mailru/easyjson v0.7.6
go: downloading github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578
go: downloading github.com/josharian/intern v1.0.0
# github.com/atombender/ktail
./main.go:101:18: undefined: isTerminal
$

^ Undefined symbol isTerminal.

--
Let's find out where that's called in the code:

$ grep -RI isTerminal .
./main.go
101:            colorEnabled = isTerminal(os.Stdout)

--
Ok, let's find out which module that comes from:

$ grep -RIl isTerminal $GOPATH/pkg/mod/
/data/go/pkg/mod/k8s.io/client-go@v0.25.3/plugin/pkg/client/auth/exec/exec_test.go
/data/go/pkg/mod/golang.org/x/term@v0.1.0/term_plan9.go
/data/go/pkg/mod/golang.org/x/term@v0.1.0/term.go
/data/go/pkg/mod/golang.org/x/term@v0.1.0/term_windows.go
/data/go/pkg/mod/golang.org/x/term@v0.1.0/term_unix.go
/data/go/pkg/mod/golang.org/x/term@v0.1.0/term_unsupported.go

^ Seems like golang.org/x/term.

--
Let's see where that module is imported:

$ grep -RIi x/term
go.mod
45:     golang.org/x/term v0.1.0 // indirect

go.sum
407:golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
408:golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
409:golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=

^ go.mod and go.sum. Ok, I really don't know Go...

--
Maybe it needs to be imported in main.go?! ๐Ÿ™‚

$ go install .
# github.com/atombender/ktail
./main.go:19:9: imported and not used: "golang.org/x/term"
./main.go:102:18: undefined: isTerminal

^ Nope ("Imported and not used")! ๐Ÿ˜…

--
Let's find the commit(s) where isTerminal is modified:

$ git log -GisTerminal -p
commit 0ca582825e6be80ee34ecfd2f56b0d7b55b23c61
Author: Alexander Staubo <alex@purefiction.net>
Date:   Mon Oct 17 13:17:25 2022 +0200

    feat: don't enable colour when not writing to a tty; adds --color/--colour to
    control colour output precisely.
diff --git a/main.go b/main.go
index 422ec6a..81c96a9 100644
--- a/main.go
+++ b/main.go
@@ -43,6 +43,7 @@ func main() {
                includePatterns       []*regexp.Regexp
                excludePatternStrings []string
                noColor               bool
+               colorMode             string
        )

        flags := pflag.NewFlagSet("ktail", pflag.ExitOnError)
@@ -68,7 +69,9 @@ func main() {
        flags.BoolVarP(&sinceStart, "since-start", "s", false,
                "Start reading log from the beginning of the container's lifetime.")
        flags.BoolVarP(&showVersion, "version", "", false, "Show version.")
-       flags.BoolVarP(&noColor, "no-color", "", false, "Disable color.")
+       flags.BoolVar(&noColor, "no-color", false, "Alias for --color=never.")
+       flags.StringVar(&colorMode, "color", "auto", "Set color mode: one of 'auto' (default), 'never', or 'always'.")
+       flags.StringVar(&colorMode, "colour", "auto", "Set color mode: one of 'auto' (default), 'never', or 'always'.")

        if err := flags.Parse(os.Args[1:]); err != nil {
                if err == pflag.ErrHelp {
@@ -78,7 +81,19 @@ func main() {
                os.Exit(1)
        }

-       color.NoColor = noColor
+       if noColor {
+               colorMode = "never"
+       }
+       var colorEnabled bool
+       switch colorMode {
+       case "always":
+               colorEnabled = true
+       case "auto":
+               colorEnabled = isTerminal(os.Stdout)
+       case "never":
+       }
+
+       color.NoColor = !colorEnabled

        if showVersion {
                fmt.Printf("ktail %s\n", version)
@@ -222,7 +237,7 @@ func main() {
                        }

                        payload := event.Message
-                       if !noColor && len(payload) >= 2 && payload[0] == '{' && payload[len(payload)-1] == '}' {
+                       if colorEnabled && len(payload) >= 2 && payload[0] == '{' && payload[len(payload)-1] == '}' {
                                var dest interface{}
                                if err := json.Unmarshal([]byte(payload), &dest); err == nil {
                                        var buf bytes.Buffer

^ Ok, just one commit. Fairly recent... maybe not battle-tested "building in the wild" yet... ๐Ÿค”

--
Find any commits involving the string "x/term":

$ git log -Gx/term --stat
commit 80ddf41b886ba762fb10a3ce0041e20d3fa8bffa
Author: Alexander Staubo <alex@purefiction.net>
Date:   Thu Oct 20 23:14:32 2022 +0200

    feat: upgrades Kubernetes client and HTTP2 client to pick up bug fixes etc.

 go.mod |  53 +++++++++++++----------
 go.sum | 243 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------
 2 files changed, 164 insertions(+), 132 deletions(-)

commit d94d59538c9f185c4a1649af4f1c7908472c5ba8
Author: Curtis Ruck <curtis@ruck.io>
Date:   Thu Dec 16 13:15:51 2021 -0500

    update dependencies to latest kubernetes client version

 go.mod |  49 ++++++--
 go.sum | 621 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
 2 files changed, 560 insertions(+), 110 deletions(-)

^ Just lots of go.mod and go.sum noise.

--
Ok.
Discover goimports tool.
Install it with go install golang.org/x/tools/cmd/goimports@latest.
Run it:

$ goimports -d main.go
$

^ No output. goimports thinks everything is legit as hell. ๐Ÿ˜ž

--
I then desperately tried make build, make clean, go clean, export GODEBUG=gocacheverify=1, and wiping out my entire build cache and modules directory, etc etc etc.

No amount of Googling and nothing I could do would make it compile.

Is there something wrong with my Go installation?
Seems like I can go install everything else.

Thanks!

Apologies. There a file that had not been committed. Try pulling and running go install again.

brc commented

No worries, that did the trick! Thanks for the GREAT tool!