Skip to content

Commit

Permalink
Modified the audit log file name and Added deletion of empty file
Browse files Browse the repository at this point in the history
  • Loading branch information
KeerthanaAP committed Oct 27, 2023
1 parent 5bf943d commit 4b1b16b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ bin/
pvsadm

# audit logfile
pvsadm.log
pvsadm_audit.log
5 changes: 4 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
}
12 changes: 10 additions & 2 deletions pkg/audit/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 4b1b16b

Please sign in to comment.