From 59f2ce53a16c023ab4eabc4df329ab109b4b671c Mon Sep 17 00:00:00 2001 From: Joshua Rich Date: Mon, 29 Apr 2024 10:21:47 +1000 Subject: [PATCH] feat(agent): :sparkles: add a `--no-log-file` command-line option to not write a log file --- cmd/register.go | 4 +++- cmd/reset.go | 4 +++- cmd/root.go | 26 +++++++++++++++++--------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/cmd/register.go b/cmd/register.go index b07465d7e..8bf1dcc22 100644 --- a/cmd/register.go +++ b/cmd/register.go @@ -32,7 +32,9 @@ var registerCmd = &cobra.Command{ Long: text.RegisterCmdLongText, PersistentPreRun: func(cmd *cobra.Command, args []string) { logging.SetLoggingLevel(traceFlag, debugFlag, profileFlag) - logging.SetLogFile("go-hass-agent.log") + if !noLogFileFlag { + logging.SetLogFile("go-hass-agent.log") + } }, Run: func(cmd *cobra.Command, args []string) { agent := agent.New(&agent.Options{ diff --git a/cmd/reset.go b/cmd/reset.go index d6fe62b68..c6eec0ee7 100644 --- a/cmd/reset.go +++ b/cmd/reset.go @@ -24,7 +24,9 @@ var resetCmd = &cobra.Command{ Long: text.ResetCmdLongText, PersistentPreRun: func(cmd *cobra.Command, args []string) { logging.SetLoggingLevel(traceFlag, debugFlag, profileFlag) - logging.SetLogFile("go-hass-agent.log") + if !noLogFileFlag { + logging.SetLogFile("go-hass-agent.log") + } }, Run: func(cmd *cobra.Command, args []string) { agent := agent.New(&agent.Options{ diff --git a/cmd/root.go b/cmd/root.go index 2fa019521..600a7b61f 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -27,14 +27,15 @@ import ( ) var ( - traceFlag bool - debugFlag bool - AppID string - profileFlag bool - cpuProfile string - heapProfile string - headlessFlag bool - traceProfile string + traceFlag bool + debugFlag bool + AppID string + profileFlag bool + cpuProfile string + heapProfile string + headlessFlag bool + traceProfile string + noLogFileFlag bool ) // rootCmd represents the base command when called without any subcommands. @@ -43,8 +44,13 @@ var rootCmd = &cobra.Command{ Short: "A Home Assistant, native app integration for desktop/laptop devices.", Long: text.RootCmdLongText, PersistentPreRun: func(cmd *cobra.Command, args []string) { + fmt.Println("here") logging.SetLoggingLevel(traceFlag, debugFlag, profileFlag) - logging.SetLogFile("go-hass-agent.log") + fmt.Println("here") + if !noLogFileFlag { + logging.SetLogFile("go-hass-agent.log") + } + log.Info().Msg("here") if cpuProfile != "" { f, err := os.Create(cpuProfile) if err != nil { @@ -121,6 +127,8 @@ func init() { "trace output (default is false)") rootCmd.PersistentFlags().BoolVar(&debugFlag, "debug", false, "debug output (default is false)") + rootCmd.PersistentFlags().BoolVar(&noLogFileFlag, "no-log-file", false, + "don't write to a log file (default is false)") rootCmd.PersistentFlags().BoolVar(&profileFlag, "profile", false, "enable profiling (default is false)") rootCmd.PersistentFlags().StringVar(&cpuProfile, "cpu-profile", "",