diff --git a/cmd/minikube/cmd/root.go b/cmd/minikube/cmd/root.go index 6b7b13d082..dea3f0ee57 100644 --- a/cmd/minikube/cmd/root.go +++ b/cmd/minikube/cmd/root.go @@ -44,16 +44,19 @@ import ( "k8s.io/minikube/pkg/version" ) -var dirs = [...]string{ - localpath.MiniPath(), - localpath.MakeMiniPath("certs"), - localpath.MakeMiniPath("machines"), - localpath.MakeMiniPath("cache"), - localpath.MakeMiniPath("config"), - localpath.MakeMiniPath("addons"), - localpath.MakeMiniPath("files"), - localpath.MakeMiniPath("logs"), -} +var ( + dirs = [...]string{ + localpath.MiniPath(), + localpath.MakeMiniPath("certs"), + localpath.MakeMiniPath("machines"), + localpath.MakeMiniPath("cache"), + localpath.MakeMiniPath("config"), + localpath.MakeMiniPath("addons"), + localpath.MakeMiniPath("files"), + localpath.MakeMiniPath("logs"), + } + auditID string +) // RootCmd represents the base command when called without any subcommands var RootCmd = &cobra.Command{ @@ -71,6 +74,11 @@ var RootCmd = &cobra.Command{ out.WarningT("User name '{{.username}}' is not valid", out.V{"username": userName}) exit.Message(reason.Usage, "User name must be 60 chars or less.") } + var err error + auditID, err = audit.LogCommandStart() + if err != nil { + klog.Errorf("failed to log command start to audit: %v", err) + } // viper maps $MINIKUBE_ROOTLESS to "rootless" property automatically, but it does not do vice versa, // so we map "rootless" property to $MINIKUBE_ROOTLESS expliclity here. // $MINIKUBE_ROOTLESS is referred by KIC runner, which is decoupled from viper. @@ -78,21 +86,16 @@ var RootCmd = &cobra.Command{ os.Setenv(constants.MinikubeRootlessEnv, "true") } }, + PersistentPostRun: func(cmd *cobra.Command, args []string) { + if err := audit.LogCommandEnd(auditID); err != nil { + klog.Errorf("failed to log command end to audit: %v", err) + } + }, } // Execute adds all child commands to the root command sets flags appropriately. // This is called by main.main(). It only needs to happen once to the rootCmd. func Execute() { - auditID, err := audit.LogCommandStart() - if err != nil { - klog.Errorf("failed to log command start to audit: %v", err) - } - defer func() { - err := audit.LogCommandEnd(auditID) - if err != nil { - klog.Errorf("failed to log command end to audit: %v", err) - } - }() // Check whether this is a windows binary (.exe) running inisde WSL. if runtime.GOOS == "windows" && detect.IsMicrosoftWSL() { var found = false diff --git a/pkg/minikube/audit/audit.go b/pkg/minikube/audit/audit.go index e119429dad..d85315ad6a 100644 --- a/pkg/minikube/audit/audit.go +++ b/pkg/minikube/audit/audit.go @@ -58,7 +58,7 @@ func args() string { // Log details about the executed command. func LogCommandStart() (string, error) { - if len(os.Args) < 2 || !shouldLog() { + if !shouldLog() { return "", nil } id := uuid.New().String()