go-blog/clt/initdatabase/init_database.go

75 lines
1.3 KiB
Go
Raw Permalink Normal View History

2018-04-27 14:38:12 +02:00
// Copyright 2018 Lars Hoogestraat
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"bufio"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
2020-11-29 18:26:34 +01:00
"git.hoogi.eu/snafu/go-blog/database"
2020-11-28 16:40:45 +01:00
"git.hoogi.eu/snafu/go-blog/logger"
2018-04-27 14:38:12 +02:00
)
var (
BuildVersion = "develop"
GitHash = ""
)
func main() {
logger.InitLogger(ioutil.Discard, "Error")
fmt.Printf("init_database version %s\n", BuildVersion)
2018-11-13 11:30:24 +01:00
file := flag.String("sqlite", "", "Location for the sqlite3 database")
2018-04-27 14:38:12 +02:00
flag.Parse()
if flag.Parsed() {
2018-12-13 16:38:28 +01:00
if err := initSQLite(*file); err != nil {
2018-04-27 14:38:12 +02:00
fmt.Println(err)
os.Exit(1)
}
2018-11-25 00:07:40 +01:00
fmt.Println("The tables were created")
2018-04-27 14:38:12 +02:00
}
}
2018-12-13 16:38:28 +01:00
func initSQLite(sqlitefile string) error {
2018-12-13 16:36:35 +01:00
if len(sqlitefile) == 0 {
return fmt.Errorf("the argument -sqlite is empty. Please specify the location of the sqlite3 database file")
}
2018-04-27 14:38:12 +02:00
fmt.Print(">> Do you want to create the tables now? (y|N): ")
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
lInput := strings.ToLower(input)
if strings.ToLower(lInput) != "y\n" {
fmt.Println("Aborted. Tables were not created.")
os.Exit(0)
}
dbConfig := database.SQLiteConfig{
2018-12-13 16:38:28 +01:00
File: sqlitefile,
2018-04-27 14:38:12 +02:00
}
db, err := dbConfig.Open()
if err != nil {
return err
}
defer func() {
db.Close()
}()
2018-11-25 00:07:40 +01:00
return database.InitTables(db)
2018-04-27 14:38:12 +02:00
}