-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
36 lines (29 loc) · 884 Bytes
/
file.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
package api
import (
"database/sql"
"fmt"
"os"
"path"
"github.com/pkg/errors"
_ "github.com/mattn/go-sqlite3"
)
// OpenDocument opens the given MoneyWell document and returns an interface to the SQLite database
// therein.
func OpenDocument(moneywellPath string) (*sql.DB, error) {
fi, err := os.Stat(moneywellPath)
if err != nil {
return nil, errors.Wrapf(err, "failed to stat %s", moneywellPath)
}
switch mode := fi.Mode(); {
// Assume the `.moneywell` bundle was specified and resolve to the persistentStore therein.
case mode.IsDir():
moneywellPath = path.Join(moneywellPath, "StoreContent/persistentStore")
case mode.IsRegular():
// Attempt to access the path as-is.
}
database, err := sql.Open("sqlite3", fmt.Sprintf("%s?mode=ro", moneywellPath))
if err != nil {
return nil, errors.Wrapf(err, "failed to open database")
}
return database, nil
}