internal/control/audit.go
44 lines · 1026 bytes
1package control
2
3import (
4 "fmt"
5 "io"
6 "strconv"
7
8 "gitbay.org/gitbay/internal/protocol"
9)
10
11func init() {
12 register(Command{Path: []string{"audit"},
13 Summary: "instance audit log (admins): audit [--limit <n>]", ReadOnly: true, SSHOnly: true, Run: runAudit})
14}
15
16func runAudit(c *Ctx, args []string) int {
17 if !c.User.IsAdmin {
18 return c.fail(protocol.ExitDenied, "the audit log is for instance admins")
19 }
20 limit := 100
21 for i := 0; i < len(args); i++ {
22 if args[i] == "--limit" && i+1 < len(args) {
23 if n, err := strconv.Atoi(args[i+1]); err == nil && n > 0 && n <= 10000 {
24 limit = n
25 }
26 i++
27 } else {
28 return c.fail(protocol.ExitUsage, "usage: audit [--limit <n>]")
29 }
30 }
31 entries, err := c.Store.AuditEntries(limit)
32 if err != nil {
33 return c.fail(protocol.ExitFailure, "%v", err)
34 }
35 return c.emit(entries, func(w io.Writer) {
36 for _, e := range entries {
37 actor := e.Actor
38 if actor == "" {
39 actor = "-"
40 }
41 fmt.Fprintf(w, "%s\t%s\t%s\t%s\n", e.CreatedAt, actor, e.Action, e.Data)
42 }
43 })
44}