Skip to content

Commit

Permalink
adds go releaser
Browse files Browse the repository at this point in the history
Signed-off-by: Bruno Calza <[email protected]>
  • Loading branch information
brunocalza committed May 10, 2024
1 parent 8f862a7 commit 628b6b7
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 10 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/binaries.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Generate binaries
on:
release:
types:
- created
permissions:
contents: write
jobs:
binaries:
runs-on: ubuntu-latest
container:
image: ghcr.io/goreleaser/goreleaser-cross:v1.18
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
submodules: 'true'
- name: Fetch repo
run: git fetch --prune --unshallow
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: v1.21.x
- name: Release
uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
25 changes: 16 additions & 9 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func newExportCommand() *cli.Command {
var upload bool
var upload, decompress bool
var tables *cli.StringSlice
var machine, output string
return &cli.Command{
Expand Down Expand Up @@ -41,17 +41,25 @@ func newExportCommand() *cli.Command {
Destination: &output,
Value: ".",
},
&cli.StringFlag{
Name: "machine",
Category: "REQUIRED:",
Usage: "machine's hash",
DefaultText: "empty",
Destination: &machine,
Value: "",
&cli.BoolFlag{
Name: "decompress",
Aliases: []string{"d"},
Category: "OPTIONAL:",
Usage: "Decompress a SQLite database compressed using zstd",
DefaultText: "false",
Destination: &decompress,
Value: false,
},
},
Action: func(cCtx *cli.Context) error {
dbPath := cCtx.Args().First()
if decompress {
var err error
dbPath, err = Decompress(dbPath)
if err != nil {
log.Fatal(err)
}
}

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

tables := cCtx.StringSlice("tables")

exporter := NewDatabaseExporter(db, sink, output)
if len(tables) > 0 {
if err := exporter.ExportTables(cCtx.Context, tables); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ module github.com/tablelandnetwork/sqlite-exporter
go 1.21.0

require (
github.com/DataDog/zstd v1.5.5
github.com/jmoiron/sqlx v1.3.5
github.com/marcboeker/go-duckdb v1.6.1
github.com/mattn/go-sqlite3 v1.14.22
github.com/parquet-go/parquet-go v0.20.1
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.4
github.com/urfave/cli/v2 v2.27.1
golang.org/x/sync v0.7.0
golang.org/x/text v0.14.0
)

Expand All @@ -34,7 +37,6 @@ require (
github.com/zeebo/xxh3 v1.0.2 // indirect
golang.org/x/exp v0.0.0-20240416160154-fe59bbe5cc7f // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/tools v0.20.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/DataDog/zstd v1.5.5 h1:oWf5W7GtOLgp6bciQYDmhHHjdhYkALu6S/5Ni9ZgSvQ=
github.com/DataDog/zstd v1.5.5/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig=
github.com/apache/arrow/go/v14 v14.0.2 h1:N8OkaJEOfI3mEZt07BIkvo4sC6XDbL+48MBPWO5IONw=
Expand Down Expand Up @@ -44,6 +46,8 @@ github.com/parquet-go/parquet-go v0.20.1 h1:r5UqeMqyH2DrahZv6dlT41hH2NpS2F8atJWm
github.com/parquet-go/parquet-go v0.20.1/go.mod h1:4YfUo8TkoGoqwzhA/joZKZ8f77wSMShOLHESY4Ys0bY=
github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ=
github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
Expand Down
82 changes: 82 additions & 0 deletions goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# goreleaser.yml
before:
hooks:
- go mod tidy

env:
- CGO_ENABLED=1

project_name: sqlite-exporter

builds:
- id: sqlite-exporter-darwin-amd64
binary: sqlite-exporter
main: .
goarch:
- amd64
goos:
- darwin
env:
- CC=o64-clang
- CXX=o64-clang++
flags:
- -trimpath
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.FullCommit}} -X main.date={{.CommitDate}}
- id: sqlite-exporter-darwin-arm64
binary: sqlite-exporter
main: .
goarch:
- arm64
goos:
- darwin
env:
- CC=oa64-clang
- CXX=oa64-clang++
flags:
- -trimpath
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.FullCommit}} -X main.date={{.CommitDate}}
- id: sqlite-exporter-linux-amd64
binary: sqlite-exporter
main: .
goarch:
- amd64
goos:
- linux
env:
- CC=x86_64-linux-gnu-gcc
- CXX=x86_64-linux-gnu-g++
flags:
- -trimpath
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.FullCommit}} -X main.date={{.CommitDate}}
- id: sqlite-exporter-linux-arm64
binary: sqlite-exporter
main: .
goarch:
- arm64
goos:
- linux
env:
- CC=aarch64-linux-gnu-gcc
- CXX=aarch64-linux-gnu-g++
flags:
- -trimpath
ldflags:
- -s -w -X main.version={{.Version}} -X main.commit={{.FullCommit}} -X main.date={{.CommitDate}}

archives:
- id: sqlite-exporter-archive
format: tar.gz
files:
- none*
builds:
- sqlite-exporter-darwin-amd64
- sqlite-exporter-darwin-arm64
- sqlite-exporter-linux-amd64
- sqlite-exporter-linux-arm64
name_template: "{{ .ProjectName }}-{{ .Os }}-{{ .Arch }}"

checksum:
disable: true
56 changes: 56 additions & 0 deletions zstd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package main

import (
"bufio"
"fmt"
"io"
"os"
"path/filepath"
"strings"

"github.com/DataDog/zstd"
"github.com/pkg/errors"
"golang.org/x/sync/errgroup"
)

// Decompress decompresses a zstd file.
func Decompress(path string) (string, error) {
file, err := os.Open(path)
if err != nil {
return "", fmt.Errorf("open file: %s", err)
}
pr, pw := io.Pipe()
gzR := zstd.NewReader(file)

errs := errgroup.Group{}
errs.Go(func() error {
if _, err := io.Copy(pw, gzR); err != nil {
return errors.Errorf("copy to writer: %s", err)
}

if err := gzR.Close(); err != nil {
return errors.Errorf("closing reader: %s", err)
}
if err := pw.Close(); err != nil {
return errors.Errorf("closing pipe writer: %s", err)
}

return nil
})

newFilepath := strings.TrimSuffix(path, "."+filepath.Ext(path))
df, err := os.OpenFile(newFilepath, os.O_CREATE|os.O_WRONLY, 0o755)
if err != nil {
return "", errors.Errorf("open new file: %s", err)
}

writer := bufio.NewWriter(df)
if _, err := io.Copy(writer, pr); err != nil {
return "", errors.Errorf("copy dest file: %s", err)
}

if err := errs.Wait(); err != nil {
return "", errors.Errorf("errgroup wait: %s", err)
}
return newFilepath, nil
}

0 comments on commit 628b6b7

Please sign in to comment.