Skip to content

Commit 1995ce9

Browse files
committed
adds go releaser
Signed-off-by: Bruno Calza <brunoangelicalza@gmail.com>
1 parent 8f862a7 commit 1995ce9

File tree

6 files changed

+192
-10
lines changed

6 files changed

+192
-10
lines changed

.github/workflows/binaries.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: Generate binaries
2+
on:
3+
release:
4+
types:
5+
- created
6+
permissions:
7+
contents: write
8+
jobs:
9+
binaries:
10+
runs-on: ubuntu-latest
11+
container:
12+
image: ghcr.io/goreleaser/goreleaser-cross:v1.18
13+
steps:
14+
- name: Checkout repo
15+
uses: actions/checkout@v3
16+
with:
17+
submodules: 'true'
18+
- name: Fetch repo
19+
run: git fetch --prune --unshallow
20+
- name: Install Go
21+
uses: actions/setup-go@v3
22+
with:
23+
go-version: v1.21.x
24+
- name: Release
25+
uses: goreleaser/goreleaser-action@v4
26+
with:
27+
distribution: goreleaser
28+
version: latest
29+
args: release --clean
30+
env:
31+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

cmd.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
)
88

99
func newExportCommand() *cli.Command {
10-
var upload bool
10+
var upload, decompress bool
1111
var tables *cli.StringSlice
1212
var machine, output string
1313
return &cli.Command{
@@ -41,17 +41,25 @@ func newExportCommand() *cli.Command {
4141
Destination: &output,
4242
Value: ".",
4343
},
44-
&cli.StringFlag{
45-
Name: "machine",
46-
Category: "REQUIRED:",
47-
Usage: "machine's hash",
48-
DefaultText: "empty",
49-
Destination: &machine,
50-
Value: "",
44+
&cli.BoolFlag{
45+
Name: "decompress",
46+
Aliases: []string{"d"},
47+
Category: "OPTIONAL:",
48+
Usage: "Decompress a SQLite database compressed using zstd",
49+
DefaultText: "false",
50+
Destination: &decompress,
51+
Value: false,
5152
},
5253
},
5354
Action: func(cCtx *cli.Context) error {
5455
dbPath := cCtx.Args().First()
56+
if decompress {
57+
var err error
58+
dbPath, err = Decompress(dbPath)
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
}
5563

5664
db, err := NewSQLite(dbPath)
5765
if err != nil {
@@ -67,7 +75,6 @@ func newExportCommand() *cli.Command {
6775
}
6876

6977
tables := cCtx.StringSlice("tables")
70-
7178
exporter := NewDatabaseExporter(db, sink, output)
7279
if len(tables) > 0 {
7380
if err := exporter.ExportTables(cCtx.Context, tables); err != nil {

go.mod

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ module github.com/tablelandnetwork/sqlite-exporter
33
go 1.21.0
44

55
require (
6+
github.com/DataDog/zstd v1.5.5
67
github.com/jmoiron/sqlx v1.3.5
78
github.com/marcboeker/go-duckdb v1.6.1
89
github.com/mattn/go-sqlite3 v1.14.22
910
github.com/parquet-go/parquet-go v0.20.1
11+
github.com/pkg/errors v0.9.1
1012
github.com/stretchr/testify v1.8.4
1113
github.com/urfave/cli/v2 v2.27.1
14+
golang.org/x/sync v0.7.0
1215
golang.org/x/text v0.14.0
1316
)
1417

@@ -34,7 +37,6 @@ require (
3437
github.com/zeebo/xxh3 v1.0.2 // indirect
3538
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
3639
golang.org/x/mod v0.17.0 // indirect
37-
golang.org/x/sync v0.7.0 // indirect
3840
golang.org/x/sys v0.19.0 // indirect
3941
golang.org/x/tools v0.20.0 // indirect
4042
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
2+
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
13
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
24
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
35
github.com/apache/arrow/go/v14 v14.0.2 h1:N8OkaJEOfI3mEZt07BIkvo4sC6XDbL+48MBPWO5IONw=
@@ -44,6 +46,8 @@ github.com/parquet-go/parquet-go v0.20.1 h1:r5UqeMqyH2DrahZv6dlT41hH2NpS2F8atJWm
4446
github.com/parquet-go/parquet-go v0.20.1/go.mod h1:4YfUo8TkoGoqwzhA/joZKZ8f77wSMShOLHESY4Ys0bY=
4547
github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ=
4648
github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
49+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
50+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
4751
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4852
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4953
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=

goreleaser.yml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# goreleaser.yml
2+
before:
3+
hooks:
4+
- go mod tidy
5+
6+
env:
7+
- CGO_ENABLED=1
8+
9+
project_name: sqlite-exporter
10+
11+
builds:
12+
- id: sqlite-exporter-darwin-amd64
13+
binary: sqlite-exporter
14+
main: .
15+
goarch:
16+
- amd64
17+
goos:
18+
- darwin
19+
env:
20+
- CC=o64-clang
21+
- CXX=o64-clang++
22+
flags:
23+
- -trimpath
24+
ldflags:
25+
- -s -w -X main.version={{.Version}} -X main.commit={{.FullCommit}} -X main.date={{.CommitDate}}
26+
- id: sqlite-exporter-darwin-arm64
27+
binary: sqlite-exporter
28+
main: ./cmd/sqlite-exporter
29+
goarch:
30+
- arm64
31+
goos:
32+
- darwin
33+
env:
34+
- CC=oa64-clang
35+
- CXX=oa64-clang++
36+
flags:
37+
- -trimpath
38+
ldflags:
39+
- -s -w -X main.version={{.Version}} -X main.commit={{.FullCommit}} -X main.date={{.CommitDate}}
40+
- id: sqlite-exporter-linux-amd64
41+
binary: sqlite-exporter
42+
main: ./cmd/sqlite-exporter
43+
goarch:
44+
- amd64
45+
goos:
46+
- linux
47+
env:
48+
- CC=x86_64-linux-gnu-gcc
49+
- CXX=x86_64-linux-gnu-g++
50+
flags:
51+
- -trimpath
52+
ldflags:
53+
- -s -w -X main.version={{.Version}} -X main.commit={{.FullCommit}} -X main.date={{.CommitDate}}
54+
- id: sqlite-exporter-linux-arm64
55+
binary: sqlite-exporter
56+
main: ./cmd/sqlite-exporter
57+
goarch:
58+
- arm64
59+
goos:
60+
- linux
61+
env:
62+
- CC=aarch64-linux-gnu-gcc
63+
- CXX=aarch64-linux-gnu-g++
64+
flags:
65+
- -trimpath
66+
ldflags:
67+
- -s -w -X main.version={{.Version}} -X main.commit={{.FullCommit}} -X main.date={{.CommitDate}}
68+
69+
archives:
70+
- id: sqlite-exporter-archive
71+
format: tar.gz
72+
files:
73+
- none*
74+
builds:
75+
- sqlite-exporter-darwin-amd64
76+
- sqlite-exporter-darwin-arm64
77+
- sqlite-exporter-linux-amd64
78+
- sqlite-exporter-linux-arm64
79+
name_template: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"
80+
81+
checksum:
82+
disable: true

zstd.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"io"
7+
"os"
8+
"path/filepath"
9+
"strings"
10+
11+
"github.com/DataDog/zstd"
12+
"github.com/pkg/errors"
13+
"golang.org/x/sync/errgroup"
14+
)
15+
16+
// Decompress decompresses a zstd file.
17+
func Decompress(path string) (string, error) {
18+
file, err := os.Open(path)
19+
if err != nil {
20+
return "", fmt.Errorf("open file: %s", err)
21+
}
22+
pr, pw := io.Pipe()
23+
gzR := zstd.NewReader(file)
24+
25+
errs := errgroup.Group{}
26+
errs.Go(func() error {
27+
if _, err := io.Copy(pw, gzR); err != nil {
28+
return errors.Errorf("copy to writer: %s", err)
29+
}
30+
31+
if err := gzR.Close(); err != nil {
32+
return errors.Errorf("closing reader: %s", err)
33+
}
34+
if err := pw.Close(); err != nil {
35+
return errors.Errorf("closing pipe writer: %s", err)
36+
}
37+
38+
return nil
39+
})
40+
41+
newFilepath := strings.TrimSuffix(path, "."+filepath.Ext(path))
42+
df, err := os.OpenFile(newFilepath, os.O_CREATE|os.O_WRONLY, 0o755)
43+
if err != nil {
44+
return "", errors.Errorf("open new file: %s", err)
45+
}
46+
47+
writer := bufio.NewWriter(df)
48+
if _, err := io.Copy(writer, pr); err != nil {
49+
return "", errors.Errorf("copy dest file: %s", err)
50+
}
51+
52+
if err := errs.Wait(); err != nil {
53+
return "", errors.Errorf("errgroup wait: %s", err)
54+
}
55+
return newFilepath, nil
56+
}

0 commit comments

Comments
 (0)