1
0
Fork 0
mirror of synced 2024-11-22 09:05:34 -05:00

add force flag to delete user command

This commit is contained in:
Ilya Sosnovsky 2022-05-23 16:21:54 +03:00
parent 87f93db6a4
commit bc6b572eed
2 changed files with 16 additions and 9 deletions

View file

@ -44,9 +44,12 @@ Commands:
create --user=USER --password=PASSWORD create --user=USER --password=PASSWORD
Create user. Create user.
delete --user=USER delete --user=USER [<flags>]
Delete user. Delete user.
flags:
--force Delete from db
revoke --user=USER revoke --user=USER
Revoke user. Revoke user.
@ -63,5 +66,5 @@ Commands:
Auth user. Auth user.
change-password --user=USER --password=PASSWORD change-password --user=USER --password=PASSWORD
Change password Change password.
``` ```

View file

@ -11,7 +11,7 @@ import (
) )
const ( const (
version = "1.0.3" version = "1.0.4"
) )
var ( var (
@ -24,8 +24,9 @@ var (
createCommandUserFlag = createCommand.Flag("user", "Username.").Required().String() createCommandUserFlag = createCommand.Flag("user", "Username.").Required().String()
createCommandPasswordFlag = createCommand.Flag("password", "Password.").Required().String() createCommandPasswordFlag = createCommand.Flag("password", "Password.").Required().String()
deleteCommand = kingpin.Command("delete", "Delete user.") deleteCommand = kingpin.Command("delete", "Delete user.")
deleteCommandUserFlag = deleteCommand.Flag("user", "Username.").Required().String() deleteCommandUserForceFlag = deleteCommand.Flag("force", "delete from db.").Default("false").Bool()
deleteCommandUserFlag = deleteCommand.Flag("user", "Username.").Required().String()
revokeCommand = kingpin.Command("revoke", "Revoke user.") revokeCommand = kingpin.Command("revoke", "Revoke user.")
revokeCommandUserFlag = revokeCommand.Flag("user", "Username.").Required().String() revokeCommandUserFlag = revokeCommand.Flag("user", "Username.").Required().String()
@ -48,7 +49,6 @@ var (
changePasswordCommandPasswordFlag = changePasswordCommand.Flag("password", "Password.").Required().String() changePasswordCommandPasswordFlag = changePasswordCommand.Flag("password", "Password.").Required().String()
debug = kingpin.Flag("debug", "Enable debug mode.").Default("false").Bool() debug = kingpin.Flag("debug", "Enable debug mode.").Default("false").Bool()
) )
type User struct { type User struct {
@ -119,7 +119,11 @@ func createUser(username, password string) {
} }
func deleteUser(username string) { func deleteUser(username string) {
res, err := getDb().Exec("UPDATE users SET deleted = 1 WHERE username = $1", username) deleteQuery := "UPDATE users SET deleted = 1 WHERE username = $1"
if *deleteCommandUserForceFlag {
deleteQuery = "DELETE FROM users WHERE username = $1"
}
res, err := getDb().Exec(deleteQuery, username)
checkErr(err) checkErr(err)
if rowsAffected, rowsErr := res.RowsAffected(); rowsErr != nil { if rowsAffected, rowsErr := res.RowsAffected(); rowsErr != nil {
if rowsAffected == 1 { if rowsAffected == 1 {
@ -181,7 +185,7 @@ func userDeleted(username string) bool {
// return true if user marked as deleted // return true if user marked as deleted
u := User{} u := User{}
_ = getDb().QueryRow("SELECT * FROM users WHERE username = $1", username).Scan(&u) _ = getDb().QueryRow("SELECT * FROM users WHERE username = $1", username).Scan(&u)
if u.deleted { if u.deleted {
fmt.Printf("User %s marked as deleted\n", username) fmt.Printf("User %s marked as deleted\n", username)
return true return true
} else { } else {
@ -193,7 +197,7 @@ func userIsActive(username string) bool {
// return true if user exist and not deleted and revoked // return true if user exist and not deleted and revoked
u := User{} u := User{}
_ = getDb().QueryRow("SELECT * FROM users WHERE username = $1", username).Scan(&u) _ = getDb().QueryRow("SELECT * FROM users WHERE username = $1", username).Scan(&u)
if !u.revoked && !u.deleted { if !u.revoked && !u.deleted {
fmt.Printf("User %s is active\n", username) fmt.Printf("User %s is active\n", username)
return true return true
} else { } else {