Skip to content

Commit acbc720

Browse files
authored
Add line counting mode, add rate limit, add static build (#25)
1 parent 2d40379 commit acbc720

File tree

6 files changed

+67
-29
lines changed

6 files changed

+67
-29
lines changed

.github/workflows/release-assets.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
uses: actions/checkout@v4
2525
- name: Build artifacts
2626
run: |
27-
make release-assets
27+
make release-assets docker-build-static
2828
- name: Upload linux_amd64.tar.gz
2929
if: hashFiles('linux_amd64.tar.gz') != ''
3030
uses: actions/upload-release-asset@v1
@@ -41,6 +41,14 @@ jobs:
4141
asset_path: ./linux_amd64_dbg.tar.gz
4242
asset_name: linux_amd64_dbg.tar.gz
4343
asset_content_type: application/tar+gzip
44+
- name: Upload linux_amd64_static.tar.gz
45+
if: hashFiles('linux_amd64_static.tar.gz') != ''
46+
uses: actions/upload-release-asset@v1
47+
with:
48+
upload_url: ${{ github.event.release.upload_url }}
49+
asset_path: ./linux_amd64_static.tar.gz
50+
asset_name: linux_amd64_static.tar.gz
51+
asset_content_type: application/tar+gzip
4452
- name: Upload linux_arm64.tar.gz
4553
if: hashFiles('linux_arm64.tar.gz') != ''
4654
uses: actions/upload-release-asset@v1

Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,8 @@ BUILD_LDFLAGS=-s -w
4242

4343
## Run tests
4444
test: test-unit
45+
46+
# Build statically linked binary in docker.
47+
docker-build-static:
48+
docker run --platform linux/amd64 -v $(PWD):/code -w /code --rm golang:alpine /bin/sh -c 'apk add build-base && GOOS=linux GOARCH=amd64 CGO_ENABLED=1 go build --ldflags "-linkmode external -extldflags=-static" -tags cgo_zstd -o ./bin/catp ./cmd/catp && ./bin/catp'
49+
cd ./bin && tar zcvf ../linux_amd64_static.tar.gz *

cmd/catp/README.md

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,52 +11,53 @@ go install github.com/bool64/progress/cmd/catp@latest
1111
or download from [releases](https://github.com/bool64/progress/releases).
1212

1313
```
14-
wget https://github.com/bool64/progress/releases/latest/download/linux_amd64.tar.gz && tar xf linux_amd64.tar.gz && rm linux_amd64.tar.gz
14+
wget https://github.com/bool64/progress/releases/latest/download/linux_amd64_static.tar.gz && tar xf linux_amd64_static.tar.gz && rm linux_amd64_static.tar.gz
1515
./catp -version
1616
```
1717

1818
## Usage
1919

2020
```
21-
catp dev, go1.22rc1 CGO_ZSTD
22-
23-
catp prints contents of files to STDOUT or dir/file output,
24-
while printing current progress status to STDERR.
21+
catp prints contents of files to STDOUT or dir/file output,
22+
while printing current progress status to STDERR.
2523
It can decompress data from .gz and .zst files.
2624
Use dash (-) as PATH to read STDIN.
2725
2826
Usage of catp:
2927
catp [OPTIONS] PATH ...
3028
-dbg-cpu-prof string
31-
write first 10 seconds of CPU profile to file
29+
write first 10 seconds of CPU profile to file
3230
-dbg-mem-prof string
33-
write heap profile to file after 10 seconds
31+
write heap profile to file after 10 seconds
32+
-l count lines
3433
-no-progress
35-
disable progress printing
34+
disable progress printing
3635
-out-dir string
37-
output to directory instead of STDOUT
38-
files will be written to out dir with original base names
39-
disables output flag
36+
output to directory instead of STDOUT
37+
files will be written to out dir with original base names
38+
disables output flag
4039
-output string
41-
output to file (can have .gz or .zst ext for compression) instead of STDOUT
40+
output to file (can have .gz or .zst ext for compression) instead of STDOUT
4241
-parallel int
43-
number of parallel readers if multiple files are provided
44-
lines from different files will go to output simultaneously (out of order of files, but in order of lines in each file)
45-
use 0 for multi-threaded zst decoder (slightly faster at cost of more CPU) (default 1)
42+
number of parallel readers if multiple files are provided
43+
lines from different files will go to output simultaneously (out of order of files, but in order of lines in each file)
44+
use 0 for multi-threaded zst decoder (slightly faster at cost of more CPU) (default 1)
4645
-pass value
47-
filter matching, may contain multiple AND patterns separated by ^,
48-
if filter matches, line is passed to the output (unless filtered out by -skip)
49-
each -pass value is added with OR logic,
50-
for example, you can use "-pass bar^baz -pass foo" to only keep lines that have (bar AND baz) OR foo
46+
filter matching, may contain multiple AND patterns separated by ^,
47+
if filter matches, line is passed to the output (unless filtered out by -skip)
48+
each -pass value is added with OR logic,
49+
for example, you can use "-pass bar^baz -pass foo" to only keep lines that have (bar AND baz) OR foo
5150
-progress-json string
52-
write current progress to a file
51+
write current progress to a file
52+
-rate-limit float
53+
output rate limit lines per second
5354
-skip value
54-
filter matching, may contain multiple AND patterns separated by ^,
55-
if filter matches, line is removed from the output (even if it passed -pass)
56-
each -skip value is added with OR logic,
57-
for example, you can use "-skip quux^baz -skip fooO" to skip lines that have (quux AND baz) OR fooO
55+
filter matching, may contain multiple AND patterns separated by ^,
56+
if filter matches, line is removed from the output (even if it passed -pass)
57+
each -skip value is added with OR logic,
58+
for example, you can use "-skip quux^baz -skip fooO" to skip lines that have (quux AND baz) OR fooO
5859
-version
59-
print version and exit
60+
print version and exit
6061
```
6162

6263
## Examples

cmd/catp/catp/app.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package catp
44
import (
55
"bufio"
66
"bytes"
7+
"context"
78
"encoding/json"
89
"errors"
910
"flag"
@@ -23,6 +24,7 @@ import (
2324
"github.com/bool64/progress"
2425
"github.com/klauspost/compress/zstd"
2526
gzip "github.com/klauspost/pgzip"
27+
"golang.org/x/time/rate"
2628
)
2729

2830
var versionExtra []string
@@ -57,7 +59,11 @@ type runner struct {
5759
lastStatusTime int64
5860
lastBytesUncompressed int64
5961

62+
rateLimit float64
63+
limiter *rate.Limiter
64+
6065
noProgress bool
66+
countLines bool
6167

6268
hasOptions bool
6369
options Options
@@ -198,10 +204,19 @@ func (r *runner) scanFile(filename string, rd io.Reader, out io.Writer) {
198204
lines := 0
199205
buf := make([]byte, 64*1024)
200206

207+
linesPush := 1000
208+
if r.rateLimit < 100 {
209+
linesPush = 1
210+
}
211+
201212
for s.Scan() {
202213
lines++
203214

204-
if lines >= 1000 {
215+
if r.limiter != nil {
216+
_ = r.limiter.Wait(context.Background()) //nolint:errcheck // No failure condition here.
217+
}
218+
219+
if lines >= linesPush {
205220
atomic.AddInt64(&r.currentLines, int64(lines))
206221
lines = 0
207222
}
@@ -390,7 +405,11 @@ func (r *runner) cat(filename string) (err error) { //nolint:gocyclo
390405
})
391406
}
392407

393-
if len(r.pass) > 0 || len(r.skip) > 0 || r.parallel > 1 || r.hasOptions {
408+
if r.rateLimit > 0 {
409+
r.limiter = rate.NewLimiter(rate.Limit(r.rateLimit), 100)
410+
}
411+
412+
if len(r.pass) > 0 || len(r.skip) > 0 || r.parallel > 1 || r.hasOptions || r.countLines || r.rateLimit > 0 {
394413
r.scanFile(filename, rd, out)
395414
} else {
396415
r.readFile(rd, out)
@@ -502,6 +521,8 @@ func Main(options ...func(o *Options)) error { //nolint:funlen,cyclop,gocognit,g
502521
memProfile := flag.String("dbg-mem-prof", "", "write heap profile to file after 10 seconds")
503522
output := flag.String("output", "", "output to file (can have .gz or .zst ext for compression) instead of STDOUT")
504523
flag.BoolVar(&r.noProgress, "no-progress", false, "disable progress printing")
524+
flag.BoolVar(&r.countLines, "l", false, "count lines")
525+
flag.Float64Var(&r.rateLimit, "rate-limit", 0, "output rate limit lines per second")
505526
progressJSON := flag.String("progress-json", "", "write current progress to a file")
506527
ver := flag.Bool("version", false, "print version and exit")
507528

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
module github.com/bool64/progress
22

3-
go 1.22
3+
go 1.23.0
44

55
require (
66
github.com/DataDog/zstd v1.5.7
77
github.com/bool64/dev v0.2.40
88
github.com/klauspost/compress v1.18.0
99
github.com/klauspost/pgzip v1.2.6
10+
golang.org/x/time v0.12.0
1011
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zt
66
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
77
github.com/klauspost/pgzip v1.2.6 h1:8RXeL5crjEUFnR2/Sn6GJNWtSQ3Dk8pq4CL3jvdDyjU=
88
github.com/klauspost/pgzip v1.2.6/go.mod h1:Ch1tH69qFZu15pkjo5kYi6mth2Zzwzt50oCQKQE9RUs=
9+
golang.org/x/time v0.12.0 h1:ScB/8o8olJvc+CQPWrK3fPZNfh7qgwCrY0zJmoEQLSE=
10+
golang.org/x/time v0.12.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=

0 commit comments

Comments
 (0)