Skip to content

Commit

Permalink
Refactor (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jun 9, 2023
1 parent 51b485c commit b98a9d6
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 217 deletions.
136 changes: 0 additions & 136 deletions .github/workflows/test-unit.yml

This file was deleted.

Binary file modified cmd/catp.pgo
Binary file not shown.
89 changes: 56 additions & 33 deletions cmd/catp/main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package main provides catp CLI tool.
package main

import (
Expand All @@ -13,6 +14,7 @@ import (
"sync/atomic"
"time"

"github.com/bool64/dev/version"
"github.com/bool64/progress"
"github.com/klauspost/compress/zstd"
gzip "github.com/klauspost/pgzip"
Expand All @@ -25,22 +27,22 @@ type runner struct {
matches int64
totalBytes int64

grep [][]byte
reverse bool
grep [][]byte

currentFile *progress.CountingReader
currentTotal int64
lastErr error
}

// st renders ProgressStatus as a string.
func (r *runner) st(s progress.ProgressStatus) string {
// st renders Status as a string.
func (r *runner) st(s progress.Status) string {
var res string

if len(r.sizes) > 1 {
fileDonePercent := 100 * float64(r.currentFile.Bytes()) / float64(r.currentTotal)
res = fmt.Sprintf("all: %.1f%% bytes read, %s: %.1f%% bytes read, %d lines processed, %.1f l/s, %.1f MB/s, elapsed %s, remaining %s",
s.DonePercent, s.Task, fileDonePercent, s.LinesCompleted, s.SpeedLPS, s.SpeedMBPS,
s.Elapsed.Round(10*time.Millisecond).String(), s.Remaining.String())

} else {
res = fmt.Sprintf("%s: %.1f%% bytes read, %d lines processed, %.1f l/s, %.1f MB/s, elapsed %s, remaining %s",
s.Task, s.DonePercent, s.LinesCompleted, s.SpeedLPS, s.SpeedMBPS,
Expand Down Expand Up @@ -70,7 +72,12 @@ func (r *runner) scanFile(rd io.Reader) {
for s.Scan() {
for _, g := range r.grep {
if bytes.Contains(s.Bytes(), g) {
_, _ = os.Stdout.Write(append(s.Bytes(), '\n'))
if _, err := os.Stdout.Write(append(s.Bytes(), '\n')); err != nil {
r.lastErr = err

return
}

atomic.AddInt64(&r.matches, 1)

break
Expand All @@ -79,16 +86,21 @@ func (r *runner) scanFile(rd io.Reader) {
}

if err := s.Err(); err != nil {
log.Fatal(err)
r.lastErr = err
}
}

func (r *runner) cat(filename string) {
file, err := os.Open(filename)
func (r *runner) cat(filename string) (err error) {
file, err := os.Open(filename) //nolint:gosec
if err != nil {
log.Fatal(err)
return err
}
defer file.Close()

defer func() {
if clErr := file.Close(); clErr != nil && err == nil {
err = clErr
}
}()

r.currentFile = &progress.CountingReader{Reader: file}
r.currentTotal = r.sizes[filename]
Expand All @@ -97,18 +109,14 @@ func (r *runner) cat(filename string) {
switch {
case strings.HasSuffix(filename, ".gz"):
if rd, err = gzip.NewReader(rd); err != nil {
log.Fatalf("failed to init gzip reader: %s", err)
return fmt.Errorf("failed to init gzip reader: %w", err)
}
case strings.HasSuffix(filename, ".zst"):
if rd, err = zstd.NewReader(rd); err != nil {
log.Fatalf("failed to init gzip reader: %s", err)
return fmt.Errorf("failed to init gzip reader: %w", err)
}
}

if r.reverse {

}

r.pr.Start(func(t *progress.Task) {
t.TotalBytes = func() int64 {
return r.totalBytes
Expand All @@ -121,37 +129,50 @@ func (r *runner) cat(filename string) {
t.Continue = true
})

if len(r.grep) > 0 || r.reverse {
if len(r.grep) > 0 {
r.scanFile(rd)
} else {
r.readFile(rd)
}

r.pr.Stop()
r.readBytes += r.currentFile.Bytes()

return r.lastErr
}

func startProfiling(cpuProfile string) {
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)
}()
}

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")
ver := flag.Bool("version", false, "print version and exit")

flag.Parse()

if *cpuProfile != "" {
f, err := os.Create(*cpuProfile) //nolint:gosec
if err != nil {
log.Fatal(err)
}
if *ver {
fmt.Println(version.Module("github.com/bool64/progress").Version)

if err = pprof.StartCPUProfile(f); err != nil {
log.Fatal(err)
}
return
}

go func() {
time.Sleep(10 * time.Second)
pprof.StopCPUProfile()
println("CPU profile written to", *cpuProfile)
}()
if *cpuProfile != "" {
startProfiling(*cpuProfile)
}

r := &runner{}
Expand All @@ -165,7 +186,7 @@ func main() {
r.sizes = make(map[string]int64)
r.pr = &progress.Progress{
Interval: 5 * time.Second,
Print: func(status progress.ProgressStatus) {
Print: func(status progress.Status) {
println(r.st(status))
},
}
Expand All @@ -183,6 +204,8 @@ func main() {
}

for i := 0; i < flag.NArg(); i++ {
r.cat(flag.Arg(i))
if err := r.cat(flag.Arg(i)); err != nil {
log.Fatal(err)
}
}
}
9 changes: 0 additions & 9 deletions cmd/go.mod

This file was deleted.

7 changes: 0 additions & 7 deletions cmd/go.sum

This file was deleted.

6 changes: 5 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ module github.com/bool64/progress

go 1.20

require github.com/bool64/dev v0.2.27
require (
github.com/bool64/dev v0.2.28
github.com/klauspost/compress v1.16.5
github.com/klauspost/pgzip v1.2.6
)
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
github.com/bool64/dev v0.2.27 h1:mFT+B74mFVgUeUmm/EbfM6ELPA55lEXBjQ/AOHCwCOc=
github.com/bool64/dev v0.2.27/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
github.com/bool64/dev v0.2.28 h1:6ayDfrB/jnNr2iQAZHI+uT3Qi6rErSbJYQs1y8rSrwM=
github.com/bool64/dev v0.2.28/go.mod h1:iJbh1y/HkunEPhgebWRNcs8wfGq7sjvJ6W5iabL8ACg=
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.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
3 changes: 0 additions & 3 deletions go.work

This file was deleted.

Loading

0 comments on commit b98a9d6

Please sign in to comment.