@@ -10,14 +10,14 @@ import (
1010
1111 "github.com/blevesearch/bleve"
1212 "github.com/olekukonko/tablewriter"
13+ "github.com/policy-hub/policy-hub-cli/pkg/helpers"
1314 "github.com/policy-hub/policy-hub-cli/pkg/metaconfig"
1415)
1516
1617// IndexVersion indicates the version of the search index.
1718// This is used to migrate between index versions.
1819const IndexVersion = "v1"
1920
20-
2121type Engine struct {
2222 index bleve.Index
2323}
@@ -81,7 +81,7 @@ func (e *Engine) Close() error {
8181
8282// constructIndex builds a search index
8383func constructIndex () (bleve.Index , error ) {
84- cacheDir := cacheDirectory ()
84+ cacheDir := helpers . IndexPath ()
8585 if _ , err := os .Stat (cacheDir ); os .IsNotExist (err ) {
8686 return setupIndexDirectory ()
8787 }
@@ -93,7 +93,7 @@ func constructIndex() (bleve.Index, error) {
9393func loadIndex () (bleve.Index , error ) {
9494 index , err := bleve .Open (indexDirectory ())
9595 if err == bleve .ErrorIndexPathDoesNotExist {
96- index , err := constructIndex ();
96+ index , err := constructIndex ()
9797 if err != nil {
9898 return nil , fmt .Errorf ("construct index: %w" , err )
9999 }
@@ -108,7 +108,7 @@ func loadIndex() (bleve.Index, error) {
108108
109109// setupIndexDirectory setups the index directory
110110func setupIndexDirectory () (bleve.Index , error ) {
111- cacheDir := cacheDirectory ()
111+ cacheDir := helpers . IndexPath ()
112112 if err := os .MkdirAll (cacheDir , os .ModePerm ); err != nil {
113113 return nil , fmt .Errorf ("make search dir: %w" , err )
114114 }
@@ -117,7 +117,7 @@ func setupIndexDirectory() (bleve.Index, error) {
117117 if err != nil {
118118 return nil , fmt .Errorf ("create version file: %w" , err )
119119 }
120-
120+
121121 mapping := bleve .NewIndexMapping ()
122122 index , err := bleve .New (indexDirectory (), mapping )
123123 if err != nil {
@@ -127,19 +127,7 @@ func setupIndexDirectory() (bleve.Index, error) {
127127 return index , nil
128128}
129129
130- // cacheDirectory returns the directory to cache policy-cli configs
131- func cacheDirectory () string {
132- const cacheDir = ".policy-hub"
133-
134- homeDir , _ := os .UserHomeDir ()
135-
136- directory := filepath .Join (homeDir , cacheDir )
137- directory = filepath .ToSlash (directory )
138-
139- return directory
140- }
141-
142130// indexDirectory returns the directory to store the search index in
143131func indexDirectory () string {
144- return filepath .Join (cacheDirectory (), "index" )
145- }
132+ return filepath .Join (helpers . IndexPath (), "index" )
133+ }
0 commit comments