From 75b39cfbf7793521fe307e11c4775fd92a34ce1e Mon Sep 17 00:00:00 2001 From: Luke Granger-Brown Date: Mon, 10 Oct 2022 00:49:44 +0100 Subject: [PATCH] bcacheup: use nixstore.Open --- go/nix/bcacheup/bcacheup.go | 5 ++--- go/nix/nixstore/default.nix | 3 +++ go/nix/nixstore/nixstore.go | 26 ++++++++++++++++++++++++++ go/nix/nixstore/sqlitestore.go | 2 +- 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 go/nix/nixstore/nixstore.go diff --git a/go/nix/bcacheup/bcacheup.go b/go/nix/bcacheup/bcacheup.go index 5bbbd51143..e44a3a6eb5 100644 --- a/go/nix/bcacheup/bcacheup.go +++ b/go/nix/bcacheup/bcacheup.go @@ -31,7 +31,6 @@ import ( var ( blobURLFlag = flag.String("cache_url", "", "Cache URL") - storeDBFlag = flag.String("nix_store_db", nixstore.DefaultStoreDB, "Path to the nix store database") stateSummaryIntervalFlag = flag.Duration("state_summary_interval", 10*time.Second, "Time between state summary outputs.") ) @@ -156,7 +155,7 @@ func (t *stateTracker) StateSummary() string { type uploader struct { bucket *blob.Bucket - store *nixstore.DB + store nixstore.Store storePath string st stateTracker @@ -421,7 +420,7 @@ func main() { } defer bucket.Close() - store, err := nixstore.Open(*storeDBFlag) + store, err := nixstore.Open() if err != nil { log.Fatalf("opening Nix store: %v", err) } diff --git a/go/nix/nixstore/default.nix b/go/nix/nixstore/default.nix index 241d82c63b..056f8aa5a8 100644 --- a/go/nix/nixstore/default.nix +++ b/go/nix/nixstore/default.nix @@ -7,10 +7,13 @@ depot.third_party.buildGo.package { name = "nixstore"; path = "hg.lukegb.com/lukegb/depot/go/nix/nixstore"; srcs = [ + ./nixstore.go + ./remotestore.go ./sqlitestore.go ]; deps = with depot; [ go.nix.nar.narinfo + go.nix.nixwire third_party.gopkgs."github.com".mattn.go-sqlite3 ]; } diff --git a/go/nix/nixstore/nixstore.go b/go/nix/nixstore/nixstore.go new file mode 100644 index 0000000000..048a59b453 --- /dev/null +++ b/go/nix/nixstore/nixstore.go @@ -0,0 +1,26 @@ +package nixstore + +import ( + "fmt" + + "hg.lukegb.com/lukegb/depot/go/nix/nar/narinfo" +) + +type Store interface { + NARInfo(storePath string) (*narinfo.NarInfo, error) + Close() error +} + +func Open() (Store, error) { + s, errDmn := OpenDaemon(DaemonSock) + if errDmn == nil { + return s, nil + } + + s2, errDB := OpenDB(DefaultStoreDB) + if errDB == nil { + return s2, nil + } + + return nil, fmt.Errorf("failed to open daemon or DB: %v / %v", errDmn, errDB) +} diff --git a/go/nix/nixstore/sqlitestore.go b/go/nix/nixstore/sqlitestore.go index 08f2d68fba..e605824e68 100644 --- a/go/nix/nixstore/sqlitestore.go +++ b/go/nix/nixstore/sqlitestore.go @@ -119,7 +119,7 @@ func (d *DB) Close() error { return d.db.Close() } -func Open(dbPath string) (*DB, error) { +func OpenDB(dbPath string) (*DB, error) { sqlDB, err := sql.Open("sqlite3", dbPath) if err != nil { return nil, err