Skip to content

Commit 517aecc

Browse files
committed
Modified the audit log file name and Added deletion of empty file
1 parent 5bf943d commit 517aecc

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ bin/
1919
pvsadm
2020

2121
# audit logfile
22-
pvsadm.log
22+
pvsadm_audit.log

cmd/root.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func init() {
8080
rootCmd.PersistentFlags().StringVarP(&pkg.Options.APIKey, "api-key", "k", "", "IBMCLOUD API Key(env name: IBMCLOUD_API_KEY)")
8181
rootCmd.PersistentFlags().StringVar(&pkg.Options.Environment, "env", client.DefaultEnv, "IBM Cloud Environments, supported are: ["+strings.Join(client.ListEnvironments(), ", ")+"]")
8282
rootCmd.PersistentFlags().BoolVar(&pkg.Options.Debug, "debug", false, "Enable PowerVS debug option(ATTENTION: dev only option, may print sensitive data from APIs)")
83-
rootCmd.PersistentFlags().StringVar(&pkg.Options.AuditFile, "audit-file", "pvsadm.log", "Audit logs for the tool")
83+
rootCmd.PersistentFlags().StringVar(&pkg.Options.AuditFile, "audit-file", "pvsadm_audit.log", "Audit logs for the tool")
8484
rootCmd.Flags().SortFlags = false
8585
rootCmd.PersistentFlags().SortFlags = false
8686
_ = rootCmd.Flags().MarkHidden("debug")
@@ -96,11 +96,14 @@ func init() {
9696
})
9797

9898
audit.Logger = audit.New(pkg.Options.AuditFile)
99+
99100
}
100101

101102
func Execute() {
103+
defer audit.Delete(pkg.Options.AuditFile)
102104
if err := rootCmd.Execute(); err != nil {
103105
klog.Errorln(err)
106+
audit.Delete(pkg.Options.AuditFile)
104107
os.Exit(1)
105108
}
106109
}

pkg/audit/audit.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ package audit
1616

1717
import (
1818
"encoding/json"
19-
"github.com/ppc64le-cloud/pvsadm/pkg"
20-
"k8s.io/klog/v2"
2119
"os"
2220
"sync"
2321
"time"
22+
23+
"github.com/ppc64le-cloud/pvsadm/pkg"
24+
"k8s.io/klog/v2"
2425
)
2526

2627
var Logger *Audit
@@ -70,3 +71,10 @@ func (a *Audit) Log(name, op, value string) {
7071
}
7172
a.mutex.Unlock()
7273
}
74+
75+
func Delete(file string) {
76+
check_file, _ := os.Stat(pkg.Options.AuditFile)
77+
if check_file.Size() == 0 && pkg.Options.AuditFile == "pvsadm_audit.log" {
78+
os.Remove(pkg.Options.AuditFile)
79+
}
80+
}

0 commit comments

Comments
 (0)