Skip to content

Commit

Permalink
refactor: use pure go sqlite driver (#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaineK00n authored May 10, 2023
1 parent 7030ca7 commit 1fd1251
Show file tree
Hide file tree
Showing 19 changed files with 157 additions and 143 deletions.
17 changes: 7 additions & 10 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,10 @@ jobs:
go-version-file: go.mod
-
name: Run GoReleaser
run: |
docker run --rm \
-e CGO_ENABLED=1 \
-e GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} \
-v /var/run/docker.sock:/var/run/docker.sock \
-v `pwd`:/go/src/github.com/vulsio/goval-dictionary \
-w /go/src/github.com/vulsio/goval-dictionary \
ghcr.io/goreleaser/goreleaser-cross:v1.20 \
release --clean
uses: goreleaser/goreleaser-action@v4
with:
distribution: goreleaser
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7 changes: 3 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
name: golang-ci

run:
timeout: 10m
go: '1.18'

linters-settings:
revive:
# see https://github.com/mgechev/revive#available-rules for details.
Expand Down Expand Up @@ -35,6 +31,9 @@ linters-settings:
- name: unused-parameter
- name: unreachable-code
- name: redefines-builtin-id
staticcheck:
# https://staticcheck.io/docs/options#checks
checks: ["all", "-SA1019"]
# errcheck:
#exclude: /path/to/file.txt
misspell:
Expand Down
35 changes: 2 additions & 33 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,46 +4,15 @@ release:
owner: vulsio
name: goval-dictionary
env:
- GO111MODULE=on
- CGO_ENABLED=1
- CGO_ENABLED=0
builds:
- id: linux-amd64
- id: goval-dictionary
goos:
- linux
goarch:
- amd64
env:
- CC=x86_64-linux-gnu-gcc
main: .
ldflags: -s -w -X github.com/vulsio/goval-dictionary/config.Version={{.Version}} -X github.com/vulsio/goval-dictionary/config.Revision={{.Commit}}
binary: goval-dictionary
- id: linux-arm64
goos:
- linux
goarch:
- arm64
env:
- CC=aarch64-linux-gnu-gcc
main: .
ldflags: -s -w -X github.com/vulsio/goval-dictionary/config.Version={{.Version}} -X github.com/vulsio/goval-dictionary/config.Revision={{.Commit}}
binary: goval-dictionary
- id: windows-amd64
goos:
- windows
goarch:
- amd64
env:
- CC=x86_64-w64-mingw32-gcc
main: .
ldflags: -s -w -X github.com/vulsio/goval-dictionary/config.Version={{.Version}} -X github.com/vulsio/goval-dictionary/config.Revision={{.Commit}}
binary: goval-dictionary
- id: windows-arm64
goos:
- windows
goarch:
- arm64
env:
- CC=/llvm-mingw/bin/aarch64-w64-mingw32-gcc
main: .
ldflags: -s -w -X github.com/vulsio/goval-dictionary/config.Version={{.Version}} -X github.com/vulsio/goval-dictionary/config.Revision={{.Commit}}
binary: goval-dictionary
Expand Down
16 changes: 3 additions & 13 deletions GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,22 @@ VERSION := $(shell git describe --tags --abbrev=0)
REVISION := $(shell git rev-parse --short HEAD)
LDFLAGS := -X 'github.com/vulsio/goval-dictionary/config.Version=$(VERSION)' \
-X 'github.com/vulsio/goval-dictionary/config.Revision=$(REVISION)'
GO := GO111MODULE=on go
GO_OFF := GO111MODULE=off go
GO := CGO_ENABLED=0 go

all: build test

build: main.go
$(GO) build -a -ldflags "$(LDFLAGS)" -o goval-dictionary $<

b: main.go
$(GO) build -ldflags "$(LDFLAGS)" -o goval-dictionary $<

install: main.go
$(GO) install -ldflags "$(LDFLAGS)"

lint:
$(GO) install github.com/mgechev/revive@latest
go install github.com/mgechev/revive@latest
revive -config ./.revive.toml -formatter plain $(PKGS)

golangci:
$(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
golangci-lint run

vet:
Expand All @@ -57,9 +53,6 @@ vet:
fmt:
gofmt -s -w $(SRCS)

mlint:
$(foreach file,$(SRCS),gometalinter $(file) || exit;)

fmtcheck:
$(foreach file,$(SRCS),gofmt -s -d $(file);)

Expand All @@ -68,9 +61,6 @@ pretest: lint vet fmtcheck
test: pretest
$(GO) test -cover -v ./... || exit;

unused:
$(foreach pkg,$(PKGS),unused $(pkg);)

cov:
@ go get -v github.com/axw/gocov/gocov
@ go get golang.org/x/tools/cmd/cover
Expand Down
4 changes: 2 additions & 2 deletions commands/fetch-alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func fetchAlpine(_ *cobra.Command, args []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
Expand Down
10 changes: 2 additions & 8 deletions commands/fetch-amazon.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,13 @@ func fetchAmazon(_ *cobra.Command, args []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
defer func() {
err := driver.CloseDB()
if err != nil {
log15.Error("Failed to close DB", "err", err)
}
}()

fetchMeta, err := driver.GetFetchMeta()
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions commands/fetch-debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,13 @@ func fetchDebian(_ *cobra.Command, args []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
defer func() {
_ = driver.CloseDB()
}()

fetchMeta, err := driver.GetFetchMeta()
if err != nil {
Expand Down
10 changes: 2 additions & 8 deletions commands/fetch-fedora.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,13 @@ func fetchFedora(_ *cobra.Command, args []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
defer func() {
err := driver.CloseDB()
if err != nil {
log15.Error("Failed to close DB", "err", err)
}
}()

fetchMeta, err := driver.GetFetchMeta()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions commands/fetch-oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func fetchOracle(_ *cobra.Command, args []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions commands/fetch-redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ func fetchRedHat(_ *cobra.Command, args []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions commands/fetch-suse.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ func fetchSUSE(_ *cobra.Command, args []string) (err error) {
return xerrors.Errorf("Specify SUSE type to fetch. Available SUSE Type: opensuse, opensuse-leap, suse-enterprise-server, suse-enterprise-desktop")
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions commands/fetch-ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@ func fetchUbuntu(_ *cobra.Command, args []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
Expand Down
8 changes: 4 additions & 4 deletions commands/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,12 @@ func executeSelect(_ *cobra.Command, args []string) error {
}
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
return xerrors.Errorf("Failed to open DB. Close DB connection before select. err: %w", err)
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return err
return xerrors.Errorf("Failed to open DB. err: %w", err)
}

fetchMeta, err := driver.GetFetchMeta()
Expand Down
6 changes: 3 additions & 3 deletions commands/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ func executeServer(_ *cobra.Command, _ []string) (err error) {
return xerrors.Errorf("Failed to SetLogger. err: %w", err)
}

driver, locked, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
driver, err := db.NewDB(viper.GetString("dbtype"), viper.GetString("dbpath"), viper.GetBool("debug-sql"), db.Option{})
if err != nil {
if locked {
return xerrors.Errorf("Failed to initialize DB. Close DB connection before fetching. err: %w", err)
if xerrors.Is(err, db.ErrDBLocked) {
return xerrors.Errorf("Failed to open DB. Close DB connection before fetching. err: %w", err)
}
return xerrors.Errorf("Failed to open DB. err: %w", err)
}
Expand Down
21 changes: 9 additions & 12 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// DB is interface for a database driver
type DB interface {
Name() string
OpenDB(string, string, bool, Option) (bool, error)
OpenDB(string, string, bool, Option) error
CloseDB() error
MigrateDB() error

Expand All @@ -34,30 +34,27 @@ type Option struct {
}

// NewDB return DB accessor.
func NewDB(dbType, dbPath string, debugSQL bool, option Option) (driver DB, locked bool, err error) {
func NewDB(dbType, dbPath string, debugSQL bool, option Option) (driver DB, err error) {
if driver, err = newDB(dbType); err != nil {
return driver, false, xerrors.Errorf("Failed to new db. err: %w", err)
return driver, xerrors.Errorf("Failed to new db. err: %w", err)
}

if locked, err := driver.OpenDB(dbType, dbPath, debugSQL, option); err != nil {
if locked {
return nil, true, err
}
return nil, false, err
if err := driver.OpenDB(dbType, dbPath, debugSQL, option); err != nil {
return nil, xerrors.Errorf("Failed to open db. err: %w", err)
}

isV1, err := driver.IsGovalDictModelV1()
if err != nil {
return nil, false, xerrors.Errorf("Failed to IsGovalDictModelV1. err: %w", err)
return nil, xerrors.Errorf("Failed to IsGovalDictModelV1. err: %w", err)
}
if isV1 {
return nil, false, xerrors.New("Failed to NewDB. Since SchemaVersion is incompatible, delete Database and fetch again.")
return nil, xerrors.New("Failed to NewDB. Since SchemaVersion is incompatible, delete Database and fetch again.")
}

if err := driver.MigrateDB(); err != nil {
return driver, false, xerrors.Errorf("Failed to migrate db. err: %w", err)
return driver, xerrors.Errorf("Failed to migrate db. err: %w", err)
}
return driver, false, nil
return driver, nil
}

func newDB(dbType string) (DB, error) {
Expand Down
Loading

0 comments on commit 1fd1251

Please sign in to comment.