From 985a7bf0059276c27da7be61aa8443dd05bd5847 Mon Sep 17 00:00:00 2001 From: restitux Date: Wed, 13 Apr 2022 18:21:19 -0600 Subject: [PATCH] Added logic to open sqlite database --- go.mod | 5 ++++- go.sum | 2 ++ main.go | 40 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index a5f4ab9..eed49c0 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/restitux/ikinuki-server go 1.18 -require github.com/pelletier/go-toml/v2 v2.0.0-beta.8 +require ( + github.com/mattn/go-sqlite3 v1.14.12 + github.com/pelletier/go-toml/v2 v2.0.0-beta.8 +) diff --git a/go.sum b/go.sum index 6ee4343..e27fe31 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0= +github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU= github.com/pelletier/go-toml/v2 v2.0.0-beta.8 h1:dy81yyLYJDwMTifq24Oi/IslOslRrDSb3jwDggjz3Z0= github.com/pelletier/go-toml/v2 v2.0.0-beta.8/go.mod h1:r9LEWfGN8R5k0VXJ+0BkIe7MYkRdwZOjgMj2KwnJFUo= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= diff --git a/main.go b/main.go index b5b26db..c59efef 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,15 @@ package main import ( + "database/sql" "fmt" - toml "github.com/pelletier/go-toml/v2" "io/ioutil" "os" "path/filepath" + + _ "github.com/mattn/go-sqlite3" + + toml "github.com/pelletier/go-toml/v2" ) type Config struct { @@ -86,6 +90,36 @@ func get_config(config_dir string) (*Config, error) { } +func get_data_dir() string { + // Find data directory + + // Linux (XDG base directory specification compliant) + xdg_data_home := os.Getenv("XDG_DATA_HOME") + + if xdg_data_home != "" { + return filepath.Join(xdg_data_home, "ikinuki") + } else { + home := os.Getenv("HOME") + return filepath.Join(home, ".local", "share", "ikinuki") + } +} + +func open_database(data_dir string) (*sql.DB, error) { + err := os.MkdirAll(data_dir, 0755) + if err != nil { + return nil, fmt.Errorf("ERROR: could not create data directory: %v", err) + } + + db_path := filepath.Join(data_dir, "ikinuki-server.db") + + db, err := sql.Open("sqlite3", db_path) + if err != nil { + return nil, fmt.Errorf("ERROR: could not open database: %v", err) + } + + return db, nil +} + func scan_directory(directory string) { files, err := ioutil.ReadDir(directory) if err != nil { @@ -114,6 +148,10 @@ func main() { } fmt.Printf("Config: %v\n", config) + data_dir := get_data_dir() + + open_database(data_dir) + scan_directory(config.ScanDirectory) }