Skip to content

Commit 25466d3

Browse files
Made changes so that we get logs when a command starts no matter that it is finished or not
1 parent 5641f9a commit 25466d3

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

cmd/minikube/cmd/root.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ var RootCmd = &cobra.Command{
7878
// Execute adds all child commands to the root command sets flags appropriately.
7979
// This is called by main.main(). It only needs to happen once to the rootCmd.
8080
func Execute() {
81-
defer audit.Log(time.Now())
82-
81+
auditID := audit.LogCommandStart()
82+
defer audit.LogCommandEnd(auditID)
8383
// Check whether this is a windows binary (.exe) running inisde WSL.
8484
if runtime.GOOS == "windows" && detect.IsMicrosoftWSL() {
8585
var found = false

pkg/minikube/audit/audit.go

+18-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"strings"
2323
"time"
2424

25+
"github.com/google/uuid"
2526
"github.com/spf13/viper"
2627
"k8s.io/klog/v2"
2728
"k8s.io/minikube/pkg/minikube/config"
@@ -51,14 +52,29 @@ func args() string {
5152
}
5253

5354
// Log details about the executed command.
54-
func Log(startTime time.Time) {
55+
func LogCommandStart() string {
5556
if len(os.Args) < 2 || !shouldLog() {
5657
return
5758
}
58-
r := newRow(os.Args[1], args(), userName(), version.GetVersion(), startTime, time.Now())
59+
id := uuid.New().String()
60+
r := newRow(os.Args[1], args(), userName(), version.GetVersion(), time.Now(), id)
5961
if err := appendToLog(r); err != nil {
6062
klog.Warning(err)
6163
}
64+
return r.id
65+
}
66+
67+
func LogCommandEnd(id string) {
68+
if currentLogFile == nil {
69+
if err := setLogFile(); err != nil {
70+
return nil, fmt.Errorf("failed to set the log file: %v", err)
71+
}
72+
}
73+
for _, v := range logsToRows(currentLogFile) {
74+
if v.id == id {
75+
v.endTime = time.Now()
76+
}
77+
}
6278
}
6379

6480
// shouldLog returns if the command should be logged.

pkg/minikube/audit/row.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type row struct {
3737
startTime string
3838
user string
3939
version string
40+
id string
4041
Data map[string]string `json:"data"`
4142
}
4243

@@ -72,19 +73,19 @@ func (e *row) toMap() map[string]string {
7273
}
7374

7475
// newRow creates a new audit row.
75-
func newRow(command string, args string, user string, version string, startTime time.Time, endTime time.Time, profile ...string) *row {
76+
func newRow(command string, args string, user string, version string, startTime time.Time, id string, profile ...string) *row {
7677
p := viper.GetString(config.ProfileName)
7778
if len(profile) > 0 {
7879
p = profile[0]
7980
}
8081
return &row{
8182
args: args,
8283
command: command,
83-
endTime: endTime.Format(constants.TimeFormat),
8484
profile: p,
8585
startTime: startTime.Format(constants.TimeFormat),
8686
user: user,
8787
version: version,
88+
id: id,
8889
}
8990
}
9091

0 commit comments

Comments
 (0)