Added logic to open sqlite database
This commit is contained in:
@@ -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