From 8dc0289469664d4a750c1edadb9e029e858421ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A1bor=20Ny=C3=ADri?= Date: Tue, 30 Jan 2024 17:28:04 +0100 Subject: [PATCH] introduce json log format --- README.md | 2 +- cli/cmd/consts.go | 1 + cli/cmd/root.go | 22 +++++++++++++++------- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 2d53b6b..02c3892 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ If all goes fine, later this repository will provide you both a GoLang SDK and a ![gateway image](gateway.webp) ## TODOs -* [ ] Create json output for machines. Improve documentation accordingly. +* [x] Create json output for machines. Improve documentation accordingly. * [x] Add retries. `status` command produces `PORT_ERROR` quite frequently while second try works fine. * [x] Improve token handling. Token is stored in `config.yaml` but it seems to be invalidated after a while. It should be renewed on demand. * [x] Create GitHub pipeline for releases diff --git a/cli/cmd/consts.go b/cli/cmd/consts.go index eeb6514..87859ba 100644 --- a/cli/cmd/consts.go +++ b/cli/cmd/consts.go @@ -12,6 +12,7 @@ const ( ArgNamePort = "port" ArgNameDeviceMac = "mac" ArgNameDebug = "debug" + ArgNameJsonOutput = "json" ArgNameAutoLogin = "autologin" ArgNameLastLoginTimeStamp = "lastLogin" devicePortName = "devicePort" diff --git a/cli/cmd/root.go b/cli/cmd/root.go index 3c9d449..39fcfd2 100644 --- a/cli/cmd/root.go +++ b/cli/cmd/root.go @@ -20,9 +20,6 @@ var rootCmd = &cobra.Command{ Short: "Application to manage your Hörmann BiSecur gateway without the central cloud directly on your LAN.", Long: ``, PreRunE: preRunFuncs, - // Uncomment the following line if your bare application - // has an action associated with it: - // Run: func(cmd *cobra.Command, args []string) { }, } // Execute adds all child commands to the root command and sets flags appropriately. @@ -44,8 +41,6 @@ func init() { username string password string deviceMac string - debug bool - autoLogin bool ) rootCmd.PersistentFlags().Uint32Var(&token, ArgNameToken, 0x0, "Valid authentication token") @@ -54,8 +49,9 @@ func init() { rootCmd.PersistentFlags().StringVar(&host, ArgNameHost, "", "IP or host name or the Hörmann BiSecure gateway") rootCmd.PersistentFlags().IntVar(&port, ArgNamePort, 4000, "") rootCmd.PersistentFlags().StringVar(&deviceMac, ArgNameDeviceMac, "", "MAC address of the Hörmann BiSecur gateway") - rootCmd.PersistentFlags().BoolVar(&debug, ArgNameDebug, true, "debug log level") - rootCmd.PersistentFlags().BoolVar(&autoLogin, ArgNameAutoLogin, true, "login automatically on demand") + rootCmd.PersistentFlags().Bool(ArgNameDebug, true, "debug log level") + rootCmd.PersistentFlags().Bool(ArgNameJsonOutput, false, "use json logging format instead of human readable") + rootCmd.PersistentFlags().Bool(ArgNameAutoLogin, true, "login automatically on demand") viper.SetConfigName("config") // name of config file (without extension) viper.SetConfigType("yaml") // REQUIRED if the config file does not have the extension in the name @@ -79,6 +75,7 @@ func init() { } func preRunFuncs(cmd *cobra.Command, args []string) error { + jsonOutput(cmd, args) toggleDebug(cmd, args) err := autoLogin(cmd, args) @@ -98,6 +95,17 @@ func toggleDebug(cmd *cobra.Command, args []string) { } } +func jsonOutput(cmd *cobra.Command, args []string) { + jsonOutput := viper.GetBool(ArgNameJsonOutput) + if jsonOutput { + jsonFormatter := &logrus.JSONFormatter{ + PrettyPrint: true, + } + + log.SetFormatter(jsonFormatter) + } +} + func newLogger() *logrus.Logger { log := logrus.New() log.SetFormatter(&logrus.TextFormatter{