2016-12-08 19:45:26 +00:00
|
|
|
package commands
|
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
import (
|
|
|
|
"github.com/dannyvankooten/ana/db"
|
|
|
|
"github.com/dannyvankooten/ana/models"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"log"
|
2016-12-08 19:45:26 +00:00
|
|
|
)
|
|
|
|
|
2016-12-08 19:57:44 +00:00
|
|
|
func createUser() {
|
2016-12-11 13:50:01 +00:00
|
|
|
if emailArg == "" || passwordArg == "" {
|
|
|
|
log.Fatal("Please supply -email and -password values")
|
|
|
|
}
|
2016-12-08 19:45:26 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
hash, _ := bcrypt.GenerateFromPassword([]byte(passwordArg), 10)
|
|
|
|
user := models.User{
|
|
|
|
Email: emailArg,
|
|
|
|
Password: string(hash),
|
|
|
|
}
|
|
|
|
user.Save(db.Conn)
|
2016-12-08 19:57:44 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
log.Printf("User %s #%d created", emailArg, user.ID)
|
2016-12-08 19:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func deleteUser() {
|
2016-12-11 13:50:01 +00:00
|
|
|
if emailArg == "" && idArg == 0 {
|
|
|
|
log.Fatal("Please supply an -email or -id value")
|
|
|
|
}
|
2016-12-08 19:57:44 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
stmt2, _ := db.Conn.Prepare("DELETE FROM users WHERE email = ? OR id = ?")
|
|
|
|
stmt2.Exec(emailArg, idArg)
|
2016-12-08 19:57:44 +00:00
|
|
|
|
2016-12-11 13:50:01 +00:00
|
|
|
log.Printf("User with email %s or ID %d deleted", emailArg, idArg)
|
2016-12-08 19:45:26 +00:00
|
|
|
}
|