Skip to content

Commit

Permalink
Extract get audit list (#1217)
Browse files Browse the repository at this point in the history
  • Loading branch information
baurine authored Mar 17, 2021
1 parent 671b5f4 commit 43f0dea
Showing 1 changed file with 37 additions and 9 deletions.
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

0 comments on commit 43f0dea

Please sign in to comment.