Skip to content

Commit

Permalink
add: audit-log command
Browse files Browse the repository at this point in the history
Signed-off-by: bupd <[email protected]>
  • Loading branch information
bupd committed Jun 5, 2024
1 parent 24b2bd4 commit 541db1d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/harbor/root/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ harbor help
repositry.Repository(),
user.User(),
artifact.Artifact(),
Logs(),
)

return root
Expand Down
46 changes: 46 additions & 0 deletions cmd/harbor/root/logs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package root

import (
"log"

"github.com/goharbor/harbor-cli/pkg/api"
"github.com/goharbor/harbor-cli/pkg/utils"
list "github.com/goharbor/harbor-cli/pkg/views/logs"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func Logs() *cobra.Command {
var opts api.ListFlags

cmd := &cobra.Command{
Use: "logs",
Short: "Get recent logs of the projects which the user is a member of",
Run: func(cmd *cobra.Command, args []string) {
logs, err := api.AuditLogs(opts)
if err != nil {
log.Fatalf("failed to get projects list: %v", err)
}
FormatFlag := viper.GetString("output-format")
if FormatFlag != "" {
utils.PrintPayloadInJSONFormat(logs.Payload)
return
}
list.ListLogs(logs.Payload)
},
}

flags := cmd.Flags()
flags.Int64VarP(&opts.Page, "page", "", 1, "Page number")
flags.Int64VarP(&opts.PageSize, "page-size", "", 10, "Size of per page")
flags.StringVarP(&opts.Q, "query", "q", "", "Query string to query resources")
flags.StringVarP(
&opts.Sort,
"sort",
"",
"",
"Sort the resource list in ascending or descending order",
)

return cmd
}
25 changes: 25 additions & 0 deletions pkg/api/auditLogs_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package api

import (
"github.com/goharbor/go-client/pkg/sdk/v2.0/client/auditlog"
"github.com/goharbor/harbor-cli/pkg/utils"
)

func AuditLogs(opts ListFlags) (*auditlog.ListAuditLogsOK, error) {
ctx, client, err := utils.ContextWithClient()
if err != nil {
return nil, err
}

response, err := client.Auditlog.ListAuditLogs(ctx, &auditlog.ListAuditLogsParams{
Q: &opts.Q,
Sort: &opts.Sort,
Page: &opts.Page,
PageSize: &opts.PageSize,
})
if err != nil {
return nil, err
}

return response, nil
}
41 changes: 41 additions & 0 deletions pkg/views/logs/view.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package list

import (
"fmt"
"os"

"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/goharbor/go-client/pkg/sdk/v2.0/models"
"github.com/goharbor/harbor-cli/pkg/utils"
"github.com/goharbor/harbor-cli/pkg/views/base/tablelist"
)

var columns = []table.Column{
{Title: "User", Width: 18},
{Title: "Resource", Width: 18},
{Title: "Resource Type", Width: 14},
{Title: "Operation", Width: 10},
{Title: "Time", Width: 16},
}

func ListLogs(logs []*models.AuditLog) {
var rows []table.Row
for _, log := range logs {
operationTime, _ := utils.FormatCreatedTime(log.OpTime.String())
rows = append(rows, table.Row{
log.Username,
log.Resource,
log.ResourceType,
log.Operation,
operationTime,
})
}

m := tablelist.NewModel(columns, rows, len(rows))

if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("Error running program:", err)
os.Exit(1)
}
}

0 comments on commit 541db1d

Please sign in to comment.