Skip to content
Merged
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
46 changes: 37 additions & 9 deletions pkg/cluster/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,39 @@ func decodeCommandArgs(args []string) ([]string, error) {
func ShowAuditList(dir string) error {
// Header
clusterTable := [][]string{{"ID", "Time", "Command"}}

auditList, err := GetAuditList(dir)
if err != nil {
return err
}

for _, item := range auditList {
clusterTable = append(clusterTable, []string{
item.ID,
item.Time,
item.Command,
})
}

cliutil.PrintTable(clusterTable, true)
return nil
}

// Item represents a single audit item
type Item struct {
ID string `json:"id"`
Time string `json:"time"`
Command string `json:"command"`
}

// GetAuditList get the audit item list
func GetAuditList(dir string) ([]Item, error) {
fileInfos, err := os.ReadDir(dir)
if err != nil && !os.IsNotExist(err) {
return err
return nil, err
}

auditList := []Item{}
for _, fi := range fileInfos {
if fi.IsDir() {
continue
Expand All @@ -95,19 +124,18 @@ func ShowAuditList(dir string) error {
continue
}
cmd := strings.Join(args, " ")
clusterTable = append(clusterTable, []string{
fi.Name(),
t.Format(time.RFC3339),
cmd,
auditList = append(auditList, Item{
ID: fi.Name(),
Time: t.Format(time.RFC3339),
Command: cmd,
})
}

sort.Slice(clusterTable[1:], func(i, j int) bool {
return clusterTable[i+1][1] > clusterTable[j+1][1]
sort.Slice(auditList, func(i, j int) bool {
return auditList[i].Time > auditList[j].Time
})

cliutil.PrintTable(clusterTable, true)
return nil
return auditList, nil
}

// OutputAuditLog outputs audit log.
Expand Down