v0.1 公开测试版

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
2026-04-23 21:48:04 +08:00
parent 62e076111f
commit f8051ea8c9
6 changed files with 561 additions and 70 deletions
+42 -2
View File
@@ -33,6 +33,8 @@ func main() {
handleListUsers(cfg, os.Args[2:])
case "list-tokens":
handleListTokens(cfg, os.Args[2:])
case "list-usage":
handleListUsage(cfg, os.Args[2:])
default:
printUsage()
os.Exit(1)
@@ -160,7 +162,44 @@ func handleListTokens(cfg config.Config, args []string) {
if t.MaxUses == 0 {
maxUsesDisplay = "unlimited"
}
fmt.Printf("%d\t%s\t%t\t%d/%s\t%s\t%s\t%s\n", t.ID, t.Username, t.Disabled, t.UsedCount, maxUsesDisplay, t.ExpiresAt.Format(time.RFC3339), t.CreatedAt.Format(time.RFC3339), t.Token)
fmt.Printf("%d\t%s\t%t\t%d/%s\t%s\t%s\t%s\n", t.ID, t.Username, t.Disabled, t.UsedCount, maxUsesDisplay, t.ExpiresAt.Format(time.RFC3339), t.CreatedAt.Format(time.RFC3339), t.Token.Token)
}
}
func handleListUsage(cfg config.Config, args []string) {
fs := flag.NewFlagSet("list-usage", flag.ExitOnError)
dbPath := fs.String("db", cfg.DBPath, "SQLite DB path")
limit := fs.Int("limit", 50, "max rows to show, <=0 means all")
_ = fs.Parse(args)
store := openStore(*dbPath, cfg.BusyTimeoutMS)
defer store.Close()
entries, err := store.ListUsageEntries(context.Background(), *limit)
if err != nil {
log.Fatal(err)
}
fmt.Println("time\trequest_ip\tuser_id\ttoken_id\thttp_status\tsuccess\terror_reason\toriginal_url")
for _, e := range entries {
userID := "-"
tokenID := "-"
if e.UserID.Valid {
userID = fmt.Sprintf("%d", e.UserID.Int64)
}
if e.TokenID.Valid {
tokenID = fmt.Sprintf("%d", e.TokenID.Int64)
}
fmt.Printf("%s\t%s\t%s\t%s\t%d\t%t\t%s\t%s\n",
e.OccurredAt.Format(time.RFC3339),
e.RequestIP,
userID,
tokenID,
e.HTTPStatus,
e.Success,
e.ErrorReason,
e.OriginalURL,
)
}
}
@@ -194,5 +233,6 @@ func printUsage() {
tokenctl issue-token --user <username> [--expires <7d|30d|RFC3339>] [--uses <n>] [--db <path>]
tokenctl disable-token --token <token> [--db <path>]
tokenctl list-users [--db <path>]
tokenctl list-tokens [--db <path>]`)
tokenctl list-tokens [--db <path>]
tokenctl list-usage [--limit <n>] [--db <path>]`)
}