change to use logging instead of print to stdout
This commit is contained in:
@@ -74,10 +74,10 @@ func ConnectDatabase() {
|
||||
db, err = sqlx.Open("sqlite", sqlPath)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Error opening sqlite database connection to file '%s' : '%s'\n", sqlPath, err)
|
||||
log.Printf("Error opening sqlite database connection to file '%s' : '%s'\n", sqlPath, err)
|
||||
os.Exit(1)
|
||||
} else {
|
||||
fmt.Printf("Connected to sqlite database file '%s'\n", sqlPath)
|
||||
log.Printf("Connected to sqlite database file '%s'\n", sqlPath)
|
||||
}
|
||||
|
||||
//sqlx.NameMapper = func(s string) string { return s }
|
||||
@@ -89,7 +89,7 @@ func ConnectDatabase() {
|
||||
}
|
||||
|
||||
func DisconnectDatabase() {
|
||||
fmt.Printf("DisconnectDatabase called")
|
||||
log.Printf("DisconnectDatabase called")
|
||||
defer db.Close()
|
||||
}
|
||||
|
||||
@@ -99,28 +99,28 @@ func CreateTables() {
|
||||
// Create database tables if it doesn't exist
|
||||
// Roles table should go first since other tables refer to it
|
||||
if _, err = db.Exec(createRoles); err != nil {
|
||||
fmt.Printf("Error checking roles table : '%s'", err)
|
||||
log.Printf("Error checking roles table : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
rowCount, _ = CheckCount("roles")
|
||||
if rowCount == 0 {
|
||||
if _, err = db.Exec("INSERT INTO roles VALUES(1, 'Admin', false, true);"); err != nil {
|
||||
fmt.Printf("Error adding initial admin role : '%s'", err)
|
||||
log.Printf("Error adding initial admin role : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if _, err = db.Exec("INSERT INTO roles VALUES(2, 'UserRole', false, false);"); err != nil {
|
||||
fmt.Printf("Error adding initial user role : '%s'", err)
|
||||
log.Printf("Error adding initial user role : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if _, err = db.Exec("INSERT INTO roles VALUES(3, 'GuestRole', true, false);"); err != nil {
|
||||
fmt.Printf("Error adding initial guest role : '%s'", err)
|
||||
log.Printf("Error adding initial guest role : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// Users table
|
||||
if _, err = db.Exec(createUsers); err != nil {
|
||||
fmt.Printf("Error checking users table : '%s'", err)
|
||||
log.Printf("Error checking users table : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
rowCount, _ = CheckCount("users")
|
||||
@@ -130,30 +130,30 @@ func CreateTables() {
|
||||
if initialPassword == "" {
|
||||
initialPassword = "password"
|
||||
} else if initialPassword[:4] == "$2a$" {
|
||||
fmt.Printf("CreateTables inital admin password is already a hash")
|
||||
log.Printf("CreateTables inital admin password is already a hash")
|
||||
} else {
|
||||
cryptText, _ := bcrypt.GenerateFromPassword([]byte(initialPassword), bcrypt.DefaultCost)
|
||||
initialPassword = string(cryptText)
|
||||
}
|
||||
if _, err = db.Exec("INSERT INTO users VALUES(1, 1, 'Administrator', ?);", initialPassword); err != nil {
|
||||
fmt.Printf("Error adding initial admin role : '%s'", err)
|
||||
log.Printf("Error adding initial admin role : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
// Secrets table
|
||||
if _, err = db.Exec(createSecrets); err != nil {
|
||||
fmt.Printf("Error checking secrets table : '%s'", err)
|
||||
log.Printf("Error checking secrets table : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
// Schema table should go last so we know if the database has a value in the schema table then everything was created properly
|
||||
if _, err = db.Exec(createSchema); err != nil {
|
||||
fmt.Printf("Error checking schema table : '%s'", err)
|
||||
log.Printf("Error checking schema table : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
schemaCheck, _ := CheckColumnExists("schema", "Version")
|
||||
if !schemaCheck {
|
||||
if _, err = db.Exec("INSERT INTO schema VALUES(1);"); err != nil {
|
||||
fmt.Printf("Error adding initial scehama version : '%s'", err)
|
||||
log.Printf("Error adding initial scehama version : '%s'", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -165,12 +165,12 @@ func CheckCount(tablename string) (int, error) {
|
||||
var count int
|
||||
stmt, err := db.Prepare("SELECT COUNT(*) as count FROM " + tablename)
|
||||
if err != nil {
|
||||
fmt.Printf("CheckCount error preparing sqlite statement : '%s'\n", err)
|
||||
log.Printf("CheckCount error preparing sqlite statement : '%s'\n", err)
|
||||
return 0, err
|
||||
}
|
||||
err = stmt.QueryRow().Scan(&count)
|
||||
if err != nil {
|
||||
fmt.Printf("CheckCount error querying database record count : '%s'\n", err)
|
||||
log.Printf("CheckCount error querying database record count : '%s'\n", err)
|
||||
return 0, err
|
||||
}
|
||||
stmt.Close() // or use defer rows.Close(), idc
|
||||
@@ -214,7 +214,7 @@ func GenerateInsertMethod(q interface{}) (string, error) {
|
||||
fieldValues = fmt.Sprintf("%s, %d", fieldValues, boolSet)
|
||||
}
|
||||
default:
|
||||
fmt.Printf("Unsupported type '%s'\n", v.Field(i).Kind())
|
||||
log.Printf("Unsupported type '%s'\n", v.Field(i).Kind())
|
||||
}
|
||||
}
|
||||
query = fmt.Sprintf("%s(%s) VALUES (%s)", query, fieldNames, fieldValues)
|
||||
@@ -227,14 +227,14 @@ func CheckColumnExists(table string, column string) (bool, error) {
|
||||
var count int64
|
||||
rows, err := db.Queryx("SELECT COUNT(*) AS CNTREC FROM pragma_table_info('" + table + "') WHERE name='" + column + "';")
|
||||
if err != nil {
|
||||
fmt.Printf("CheckColumnExists error querying database for existence of column '%s' : '%s'\n", column, err)
|
||||
log.Printf("CheckColumnExists error querying database for existence of column '%s' : '%s'\n", column, err)
|
||||
return false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
for rows.Next() {
|
||||
// cols is an []interface{} of all of the column results
|
||||
cols, _ := rows.SliceScan()
|
||||
fmt.Printf("CheckColumnExists Value is '%v' for table '%s' and column '%s'\n", cols[0].(int64), table, column)
|
||||
log.Printf("CheckColumnExists Value is '%v' for table '%s' and column '%s'\n", cols[0].(int64), table, column)
|
||||
count = cols[0].(int64)
|
||||
|
||||
if count == 1 {
|
||||
@@ -246,7 +246,7 @@ func CheckColumnExists(table string, column string) (bool, error) {
|
||||
|
||||
err = rows.Err()
|
||||
if err != nil {
|
||||
fmt.Printf("CheckColumnExists error getting results : '%s'\n", err)
|
||||
log.Printf("CheckColumnExists error getting results : '%s'\n", err)
|
||||
return false, err
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user