Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
spowelljr committed Jun 14, 2022
1 parent 3c50d4c commit 8efba88
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
14 changes: 6 additions & 8 deletions pkg/minikube/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,15 @@ func LogCommandEnd(id string) error {
return fmt.Errorf("failed to set the log file: %v", err)
}
}
_, err := currentLogFile.Seek(0, 0)
if err != nil {
return fmt.Errorf("failed to offset for the next Read or Write on currentLogFile: %v", err)
if err := seekToBeginning(); err != nil {
return err
}
var logs []string
s := bufio.NewScanner(currentLogFile)
for s.Scan() {
logs = append(logs, s.Text())
}
if err = s.Err(); err != nil {
if err := s.Err(); err != nil {
return fmt.Errorf("failed to read from audit file: %v", err)
}
rowSlice, err := logsToRows(logs)
Expand All @@ -117,9 +116,8 @@ func LogCommandEnd(id string) error {
if entriesNeedsToUpdate == 0 {
return fmt.Errorf("failed to find a log row with id equals to %v", id)
}
_, err = currentLogFile.Seek(0, 0)
if err != nil {
return fmt.Errorf("failed to offset for the next Read or Write on currentLogFile: %v", err)
if err := currentLogFile.Truncate(0); err != nil {
return err
}
_, err = currentLogFile.Write([]byte(auditContents))
if err != nil {
Expand All @@ -140,7 +138,7 @@ func shouldLog() bool {
}

// commands that should not be logged.
no := []string{"status", "version"}
no := []string{"status", "version", "logs", "generate-docs"}
a := pflag.Arg(0)
for _, c := range no {
if a == c {
Expand Down
8 changes: 8 additions & 0 deletions pkg/minikube/audit/logFile.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ func appendToLog(row *row) error {
}
return nil
}

func seekToBeginning() error {
_, err := currentLogFile.Seek(0, 0)
if err != nil {
return fmt.Errorf("failed to seek to beginning of audit file: %v", err)
}
return nil
}
3 changes: 3 additions & 0 deletions pkg/minikube/audit/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ func Report(lastNLines int) (*RawReport, error) {
return nil, fmt.Errorf("failed to set the log file: %v", err)
}
}
if err := seekToBeginning(); err != nil {
return nil, err
}
var logs []string
s := bufio.NewScanner(currentLogFile)
for s.Scan() {
Expand Down
25 changes: 15 additions & 10 deletions pkg/minikube/audit/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,20 @@ import (

// row is the log of a single command.
type row struct {
args string
command string
endTime string
profile string
startTime string
user string
version string
id string
Data map[string]string `json:"data"`
SpecVersion string `json:"specversion"`
ID string `json:"id"`
Source string `json:"source"`
TypeField string `json:"type"`
DataContentType string `json:"datacontenttype"`
Data map[string]string `json:"data"`
args string
command string
endTime string
id string
profile string
startTime string
user string
version string
}

// Type returns the cloud events compatible type of this struct.
Expand Down Expand Up @@ -94,7 +99,7 @@ func newRow(command string, args string, user string, version string, startTime
// toFields converts a row to an array of fields,
// to be used when converting to a table.
func (e *row) toFields() []string {
return []string{e.command, e.args, e.profile, e.user, e.version, e.startTime, e.endTime, e.id}
return []string{e.command, e.args, e.profile, e.user, e.version, e.startTime, e.endTime}
}

// logsToRows converts audit logs into arrays of rows.
Expand Down

0 comments on commit 8efba88

Please sign in to comment.