listAccounts command
This commit is contained in:
parent
be5cd05d08
commit
5e493554dc
|
@ -40,6 +40,7 @@ need to be up for this, making this ideal for some offline maintenance.
|
||||||
|
|
||||||
- `createInvite`: Creates an invite code to register new accounts.
|
- `createInvite`: Creates an invite code to register new accounts.
|
||||||
- `purgeInvites`: Deletes all available invite codes.
|
- `purgeInvites`: Deletes all available invite codes.
|
||||||
|
- `listAccounts`: Lists all active accounts.
|
||||||
- `deleteAccount <username>`: Deletes an account with a given `username`.
|
- `deleteAccount <username>`: Deletes an account with a given `username`.
|
||||||
|
|
||||||
## database
|
## database
|
||||||
|
|
|
@ -11,6 +11,17 @@ import (
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func GetAllAccounts(db *sqlx.DB) ([]model.Account, error) {
|
||||||
|
var accounts = []model.Account{}
|
||||||
|
|
||||||
|
err := db.Select(&accounts, "SELECT * FROM account ORDER BY created_at ASC")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return accounts, nil
|
||||||
|
}
|
||||||
|
|
||||||
func GetAccount(db *sqlx.DB, username string) (*model.Account, error) {
|
func GetAccount(db *sqlx.DB, username string) (*model.Account, error) {
|
||||||
var account = model.Account{}
|
var account = model.Account{}
|
||||||
|
|
||||||
|
|
27
main.go
27
main.go
|
@ -98,6 +98,27 @@ func main() {
|
||||||
fmt.Printf("Invites deleted successfully.\n")
|
fmt.Printf("Invites deleted successfully.\n")
|
||||||
return
|
return
|
||||||
|
|
||||||
|
case "listAccounts":
|
||||||
|
accounts, err := controller.GetAllAccounts(global.DB)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "Failed to fetch accounts: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, account := range accounts {
|
||||||
|
fmt.Printf(
|
||||||
|
"User: %s\n" +
|
||||||
|
"\tID: %s\n" +
|
||||||
|
"\tEmail: %s\n" +
|
||||||
|
"\tCreated: %s\n",
|
||||||
|
account.Username,
|
||||||
|
account.ID,
|
||||||
|
account.Email,
|
||||||
|
account.CreatedAt,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
|
||||||
case "deleteAccount":
|
case "deleteAccount":
|
||||||
if len(os.Args) < 2 {
|
if len(os.Args) < 2 {
|
||||||
fmt.Fprintf(os.Stderr, "FATAL: Account name not specified for -deleteAccount\n")
|
fmt.Fprintf(os.Stderr, "FATAL: Account name not specified for -deleteAccount\n")
|
||||||
|
@ -108,7 +129,7 @@ func main() {
|
||||||
|
|
||||||
account, err := controller.GetAccount(global.DB, username)
|
account, err := controller.GetAccount(global.DB, username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Failed to fetch account \"%s\": %s\n", username, err.Error())
|
fmt.Fprintf(os.Stderr, "Failed to fetch account \"%s\": %v\n", username, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,10 +156,12 @@ func main() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Printf(
|
// command help
|
||||||
|
fmt.Print(
|
||||||
"Available commands:\n\n" +
|
"Available commands:\n\n" +
|
||||||
"createInvite:\n\tCreates an invite code to register new accounts.\n" +
|
"createInvite:\n\tCreates an invite code to register new accounts.\n" +
|
||||||
"purgeInvites:\n\tDeletes all available invite codes.\n" +
|
"purgeInvites:\n\tDeletes all available invite codes.\n" +
|
||||||
|
"listAccounts:\n\tLists all active accounts.\n",
|
||||||
"deleteAccount <username>:\n\tDeletes an account with a given `username`.\n",
|
"deleteAccount <username>:\n\tDeletes an account with a given `username`.\n",
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package model
|
package model
|
||||||
|
|
||||||
|
import "time"
|
||||||
|
|
||||||
type (
|
type (
|
||||||
Account struct {
|
Account struct {
|
||||||
ID string `json:"id" db:"id"`
|
ID string `json:"id" db:"id"`
|
||||||
|
@ -7,6 +9,8 @@ type (
|
||||||
Password string `json:"password" db:"password"`
|
Password string `json:"password" db:"password"`
|
||||||
Email string `json:"email" db:"email"`
|
Email string `json:"email" db:"email"`
|
||||||
AvatarURL string `json:"avatar_url" db:"avatar_url"`
|
AvatarURL string `json:"avatar_url" db:"avatar_url"`
|
||||||
|
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||||
|
|
||||||
Privileges []AccountPrivilege `json:"privileges"`
|
Privileges []AccountPrivilege `json:"privileges"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,8 @@ CREATE TABLE arimelody.account (
|
||||||
username text NOT NULL UNIQUE,
|
username text NOT NULL UNIQUE,
|
||||||
password text NOT NULL,
|
password text NOT NULL,
|
||||||
email text,
|
email text,
|
||||||
avatar_url text
|
avatar_url text,
|
||||||
|
created_at TIMESTAMP DEFAULT current_timestamp
|
||||||
);
|
);
|
||||||
ALTER TABLE arimelody.account ADD CONSTRAINT account_pk PRIMARY KEY (id);
|
ALTER TABLE arimelody.account ADD CONSTRAINT account_pk PRIMARY KEY (id);
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,8 @@ CREATE TABLE arimelody.account (
|
||||||
username text NOT NULL UNIQUE,
|
username text NOT NULL UNIQUE,
|
||||||
password text NOT NULL,
|
password text NOT NULL,
|
||||||
email text,
|
email text,
|
||||||
avatar_url text
|
avatar_url text,
|
||||||
|
created_at TIMESTAMP DEFAULT current_timestamp
|
||||||
);
|
);
|
||||||
ALTER TABLE arimelody.account ADD CONSTRAINT account_pk PRIMARY KEY (id);
|
ALTER TABLE arimelody.account ADD CONSTRAINT account_pk PRIMARY KEY (id);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue