Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add logs Command to Harbor CLI #106

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}