Repository: aquasecurity/trivy-db

Issue:
add bolt options to db.Init to allow multiple readers on the same db
Receive `bbolt.Options` in `db.Init` function to allow Readonly instances

Hints from issue comments:

Solution patch:
diff --git a/pkg/db/db.go b/pkg/db/db.go
index e9603dd..9d51f1a 100644
--- a/pkg/db/db.go
+++ b/pkg/db/db.go
@@ -52,10 +52,26 @@ type Operation interface {
 	RedHatNVRToCPEs(nvr string) (cpeIndices []int, err error)
 }
 
-type Config struct {
+type Config struct{}
+
+type Option func(*Options)
+
+type Options struct {
+	boltOptions *bolt.Options
 }
 
-func Init(dbDir string) (err error) {
+func WithBoltOptions(boltOpts *bolt.Options) Option {
+	return func(opts *Options) {
+		opts.boltOptions = boltOpts
+	}
+}
+
+func Init(dbDir string, opts ...Option) (err error) {
+	dbOptions := &Options{}
+	for _, opt := range opts {
+		opt(dbOptions)
+	}
+
 	if err = os.MkdirAll(dbDir, 0700); err != nil {
 		return xerrors.Errorf("failed to mkdir: %w", err)
 	}
@@ -69,12 +85,12 @@ func Init(dbDir string) (err error) {
 			if err = os.Remove(dbPath); err != nil {
 				return
 			}
-			db, err = bolt.Open(dbPath, 0600, nil)
+			db, err = bolt.Open(dbPath, 0600, dbOptions.boltOptions)
 		}
 		debug.SetPanicOnFault(false)
 	}()
 
-	db, err = bolt.Open(dbPath, 0600, nil)
+	db, err = bolt.Open(dbPath, 0600, dbOptions.boltOptions)
 	if err != nil {
 		return xerrors.Errorf("failed to open db: %w", err)
 	}



Test patch:
diff --git a/pkg/db/db_test.go b/pkg/db/db_test.go
index d71882a..a852827 100644
--- a/pkg/db/db_test.go
+++ b/pkg/db/db_test.go
@@ -7,6 +7,7 @@ import (
 	"testing"
 
 	"github.com/stretchr/testify/require"
+	"go.etcd.io/bbolt"
 
 	"github.com/aquasecurity/trivy-db/pkg/db"
 )
@@ -15,6 +16,7 @@ func TestInit(t *testing.T) {
 	tests := []struct {
 		name   string
 		dbPath string
+		dbOpts *bbolt.Options
 	}{
 		{
 			name:   "normal db",
@@ -43,7 +45,7 @@ func TestInit(t *testing.T) {
 				require.NoError(t, err)
 			}
 
-			err := db.Init(tmpDir)
+			err := db.Init(tmpDir, db.WithBoltOptions(tt.dbOpts))
 			require.NoError(t, err)
 		})
 	}