From e9562d89789ba34bbdea08e8a25ab027b9a51f2d Mon Sep 17 00:00:00 2001 From: baurine <2008.hbl@gmail.com> Date: Tue, 16 Mar 2021 16:15:44 +0800 Subject: [PATCH 1/2] extract GetAuditList() method --- pkg/cluster/audit/audit.go | 52 +++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 9 deletions(-) diff --git a/pkg/cluster/audit/audit.go b/pkg/cluster/audit/audit.go index 8afd269116..c88d67cf0c 100644 --- a/pkg/cluster/audit/audit.go +++ b/pkg/cluster/audit/audit.go @@ -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) } @@ -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 @@ -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. From 1f2845b7a19be18f1b95bfd53097169fad7e1b3f Mon Sep 17 00:00:00 2001 From: baurine <2008.hbl@gmail.com> Date: Tue, 16 Mar 2021 22:48:02 +0800 Subject: [PATCH 2/2] refine --- pkg/cluster/audit/audit.go | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/pkg/cluster/audit/audit.go b/pkg/cluster/audit/audit.go index c88d67cf0c..d450cc84bb 100644 --- a/pkg/cluster/audit/audit.go +++ b/pkg/cluster/audit/audit.go @@ -45,12 +45,6 @@ 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) } @@ -102,21 +96,21 @@ func ShowAuditList(dir string) error { return nil } -// AuditLogItem represents a single audit item -type AuditLogItem struct { +// 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) ([]AuditLogItem, error) { +func GetAuditList(dir string) ([]Item, error) { fileInfos, err := os.ReadDir(dir) if err != nil && !os.IsNotExist(err) { return nil, err } - auditList := []AuditLogItem{} + auditList := []Item{} for _, fi := range fileInfos { if fi.IsDir() { continue @@ -130,7 +124,7 @@ func GetAuditList(dir string) ([]AuditLogItem, error) { continue } cmd := strings.Join(args, " ") - auditList = append(auditList, AuditLogItem{ + auditList = append(auditList, Item{ ID: fi.Name(), Time: t.Format(time.RFC3339), Command: cmd,