-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: bupd <[email protected]>
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -106,6 +106,7 @@ harbor help | |
repositry.Repository(), | ||
user.User(), | ||
artifact.Artifact(), | ||
Logs(), | ||
) | ||
|
||
return root | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |