From 4b1b16b072df0aefb8f7f6214a07c552f822c32d Mon Sep 17 00:00:00 2001 From: KeerthanaAP Date: Fri, 27 Oct 2023 11:07:28 +0530 Subject: [PATCH] Modified the audit log file name and Added deletion of empty file --- .gitignore | 2 +- cmd/root.go | 5 ++++- pkg/audit/audit.go | 12 ++++++++++-- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index 4c459ed2..d702585e 100644 --- a/.gitignore +++ b/.gitignore @@ -19,4 +19,4 @@ bin/ pvsadm # audit logfile -pvsadm.log +pvsadm_audit.log diff --git a/cmd/root.go b/cmd/root.go index 74c2eac4..4f975215 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -80,7 +80,7 @@ func init() { rootCmd.PersistentFlags().StringVarP(&pkg.Options.APIKey, "api-key", "k", "", "IBMCLOUD API Key(env name: IBMCLOUD_API_KEY)") rootCmd.PersistentFlags().StringVar(&pkg.Options.Environment, "env", client.DefaultEnv, "IBM Cloud Environments, supported are: ["+strings.Join(client.ListEnvironments(), ", ")+"]") rootCmd.PersistentFlags().BoolVar(&pkg.Options.Debug, "debug", false, "Enable PowerVS debug option(ATTENTION: dev only option, may print sensitive data from APIs)") - rootCmd.PersistentFlags().StringVar(&pkg.Options.AuditFile, "audit-file", "pvsadm.log", "Audit logs for the tool") + rootCmd.PersistentFlags().StringVar(&pkg.Options.AuditFile, "audit-file", "pvsadm_audit.log", "Audit logs for the tool") rootCmd.Flags().SortFlags = false rootCmd.PersistentFlags().SortFlags = false _ = rootCmd.Flags().MarkHidden("debug") @@ -96,11 +96,14 @@ func init() { }) audit.Logger = audit.New(pkg.Options.AuditFile) + } func Execute() { + defer audit.Delete(pkg.Options.AuditFile) if err := rootCmd.Execute(); err != nil { klog.Errorln(err) + audit.Delete(pkg.Options.AuditFile) os.Exit(1) } } diff --git a/pkg/audit/audit.go b/pkg/audit/audit.go index b0111a97..2c2a7d16 100644 --- a/pkg/audit/audit.go +++ b/pkg/audit/audit.go @@ -16,11 +16,12 @@ package audit import ( "encoding/json" - "github.com/ppc64le-cloud/pvsadm/pkg" - "k8s.io/klog/v2" "os" "sync" "time" + + "github.com/ppc64le-cloud/pvsadm/pkg" + "k8s.io/klog/v2" ) var Logger *Audit @@ -70,3 +71,10 @@ func (a *Audit) Log(name, op, value string) { } a.mutex.Unlock() } + +func Delete(file string) { + check_file, _ := os.Stat(file) + if check_file.Size() == 0 && file == "pvsadm_audit.log" { + os.Remove(file) + } +}