summaryrefslogtreecommitdiff
path: root/conf/main.go
diff options
context:
space:
mode:
authorsanine-a <sanine.not@pm.me>2023-05-02 13:22:21 -0500
committersanine-a <sanine.not@pm.me>2023-05-02 13:22:21 -0500
commit4343925a09c1fd34da4b352ae9fa2510397ccdcd (patch)
tree0a8aae522a017f1bfda08ada75c198972cac2d78 /conf/main.go
parent416055e02b74f640ab6030deaa9f7767221a49cd (diff)
add basic commands
Diffstat (limited to 'conf/main.go')
-rw-r--r--conf/main.go79
1 files changed, 78 insertions, 1 deletions
diff --git a/conf/main.go b/conf/main.go
index 1015592..052f086 100644
--- a/conf/main.go
+++ b/conf/main.go
@@ -1,9 +1,86 @@
package main
import (
+ "os"
+ "fmt"
+ "log"
+ "flag"
db "sanine.net/git/phlox/db"
)
+
+var p *db.Phlox
+
+
+type Params struct {
+ DbFilename string
+}
+var params Params
+
+
+type Command struct {
+ Set bool
+ Value int
+ Flag string
+ Usage string
+ Invoke func()
+}
+
+
+func RegisterCommand(val int, cmd *Command) {
+ cmd.Value = val
+ flag.BoolVar(&(*cmd).Set, cmd.Flag, false, cmd.Usage)
+}
+
+
func main() {
- db.HelloWorld()
+ p = &db.Phlox{}
+
+ cmds := map[int]*Command{
+ 1: Create(),
+ 2: SchemaVersion(),
+ }
+
+ for val, cmd := range cmds {
+ RegisterCommand(val, cmd)
+ }
+
+ flag.StringVar(&params.DbFilename, "db", "phlox.conf", "select the db file to operate on")
+ flag.Parse()
+
+ for _, cmd := range cmds {
+ if cmd.Set { cmd.Invoke(); os.Exit(0); }
+ }
+
+ log.Fatal("no command specified!")
+}
+
+
+func Create() *Command {
+ return &Command{
+ Flag: "create",
+ Usage: "create a new database",
+ Invoke: func() {
+ err := p.Create(params.DbFilename)
+ if err != nil {
+ log.Fatal(err)
+ }
+ fmt.Printf("created database %v\n", params.DbFilename)
+ },
+ }
+}
+
+
+func SchemaVersion() *Command {
+ return &Command{
+ Flag: "schema-version",
+ Usage: "get the schema version",
+ Invoke: func() {
+ err := p.Open(params.DbFilename)
+ if err != nil { log.Fatal(err) }
+ version, err := p.GetSchemaVersion()
+ if err != nil { log.Fatal(err) }
+ fmt.Printf("schema version: %v\n", version)
+ },
+ }
}