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) {
5050 }
5151
5252 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"
5454 helpStr += "help - this message\n"
5555 helpStr += "user - display user information (returns name, id, account created, pico+ expiration)\n"
5656 helpStr += "logs - stream user logs\n"
57+ helpStr += "access_logs - fetch access logs from the last 30 days\n"
5758 helpStr += "chat - IRC chat (must enable pty with `-t` to the SSH command)\n"
5859 helpStr += "not-found - return all status 404 requests for a host (hostname.com [year|month])\n"
5960 c.output(helpStr)
......@@ -96,6 +97,24 @@ func (c *Cmd) notFound(host, interval string) error {
9697 return nil
9798 }
9899
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+
99118 func (c *Cmd) logs(ctx context.Context) error {
100119 conn := shared.NewPicoPipeClient()
101120 stdoutPipe, err := pipeLogger.ReadLogs(ctx, c.Log, conn)
......@@ -246,6 +265,12 @@ func Middleware(handler *CliHandler) pssh.SSHServerMiddleware {
246265 sesh.Fatal(err)
247266 }
248267 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
249274 default:
250275 return next(sesh)
251276 }