Commit 7a37a0a
Eric Bower
·
2026-01-18 10:42:36 -0500 EST
parent 5ead68b
feat(pico): access_logs cli command
1 files changed,
+26,
-1
+26,
-1
| ... | ... | @@ -50,10 +50,11 @@ func (c *Cmd) output(out string) { | |
| 50 | 50 | } | |
| 51 | 51 | ||
| 52 | 52 | func (c *Cmd) help() { | |
| 53 | - | helpStr := "Commands: [help, user, logs, chat, not-found]\n" | |
| 53 | + | helpStr := "Commands: [help, user, logs, access_logs, chat, not-found]\n" | |
| 54 | 54 | helpStr += "help - this message\n" | |
| 55 | 55 | helpStr += "user - display user information (returns name, id, account created, pico+ expiration)\n" | |
| 56 | 56 | helpStr += "logs - stream user logs\n" | |
| 57 | + | helpStr += "access_logs - fetch access logs from the last 30 days\n" | |
| 57 | 58 | helpStr += "chat - IRC chat (must enable pty with `-t` to the SSH command)\n" | |
| 58 | 59 | helpStr += "not-found - return all status 404 requests for a host (hostname.com [year|month])\n" | |
| 59 | 60 | c.output(helpStr) |
| ... | ... | @@ -96,6 +97,24 @@ func (c *Cmd) notFound(host, interval string) error { | |
| 96 | 97 | return nil | |
| 97 | 98 | } | |
| 98 | 99 | ||
| 100 | + | func (c *Cmd) access_logs(ctx context.Context) error { | |
| 101 | + | fromDate := time.Now().AddDate(0, 0, -30) | |
| 102 | + | logs, err := c.Dbpool.FindAccessLogs(c.User.ID, &fromDate) | |
| 103 | + | if err != nil { | |
| 104 | + | return err | |
| 105 | + | } | |
| 106 | + | ||
| 107 | + | for _, log := range logs { | |
| 108 | + | jsonData, err := json.Marshal(log) | |
| 109 | + | if err != nil { | |
| 110 | + | c.Log.Error("json marshall", "err", err) | |
| 111 | + | continue | |
| 112 | + | } | |
| 113 | + | c.output(string(jsonData)) | |
| 114 | + | } | |
| 115 | + | return nil | |
| 116 | + | } | |
| 117 | + | ||
| 99 | 118 | func (c *Cmd) logs(ctx context.Context) error { | |
| 100 | 119 | conn := shared.NewPicoPipeClient() | |
| 101 | 120 | stdoutPipe, err := pipeLogger.ReadLogs(ctx, c.Log, conn) |
| ... | ... | @@ -246,6 +265,12 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware { | |
| 246 | 265 | sesh.Fatal(err) | |
| 247 | 266 | } | |
| 248 | 267 | return nil | |
| 268 | + | case "access_logs": | |
| 269 | + | err = opts.access_logs(sesh.Context()) | |
| 270 | + | if err != nil { | |
| 271 | + | sesh.Fatal(err) | |
| 272 | + | } | |
| 273 | + | return nil | |
| 249 | 274 | default: | |
| 250 | 275 | return next(sesh) | |
| 251 | 276 | } |