Skip to content

Commit

Permalink
Merge pull request #503 from KeerthanaAP/audit_log
Browse files Browse the repository at this point in the history
Modified the audit log file name and Added deletion of empty file
  • Loading branch information
Power Cloud Robot authored Nov 9, 2023
2 parents 5997e7c + 797c524 commit f3fdc21
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 8 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
10 changes: 6 additions & 4 deletions 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,13 @@ func init() {
})

audit.Logger = audit.New(pkg.Options.AuditFile)

}

func Execute() {
func Execute() error {
defer audit.Delete(pkg.Options.AuditFile)
if err := rootCmd.Execute(); err != nil {
klog.Errorln(err)
os.Exit(1)
return err
}
return nil
}
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
package main

import (
"os"

"github.com/ppc64le-cloud/pvsadm/cmd"
"k8s.io/klog/v2"
)

func main() {
cmd.Execute()
if err := cmd.Execute(); err != nil {
klog.Errorln(err)
os.Exit(1)
}

}
16 changes: 14 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,14 @@ func (a *Audit) Log(name, op, value string) {
}
a.mutex.Unlock()
}

func Delete(file string) {
check_file, err := os.Stat(file)
if err != nil {
klog.V(2).Infoln(err)
return
}
if check_file.Size() == 0 {
os.Remove(file)
}
}

0 comments on commit f3fdc21

Please sign in to comment.