listAccounts command

This commit is contained in:
ari melody 2025-01-20 19:11:16 +00:00
parent be5cd05d08
commit 5e493554dc
Signed by: ari
GPG key ID: CF99829C92678188
6 changed files with 50 additions and 9 deletions

View file

@ -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.
- `purgeInvites`: Deletes all available invite codes.
- `listAccounts`: Lists all active accounts.
- `deleteAccount <username>`: Deletes an account with a given `username`.
## database

View file

@ -11,6 +11,17 @@ import (
"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) {
var account = model.Account{}

27
main.go
View file

@ -98,6 +98,27 @@ func main() {
fmt.Printf("Invites deleted successfully.\n")
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":
if len(os.Args) < 2 {
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)
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)
}
@ -135,10 +156,12 @@ func main() {
}
fmt.Printf(
// command help
fmt.Print(
"Available commands:\n\n" +
"createInvite:\n\tCreates an invite code to register new accounts.\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",
)
return

View file

@ -1,5 +1,7 @@
package model
import "time"
type (
Account struct {
ID string `json:"id" db:"id"`
@ -7,6 +9,8 @@ type (
Password string `json:"password" db:"password"`
Email string `json:"email" db:"email"`
AvatarURL string `json:"avatar_url" db:"avatar_url"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
Privileges []AccountPrivilege `json:"privileges"`
}

View file

@ -16,7 +16,8 @@ CREATE TABLE arimelody.account (
username text NOT NULL UNIQUE,
password text NOT NULL,
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);

View file

@ -22,7 +22,8 @@ CREATE TABLE arimelody.account (
username text NOT NULL UNIQUE,
password text NOT NULL,
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);