diff --git a/Makefile b/Makefile index 138c56c..b700ab0 100644 --- a/Makefile +++ b/Makefile @@ -30,6 +30,8 @@ endif export CGO_ENABLED ?= 0 BUILD_PKG = ./cmd/catp +BUILD_LDFLAGS=-s -w +BUILD_FLAGS=-trimpath -pgo=./cmd/catp.pgo -include $(DEVGO_PATH)/makefiles/main.mk -include $(DEVGO_PATH)/makefiles/lint.mk @@ -39,5 +41,3 @@ BUILD_PKG = ./cmd/catp # Add your custom targets here. -## Run tests -test: test-unit diff --git a/cmd/catp.pgo b/cmd/catp.pgo new file mode 100644 index 0000000..6b2dfdf Binary files /dev/null and b/cmd/catp.pgo differ diff --git a/cmd/catp/main.go b/cmd/catp/main.go index 0cb8cdb..0289a84 100644 --- a/cmd/catp/main.go +++ b/cmd/catp/main.go @@ -2,18 +2,19 @@ package main import ( "bufio" + "bytes" "flag" "fmt" "io" "log" "os" + "runtime/pprof" "strings" "time" "github.com/bool64/progress" "github.com/klauspost/compress/zstd" gzip "github.com/klauspost/pgzip" - _ "gitlab.com/rackn/seekable-zstd" ) type runner struct { @@ -22,6 +23,9 @@ type runner struct { readBytes int64 totalBytes int64 + grep [][]byte + reverse bool + currentFile *progress.CountingReader currentTotal int64 } @@ -44,15 +48,34 @@ func (r *runner) st(s progress.ProgressStatus) string { return res } -func readFile(r io.Reader) { - rd := bufio.NewReader(r) +func (r *runner) readFile(rd io.Reader) { + b := bufio.NewReaderSize(rd, 64*1024) - _, err := io.Copy(os.Stdout, rd) + _, err := io.Copy(os.Stdout, b) if err != nil { log.Fatal(err) } } +func (r *runner) scanFile(rd io.Reader) { + s := bufio.NewScanner(rd) + s.Buffer(make([]byte, 64*1024), 10*1024*1024) + + for s.Scan() { + for _, g := range r.grep { + if bytes.Contains(s.Bytes(), g) { + _, _ = os.Stdout.Write(s.Bytes()) + + break + } + } + } + + if err := s.Err(); err != nil { + log.Fatal(err) + } +} + func (r *runner) cat(filename string) { file, err := os.Open(filename) if err != nil { @@ -75,6 +98,10 @@ func (r *runner) cat(filename string) { } } + if r.reverse { + + } + r.pr.Start(func(t *progress.Task) { t.TotalBytes = func() int64 { return r.totalBytes @@ -86,16 +113,49 @@ func (r *runner) cat(filename string) { t.Task = filename t.Continue = true }) - readFile(rd) + + if len(r.grep) > 0 || r.reverse { + r.scanFile(rd) + } else { + r.readFile(rd) + } r.pr.Stop() r.readBytes += r.currentFile.Bytes() } func main() { + grep := flag.String("grep", "", "grep pattern, may contain multiple patterns separated by \\|") + cpuProfile := flag.String("dbg-cpu-prof", "", "write first 10 seconds of CPU profile to file") + flag.Parse() + if *cpuProfile != "" { + f, err := os.Create(*cpuProfile) //nolint:gosec + if err != nil { + log.Fatal(err) + } + + if err = pprof.StartCPUProfile(f); err != nil { + log.Fatal(err) + } + + go func() { + time.Sleep(10 * time.Second) + pprof.StopCPUProfile() + println("CPU profile written to", *cpuProfile) + }() + } + r := &runner{} + r.reverse = *reverse + + if *grep != "" { + for _, s := range strings.Split(*grep, "\\|") { + r.grep = append(r.grep, []byte(s)) + } + } + r.sizes = make(map[string]int64) r.pr = &progress.Progress{ Interval: 5 * time.Second, diff --git a/cmd/go.mod b/cmd/go.mod index 325c620..192ac1a 100644 --- a/cmd/go.mod +++ b/cmd/go.mod @@ -6,10 +6,4 @@ require ( github.com/bool64/progress v0.1.0 github.com/klauspost/compress v1.16.5 github.com/klauspost/pgzip v1.2.5 - gitlab.com/rackn/seekable-zstd v0.8.1 -) - -require ( - github.com/cespare/xxhash/v2 v2.2.0 // indirect - gitlab.com/rackn/simplecache v0.0.0-20230324193231-44368de53d93 // indirect ) diff --git a/cmd/go.sum b/cmd/go.sum index f1bf79a..d4b1c79 100644 --- a/cmd/go.sum +++ b/cmd/go.sum @@ -1,17 +1,7 @@ github.com/bool64/dev v0.2.27 h1:mFT+B74mFVgUeUmm/EbfM6ELPA55lEXBjQ/AOHCwCOc= github.com/bool64/progress v0.1.0 h1:khsGInrTrQMyskfcRTE9gP982koSqKobYFJ977JLxhA= github.com/bool64/progress v0.1.0/go.mod h1:XwIA2+r2sEKxIRa6TFR0FqD3GHt0wz4dk4IL5CwbuKo= -github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= -github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= -github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI= github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/klauspost/pgzip v1.2.5 h1:qnWYvvKqedOF2ulHpMG72XQol4ILEJ8k2wwRl/Km8oE= github.com/klauspost/pgzip v1.2.5/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs= -github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= -github.com/stretchr/testify v1.7.2 h1:4jaiDzPyXQvSd7D0EjG45355tLlV3VOECpq10pLC+8s= -gitlab.com/rackn/seekable-zstd v0.8.1 h1:vemYjUK3hr1uSipdVQGKX/SOOlWUr1kXTfjfgLfCPVE= -gitlab.com/rackn/seekable-zstd v0.8.1/go.mod h1:9z8nf3qNXOi73VRm7KQgTmI3T0tz9YzDKKL7fzEBz9M= -gitlab.com/rackn/simplecache v0.0.0-20230324193231-44368de53d93 h1:lXoXk/e9YrtTyWzNZs1ak/ijpwZQDaJLEwKCjhp/dCw= -gitlab.com/rackn/simplecache v0.0.0-20230324193231-44368de53d93/go.mod h1:pXhP0EyrEy0pGf2DW4vTKub/As/UiamLFaZ1Q9YaFTs= -gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=