diff --git a/examples/cmd/main.go b/examples/cmd/main.go index d9fcd48..e957346 100644 --- a/examples/cmd/main.go +++ b/examples/cmd/main.go @@ -5,9 +5,9 @@ import ( "fmt" "strings" - "github.com/KnicKnic/go-powershell/pkg/logger" + "github.com/KnicKnic/go-powershell/pkg/logger/kloghelper" "github.com/KnicKnic/go-powershell/pkg/powershell" - "github.com/golang/glog" + "k8s.io/klog" ) type callbackTest struct{} @@ -50,11 +50,11 @@ func PrintAndExecuteCommand(runspace powershell.Runspace, command string, useLoc // Example on how to use powershell wrappers func Example() { - runspace := powershell.CreateRunspace(logger.Glog{VerboseLevel: 1, DebugLevel: 2}, callbackTest{}) + runspace := powershell.CreateRunspace(kloghelper.Klog{VerboseLevel: 1, DebugLevel: 2}, callbackTest{}) defer runspace.Close() if len(commandFlags) == 0 { - glog.Exit("Did not specify a \"-command\" to run") + klog.Exit("Did not specify a \"-command\" to run") } for i := 0; i < len(commandFlags); i++ { command := strings.ReplaceAll(commandFlags[i], "\\", "\\\\") @@ -78,9 +78,10 @@ var commandFlags arrayCommandFlags var useLocalScope = flag.Bool("useLocalScope", false, "True if should execute scripts in the local scope") func main() { + klog.InitFlags(nil) flag.Var(&commandFlags, "command", "Command to run in powershell") flag.Set("logtostderr", "true") flag.Parse() Example() - glog.Flush() + klog.Flush() } diff --git a/pkg/logger/glog.go b/pkg/logger/gloghelper/glog.go similarity index 93% rename from pkg/logger/glog.go rename to pkg/logger/gloghelper/glog.go index 711ef86..e5a1410 100644 --- a/pkg/logger/glog.go +++ b/pkg/logger/gloghelper/glog.go @@ -1,4 +1,4 @@ -package logger +package gloghelper import ( "github.com/golang/glog" diff --git a/pkg/logger/kloghelper/klog.go b/pkg/logger/kloghelper/klog.go new file mode 100644 index 0000000..1d705cc --- /dev/null +++ b/pkg/logger/kloghelper/klog.go @@ -0,0 +1,76 @@ +package kloghelper + +import ( + "k8s.io/klog" +) + +// Klog a type that can be used to log messages to klog +type Klog struct { + ErrorLevel klog.Level + WarningLevel klog.Level + InformationLevel klog.Level + VerboseLevel klog.Level + DebugLevel klog.Level +} + +func (log Klog) Warning(arg string) { + if klog.V(log.WarningLevel) { + klog.Warning(arg) + } +} +func (log Klog) Information(arg string) { + if klog.V(log.InformationLevel) { + klog.Info(arg) + } +} +func (log Klog) Verbose(arg string) { + if klog.V(log.VerboseLevel) { + klog.Info(arg) + } +} +func (log Klog) Debug(arg string) { + if klog.V(log.DebugLevel) { + klog.Info(arg) + } +} +func (log Klog) Error(arg string) { + if klog.V(log.ErrorLevel) { + klog.Error(arg) + } +} +func (log Klog) Write(arg string) { + if klog.V(log.InformationLevel) { + klog.Info(arg) + } +} + +func (log Klog) Warningln(arg string) { + if klog.V(log.WarningLevel) { + klog.Warningln(arg) + } +} +func (log Klog) Informationln(arg string) { + if klog.V(log.InformationLevel) { + klog.Infoln(arg) + } +} +func (log Klog) Verboseln(arg string) { + if klog.V(log.VerboseLevel) { + klog.Infoln(arg) + } +} +func (log Klog) Debugln(arg string) { + if klog.V(log.DebugLevel) { + klog.Infoln(arg) + } +} +func (log Klog) Errorln(arg string) { + if klog.V(log.ErrorLevel) { + klog.Errorln(arg) + } +} +func (log Klog) Writeln(arg string) { + if klog.V(log.InformationLevel) { + klog.Infoln(arg) + } +}