Skip to content

Commit

Permalink
Merge pull request #2 from joicemjoseph/feat/install
Browse files Browse the repository at this point in the history
added method to install db schema
  • Loading branch information
knadh committed Oct 1, 2020
2 parents 3314385 + 4775d1e commit f4d87da
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
44 changes: 44 additions & 0 deletions install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"fmt"
"strings"
)

func (app *App) installSchema(prompt bool) int {
if prompt {
fmt.Println("")
fmt.Println("** first time installation **")
fmt.Printf("** IMPORTANT: This will wipe existing dictmaker tables and types in the DB '%s' **",
ko.String("db.db"))
fmt.Println("")

var ok string

fmt.Print("continue (y/n)? ")

if _, err := fmt.Scanf("%s", &ok); err != nil {
app.logger.Fatalf("error reading value from terminal: %v", err)
}

if strings.ToLower(ok) != "y" {
fmt.Println("install cancelled.")
return 1
}
}

q, err := app.fs.Read("/schema.sql")
if err != nil {
app.logger.Fatal(err.Error())
return 1
}

if _, err := app.db.Exec(string(q)); err != nil {
app.logger.Fatal(err.Error())
return 1
}

app.logger.Println("successfully installed schema")

return 0
}
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func init() {
"path to one or more config files (will be merged in order)")
f.String("site", "", "path to a site theme. If left empty, only the APIs will run.")
f.Bool("install", false, "run first time installation")
f.Bool("prompt", true, "prompt before each steps in installation")
f.Bool("version", false, "current version of the build")
f.Parse(os.Args[1:])

Expand Down Expand Up @@ -125,6 +126,11 @@ func main() {
logger: logger,
}

// Install schema.
if ko.Bool("install") {
os.Exit(app.installSchema(ko.Bool("prompt")))
}

// Load SQL queries.
qB, err := fs.Read("/queries.sql")
if err != nil {
Expand Down

0 comments on commit f4d87da

Please sign in to comment.