Skip to content

Commit 099a01b

Browse files
committed
initialize database on first run
1 parent bddd932 commit 099a01b

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

mada/init.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"crypto/sha256"
55
"embed"
66
"encoding/json"
7+
"errors"
78
"fmt"
89
"log"
910
"os"
@@ -12,9 +13,12 @@ import (
1213

1314
"github.com/blevesearch/bleve/v2"
1415
"github.com/everystreet/go-shapefile"
16+
"github.com/mitchellh/go-homedir"
1517
"github.com/twpayne/go-geom"
1618
)
1719

20+
var DATABASE_PATH string = filepath.Join(CreateConfigDir(), "mada.bleve")
21+
1822
type Shape struct {
1923
Type string `json:"type"`
2024
Bbox []float32 `json:"bbox"`
@@ -56,7 +60,7 @@ type Geometry struct {
5660
//go:embed shp/*
5761
var Assets embed.FS
5862

59-
func Init() {
63+
func Init() (bleve.Index, error) {
6064

6165
index, err := CreateOrOpenBleve()
6266

@@ -82,16 +86,18 @@ func Init() {
8286
parseShapefile(filename, index)
8387
}
8488

89+
return index, nil
8590
}
8691

8792
func CreateOrOpenBleve() (bleve.Index, error) {
88-
if _, err := os.Stat("mada.bleve"); os.IsNotExist(err) {
93+
if _, err := os.Stat(DATABASE_PATH); os.IsNotExist(err) {
8994
geometryMapping := bleve.NewDocumentDisabledMapping()
95+
9096
mapping := bleve.NewIndexMapping()
9197
mapping.DefaultMapping.AddSubDocumentMapping("geometry", geometryMapping)
92-
return bleve.New("mada.bleve", mapping)
98+
return bleve.New(DATABASE_PATH, mapping)
9399
}
94-
return bleve.Open("mada.bleve")
100+
return bleve.Open(DATABASE_PATH)
95101
}
96102

97103
func parseShapefile(name string, index bleve.Index) {
@@ -192,3 +198,15 @@ func parseShapefile(name string, index bleve.Index) {
192198
// Err() returns the first error encountered during calls to Record()
193199
scanner.Err()
194200
}
201+
202+
func CreateConfigDir() string {
203+
home, _ := homedir.Dir()
204+
path := filepath.Join(home, ".mada")
205+
if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
206+
err := os.Mkdir(path, os.ModePerm)
207+
if err != nil {
208+
panic(err)
209+
}
210+
}
211+
return path
212+
}

mada/search.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,14 @@ package mada
33
import (
44
"encoding/json"
55
"fmt"
6+
"os"
67

78
"github.com/blevesearch/bleve/v2"
89
"github.com/blevesearch/bleve/v2/search/highlight/highlighter/ansi"
910
)
1011

1112
func Search(term string, outputInJSON bool) {
12-
index, err := CreateOrOpenBleve()
13+
index, err := InitializeBleve()
1314

1415
if err != nil {
1516
panic(err)
@@ -36,3 +37,10 @@ func Search(term string, outputInJSON bool) {
3637

3738
fmt.Println(string(b))
3839
}
40+
41+
func InitializeBleve() (bleve.Index, error) {
42+
if _, err := os.Stat(DATABASE_PATH); os.IsNotExist(err) {
43+
return Init()
44+
}
45+
return bleve.Open(DATABASE_PATH)
46+
}

0 commit comments

Comments
 (0)