Skip to content

Commit 66c3cc9

Browse files
committed
When running the app, the config files must be specified on the command line, the file paths cannot be used anymore.
1 parent 34b7a57 commit 66c3cc9

14 files changed

Lines changed: 411 additions & 395 deletions

ROAD_TO_WS4SQL.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,27 @@ The version in this branch is a work in progress to slowly add features and (unf
55
# Changes
66

77
- SQLite is embedded via [mattn/go-sqlite3](https://github.com/mattn/go-sqlite3) and CGO. Should be way faster.
8-
- Target platforms (because of CGO) are now 6 (`win/amd64`, `macos/amd64`, `macos/arm64`, `linux/amd64`, `linux/arm64`, `linux/arm6`)
8+
- Target platforms (because of CGO) are now 6 (`win/amd64`, `macos/amd64`, `macos/arm64`, `linux/amd64`, `linux/arm64`, `linux/arm6`).
9+
- [**BREAKING**] When running the app, the config files must be specified on the command line, the file paths cannot be used anymore (there). This is described in the "Migration" section below. The file path is in the config file.
10+
- The only exception is a "simple case" to serve a file path without any config. This can be done with the new `--quick-db` parameter.
911

1012
# Migration
1113

12-
- Nothing needed for now :-)
14+
- For any `--db` and `--mem-db` switch that was used, an explicit YAML config file must be created. The format is the same, but there is a new section at the beginning:
15+
```yaml
16+
database:
17+
type: SQLITE # Only SQLITE for now. If omitted, defaults to SQLITE
18+
inMemory: false # If type = SQLITE. The db is a memory one? If omitted, defaults to false
19+
path: ".../test.db" # If type = SQLITE. The db file path.
20+
id: test # If omitted and !inMemory, calculates it from the file name (if type = SQLITE)
21+
disableWALMode: false # If type = SQLITE. Same as before, but moved here.
22+
readOnly: false # Same as before, but moved here.
23+
```
1324
1425
# Roadmap
1526
16-
1. Make it mandatory to use conf files for databases
17-
1. Support duckdb (and iron out all the incompatibilities)
1827
1. Support mariadb/mysql
28+
1. Support duckdb (and iron out all the incompatibilities)
1929
1. Support postgresql
2030
1. ...
2131
1. Profit!

src/authentication.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func parseAuth(db *db) {
105105
var err error
106106
b, err = hex.DecodeString(auth.ByCredentials[i].HashedPassword)
107107
if err != nil || len(b) != 32 {
108-
mllog.Fatalf("for db '%s', hashedPassword doesn't seem to be SHA256/hex.", db.Id)
108+
mllog.Fatalf("for db '%s', hashedPassword doesn't seem to be SHA256/hex.", *db.DatabaseDef.Id)
109109
}
110110
} else {
111111
bytes32 := sha256.Sum256([]byte(auth.ByCredentials[i].Password))

src/authentication_test.go

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@ func TestSetupAuthCreds(t *testing.T) {
3636
Port: 12321,
3737
Databases: []db{
3838
{
39-
Id: "test0",
40-
Path: "../test/test0.db",
41-
DisableWALMode: true,
39+
DatabaseDef: DatabaseDef{
40+
Id: Ptr("test0"),
41+
Path: Ptr("../test/test0.db"),
42+
DisableWALMode: true,
43+
},
4244
},
4345
{
44-
Id: "test1",
45-
Path: "../test/test1.db",
46-
DisableWALMode: true,
46+
DatabaseDef: DatabaseDef{
47+
Id: Ptr("test1"),
48+
Path: Ptr("../test/test1.db"),
49+
DisableWALMode: true,
50+
},
4751
Auth: &authr{
4852
Mode: "INLINE",
4953
ByCredentials: []credentialsCfg{
@@ -59,9 +63,11 @@ func TestSetupAuthCreds(t *testing.T) {
5963
},
6064
},
6165
{
62-
Id: "test2",
63-
Path: "../test/test2.db",
64-
DisableWALMode: true,
66+
DatabaseDef: DatabaseDef{
67+
Id: Ptr("test2"),
68+
Path: Ptr("../test/test2.db"),
69+
DisableWALMode: true,
70+
},
6571
InitStatements: []string{
6672
"CREATE TABLE AUTH (USER TEXT PRIMARY KEY, PASS TEXT)",
6773
"INSERT INTO AUTH VALUES ('_pietro', 'hey'), ('_paolo', 'ciao')",
@@ -322,9 +328,11 @@ func TestBASetupAuthCreds(t *testing.T) {
322328
Port: 12321,
323329
Databases: []db{
324330
{
325-
Id: "test1",
326-
Path: "../test/test1.db",
327-
DisableWALMode: true,
331+
DatabaseDef: DatabaseDef{
332+
Id: Ptr("test1"),
333+
Path: Ptr("../test/test1.db"),
334+
DisableWALMode: true,
335+
},
328336
Auth: &authr{
329337
Mode: "HTTP",
330338
ByCredentials: []credentialsCfg{
@@ -340,9 +348,11 @@ func TestBASetupAuthCreds(t *testing.T) {
340348
},
341349
},
342350
{
343-
Id: "test2",
344-
Path: "../test/test2.db",
345-
DisableWALMode: true,
351+
DatabaseDef: DatabaseDef{
352+
Id: Ptr("test2"),
353+
Path: Ptr("../test/test2.db"),
354+
DisableWALMode: true,
355+
},
346356
InitStatements: []string{
347357
"CREATE TABLE AUTH (USER TEXT PRIMARY KEY, PASS TEXT)",
348358
"INSERT INTO AUTH VALUES ('_pietro', 'hey'), ('_paolo', 'ciao')",
@@ -549,9 +559,11 @@ func TestCustomCodeSetup(t *testing.T) {
549559
Port: 12321,
550560
Databases: []db{
551561
{
552-
Id: "test1",
553-
Path: "../test/test1.db",
554-
DisableWALMode: true,
562+
DatabaseDef: DatabaseDef{
563+
Id: Ptr("test1"),
564+
Path: Ptr("../test/test1.db"),
565+
DisableWALMode: true,
566+
},
555567
Auth: &authr{
556568
Mode: "HTTP",
557569
CustomErrorCode: &errCode,
@@ -568,9 +580,11 @@ func TestCustomCodeSetup(t *testing.T) {
568580
},
569581
},
570582
{
571-
Id: "test2",
572-
Path: "../test/test2.db",
573-
DisableWALMode: true,
583+
DatabaseDef: DatabaseDef{
584+
Id: Ptr("test2"),
585+
Path: Ptr("../test/test2.db"),
586+
DisableWALMode: true,
587+
},
574588
InitStatements: []string{
575589
"CREATE TABLE AUTH (USER TEXT PRIMARY KEY, PASS TEXT)",
576590
"INSERT INTO AUTH VALUES ('_pietro', 'hey'), ('_paolo', 'ciao')",

src/cli.go

Lines changed: 34 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package main
1919
import (
2020
"flag"
2121
"os"
22-
"path/filepath"
2322
"strings"
2423

2524
mllog "github.com/proofrock/go-mylittlelogger"
@@ -49,9 +48,9 @@ func parseCLI() config {
4948

5049
// cli parameters
5150
var dbFiles arrayFlags
52-
fs.Var(&dbFiles, "db", "Repeatable; paths of file-based databases")
53-
var memDb arrayFlags
54-
fs.Var(&memDb, "mem-db", "Repeatable; config for memory-based databases (format: ID[:configFilePath])")
51+
fs.Var(&dbFiles, "db", "Repeatable; paths of config yamls")
52+
53+
quickDb := fs.String("quick-db", "", "Shortcut to simply open a sqlite db file (for quicker config)")
5554

5655
serveDir := fs.String("serve-dir", "", "A directory to serve with builtin HTTP server")
5756

@@ -70,80 +69,42 @@ func parseCLI() config {
7069

7170
var ret config
7271

73-
// Fail fast
74-
if len(dbFiles)+len(memDb) == 0 && *serveDir == "" {
75-
mllog.Fatal("no database and no dir to serve specified")
76-
}
77-
78-
for i := range dbFiles {
79-
// if there's no ":", second is empty ("")
80-
dbFile, yamlFile := splitOnColon(dbFiles[i])
81-
82-
// resolves '~'
83-
dbFile = expandHomeDir(dbFile, "database file")
84-
85-
dir := filepath.Dir(dbFile)
86-
87-
//strips the extension from the file name (see issue #11)
88-
id := strings.TrimSuffix(filepath.Base(dbFile), filepath.Ext(dbFile))
89-
90-
if len(id) == 0 {
91-
mllog.Fatal("base filename cannot be empty")
92-
}
93-
94-
if yamlFile == "" {
95-
yamlFile = filepath.Join(dir, id+".yaml")
96-
} else {
97-
yamlFile = expandHomeDir(yamlFile, "companion file")
72+
if *quickDb != "" {
73+
if len(dbFiles) > 0 {
74+
mllog.Fatal("--quick-db must be the only database configured, if present")
9875
}
9976

100-
var dbConfig db
101-
if fileExists(yamlFile) {
102-
cfgData, err := os.ReadFile(yamlFile)
103-
if err != nil {
104-
mllog.Fatal("in reading config file: ", err.Error())
77+
ret.Databases = append(ret.Databases, db{
78+
ConfigFilePath: "quick db setting",
79+
DatabaseDef: DatabaseDef{
80+
Type: Ptr("SQLITE"),
81+
Path: quickDb,
82+
},
83+
})
84+
} else if len(dbFiles) > 0 {
85+
for i := range dbFiles {
86+
yamlFile := expandHomeDir(dbFiles[i], "companion file")
87+
88+
var dbConfig db
89+
dbConfig.ConfigFilePath = yamlFile
90+
91+
if fileExists(yamlFile) {
92+
cfgData, err := os.ReadFile(yamlFile)
93+
if err != nil {
94+
mllog.Fatal("in reading config file: ", err.Error())
95+
}
96+
97+
if err = yaml.Unmarshal(cfgData, &dbConfig); err != nil {
98+
mllog.Fatal("in parsing config file: ", err.Error())
99+
}
100+
} else {
101+
mllog.Fatal("non-existing config file: ", yamlFile)
105102
}
106103

107-
if err = yaml.Unmarshal(cfgData, &dbConfig); err != nil {
108-
mllog.Fatal("in parsing config file: ", err.Error())
109-
}
110-
} else {
111-
yamlFile = ""
104+
ret.Databases = append(ret.Databases, dbConfig)
112105
}
113-
114-
dbConfig.Id = id
115-
dbConfig.Path = dbFile
116-
dbConfig.CompanionFilePath = yamlFile
117-
ret.Databases = append(ret.Databases, dbConfig)
118-
}
119-
120-
for i := range memDb {
121-
// if there's no ":", second is empty ("")
122-
id, yamlFile := splitOnColon(memDb[i])
123-
124-
var dbConfig db
125-
if yamlFile != "" {
126-
// resolves '~'
127-
yamlFile = expandHomeDir(yamlFile, "mem-db yaml file")
128-
129-
if !fileExists(yamlFile) {
130-
mllog.Fatal("mem-db yaml file does not exist")
131-
}
132-
133-
cfgData, err := os.ReadFile(yamlFile)
134-
if err != nil {
135-
mllog.Fatal("in reading config file: ", err.Error())
136-
}
137-
138-
if err = yaml.Unmarshal(cfgData, &dbConfig); err != nil {
139-
mllog.Fatal("in parsing config file: ", err.Error())
140-
}
141-
}
142-
143-
dbConfig.Id = id
144-
dbConfig.Path = ":memory:"
145-
dbConfig.CompanionFilePath = yamlFile
146-
ret.Databases = append(ret.Databases, dbConfig)
106+
} else if *serveDir == "" {
107+
mllog.Fatal("no database and no dir to serve specified")
147108
}
148109

149110
if *serveDir != "" {

0 commit comments

Comments
 (0)