Skip to content

Commit

Permalink
refactor: use multierror (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 authored Feb 9, 2022
1 parent 01187ea commit 9cd0802
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
41 changes: 33 additions & 8 deletions cmd/rpmdb/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ import (
"fmt"
"log"

multierror "github.com/hashicorp/go-multierror"
rpmdb "github.com/knqyf263/go-rpmdb/pkg"
)

func main() {
db, err := rpmdb.Open("./rpmdb.sqlite")
if err != nil {
db, err = rpmdb.Open("./Packages.db")
}
if err != nil {
db, err = rpmdb.Open("./Packages")
if err := run(); err != nil {
log.Fatal(err)
}
}

func run() error {
db, err := detectDB()
if err != nil {
log.Fatal(err)
return err
}
pkgList, err := db.ListPackages()
if err != nil {
log.Fatal(err)
return err
}

fmt.Println("Packages:")
Expand All @@ -34,4 +34,29 @@ func main() {
fmt.Printf("\t%+v\n", *pkg)
}
fmt.Printf("[Total Packages: %d]\n", len(pkgList))

return nil
}

func detectDB() (*rpmdb.RpmDB, error) {
var result error
db, err := rpmdb.Open("./rpmdb.sqlite")
if err == nil {
return db, nil
}
result = multierror.Append(result, err)

db, err = rpmdb.Open("./Packages.db")
if err == nil {
return db, nil
}
result = multierror.Append(result, err)

db, err = rpmdb.Open("./Packages")
if err == nil {
return db, nil
}
result = multierror.Append(result, err)

return nil, result
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/knqyf263/go-rpmdb
go 1.14

require (
github.com/hashicorp/go-multierror v1.1.1
github.com/stretchr/testify v1.7.0
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1
modernc.org/sqlite v1.14.5
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ github.com/google/go-cmp v0.5.3 h1:x95R7cp+rSeeqAMI2knLtQ0DKlaBhv2NrtrOvafPHRo=
github.com/google/go-cmp v0.5.3/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
Expand Down

0 comments on commit 9cd0802

Please sign in to comment.