Skip to content

Commit

Permalink
extract GetAuditList() method
Browse files Browse the repository at this point in the history
  • Loading branch information
baurine committed Mar 16, 2021
1 parent bd3cf97 commit e9562d8
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions pkg/cluster/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ func CommandArgs(fp string) ([]string, error) {
}

args := strings.Split(scanner.Text(), " ")
// support for operations from the tiup web ui
if args[1] == "--ui" {
if scanner.Scan() {
args = strings.Split(scanner.Text(), " ")
}
}
return decodeCommandArgs(args)
}

Expand Down Expand Up @@ -78,10 +84,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
}

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

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

auditList := []AuditLogItem{}
for _, fi := range fileInfos {
if fi.IsDir() {
continue
Expand All @@ -95,19 +130,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, AuditLogItem{
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 e9562d8

Please sign in to comment.