-
Notifications
You must be signed in to change notification settings - Fork 5
/
pivot.go
102 lines (86 loc) · 2.33 KB
/
pivot.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package pivot
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"time"
"github.com/ghetzel/pivot/v3/backends"
"github.com/ghetzel/pivot/v3/dal"
"github.com/ghetzel/pivot/v3/filter"
"github.com/ghodss/yaml"
)
// create handy type aliases to avoid importing from all over the place
type DB = backends.Backend
type Model = backends.Mapper
type Collection = dal.Collection
type Record = dal.Record
type RecordSet = dal.RecordSet
type Filter = filter.Filter
var MonitorCheckInterval = time.Duration(10) * time.Second
var NetrcFile = ``
func NewDatabaseWithOptions(connection string, options backends.ConnectOptions) (DB, error) {
if cs, err := dal.ParseConnectionString(connection); err == nil {
if NetrcFile != `` {
if err := cs.LoadCredentialsFromNetrc(NetrcFile); err != nil {
return nil, err
}
}
if backend, err := backends.MakeBackend(cs); err == nil {
// set indexer
if options.Indexer != `` {
if ics, err := dal.ParseConnectionString(options.Indexer); err == nil {
if NetrcFile != `` {
if err := ics.LoadCredentialsFromNetrc(NetrcFile); err != nil {
return nil, err
}
}
if err := backend.SetIndexer(ics); err != nil {
return nil, err
}
} else {
return nil, err
}
}
// TODO: add MultiIndexer if AdditionalIndexers is present
if !options.SkipInitialize {
if err := backend.Initialize(); err != nil {
return nil, err
}
}
return backend, nil
} else {
return nil, err
}
} else {
return nil, err
}
}
func NewDatabase(connection string) (DB, error) {
return NewDatabaseWithOptions(connection, backends.ConnectOptions{})
}
func LoadSchemataFromFile(filename string) ([]*dal.Collection, error) {
if file, err := os.Open(filename); err == nil {
var collections []*dal.Collection
switch ext := path.Ext(filename); ext {
case `.json`:
if err := json.NewDecoder(file).Decode(&collections); err != nil {
return nil, fmt.Errorf("decode error: %v", err)
}
case `.yml`, `.yaml`:
if data, err := ioutil.ReadAll(file); err == nil {
if err := yaml.Unmarshal(data, &collections); err != nil {
return nil, fmt.Errorf("decode error: %v", err)
}
} else {
return nil, err
}
default:
return nil, fmt.Errorf("Unrecognized file extension %s", ext)
}
return collections, nil
} else {
return nil, err
}
}