Skip to content

Commit

Permalink
Fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
lucklove committed Feb 25, 2021
1 parent ddaa0d3 commit 7ed712a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
25 changes: 19 additions & 6 deletions pkg/cluster/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ func CommandArgs(fp string) ([]string, error) {
}

args := strings.Split(scanner.Text(), " ")
return DecodeCommandArgs(args)
return decodeCommandArgs(args)
}

// EncodeCommandArgs encode args with url.QueryEscape
func EncodeCommandArgs(args []string) []string {
// encodeCommandArgs encode args with url.QueryEscape
func encodeCommandArgs(args []string) []string {
encoded := []string{}

for _, arg := range args {
Expand All @@ -59,8 +59,8 @@ func EncodeCommandArgs(args []string) []string {
return encoded
}

// DecodeCommandArgs decode args with url.QueryUnescape
func DecodeCommandArgs(args []string) ([]string, error) {
// decodeCommandArgs decode args with url.QueryUnescape
func decodeCommandArgs(args []string) ([]string, error) {
decoded := []string{}

for _, arg := range args {
Expand Down Expand Up @@ -113,7 +113,20 @@ func ShowAuditList(dir string) error {
// OutputAuditLog outputs audit log.
func OutputAuditLog(dir string, data []byte) error {
fname := filepath.Join(dir, base52.Encode(time.Now().UnixNano()+rand.Int63n(1000)))
return os.WriteFile(fname, data, 0644)
f, err := os.Create(fname)
if err != nil {
return errors.Annotate(err, "create audit log")
}
defer f.Close()

args := encodeCommandArgs(os.Args)
if _, err := f.Write([]byte(strings.Join(args, " ") + "\n")); err != nil {
return errors.Annotate(err, "write audit log")
}
if _, err := f.Write(data); err != nil {
return errors.Annotate(err, "write audit log")
}
return nil
}

// ShowAuditLog show the audit with the specified auditID
Expand Down
5 changes: 1 addition & 4 deletions pkg/logger/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ package logger

import (
"bytes"
"os"
"strings"

"github.com/pingcap/tiup/pkg/cluster/audit"
utils2 "github.com/pingcap/tiup/pkg/utils"
Expand All @@ -41,8 +39,7 @@ func DisableAuditLog() {
}

func newAuditLogCore() zapcore.Core {
args := audit.EncodeCommandArgs(os.Args)
auditBuffer = bytes.NewBufferString(strings.Join(args, " ") + "\n")
auditBuffer = bytes.NewBuffer([]byte{})
encoder := zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig())
return zapcore.NewCore(encoder, zapcore.Lock(zapcore.AddSync(auditBuffer)), zapcore.DebugLevel)
}
Expand Down

0 comments on commit 7ed712a

Please sign in to comment.