diff options
author | sanine-a <sanine.not@pm.me> | 2023-05-02 13:22:21 -0500 |
---|---|---|
committer | sanine-a <sanine.not@pm.me> | 2023-05-02 13:22:21 -0500 |
commit | 4343925a09c1fd34da4b352ae9fa2510397ccdcd (patch) | |
tree | 0a8aae522a017f1bfda08ada75c198972cac2d78 | |
parent | 416055e02b74f640ab6030deaa9f7767221a49cd (diff) |
add basic commands
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | conf/go.mod | 5 | ||||
-rw-r--r-- | conf/go.sum | 4 | ||||
-rw-r--r-- | conf/main.go | 79 |
4 files changed, 88 insertions, 1 deletions
@@ -1,2 +1,3 @@ phlox/phlox conf/phloxconf* +conf/phlox.conf diff --git a/conf/go.mod b/conf/go.mod index ff194cd..7349d9f 100644 --- a/conf/go.mod +++ b/conf/go.mod @@ -5,3 +5,8 @@ go 1.19 replace sanine.net/git/phlox/db => ../db require sanine.net/git/phlox/db v0.0.0-00010101000000-000000000000 + +require ( + github.com/mattn/go-sqlite3 v1.14.16 // indirect + golang.org/x/crypto v0.8.0 // indirect +) diff --git a/conf/go.sum b/conf/go.sum new file mode 100644 index 0000000..48de3bc --- /dev/null +++ b/conf/go.sum @@ -0,0 +1,4 @@ +github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y= +github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg= +golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ= +golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE= 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(¶ms.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) + }, + } } |