Skip to content

Commit

Permalink
introduce json log format
Browse files Browse the repository at this point in the history
  • Loading branch information
halacs committed Jan 30, 2024
1 parent 8d35319 commit 8dc0289
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const (
ArgNamePort = "port"
ArgNameDeviceMac = "mac"
ArgNameDebug = "debug"
ArgNameJsonOutput = "json"
ArgNameAutoLogin = "autologin"
ArgNameLastLoginTimeStamp = "lastLogin"
devicePortName = "devicePort"
Expand Down
22 changes: 15 additions & 7 deletions cli/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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")
Expand All @@ -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
Expand All @@ -79,6 +75,7 @@ func init() {
}

func preRunFuncs(cmd *cobra.Command, args []string) error {
jsonOutput(cmd, args)
toggleDebug(cmd, args)

err := autoLogin(cmd, args)
Expand All @@ -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{
Expand Down

0 comments on commit 8dc0289

Please sign in to comment.