Added logic to open sqlite database
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
@@ -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=
|
||||
|
||||
@@ -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)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user