Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified the audit log file name and Added deletion of empty file #503

Merged
merged 1 commit into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
Loading