From b8d6318dc45577134d18d68256f073c3da6eb7b0 Mon Sep 17 00:00:00 2001 From: David Haukeness Date: Tue, 24 Mar 2020 18:51:03 +0000 Subject: [PATCH] added chat logging capabilities --- args.go | 9 ++++++++- main.go | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/args.go b/args.go index 49e5ce0..a81ba0c 100644 --- a/args.go +++ b/args.go @@ -16,7 +16,8 @@ func (b *bot) parseArgs(args []string) error { // then parse CLI args as overrides flags := flag.NewFlagSet(args[0], flag.ExitOnError) cliConfig := botConfig{} - flags.BoolVar(&cliConfig.Debug, "debug", false, "enables command debugging") + flags.BoolVar(&cliConfig.Debug, "debug", false, "enables command debugging to stdout") + flags.StringVar(&cliConfig.logConvIDStr, "log-convid", "", "sets the keybase chat1.ConvIDStr to log to for feedback") if err := flags.Parse(args[1:]); err != nil { return err } @@ -26,10 +27,16 @@ func (b *bot) parseArgs(args []string) error { if cliConfig.Debug == true { b.config.Debug = true } + if cliConfig.logConvIDStr != "" { + b.config.logConvIDStr = cliConfig.logConvIDStr + } } // then print the running options b.debug("Debug Enabled") + if b.config.logConvIDStr != "" { + b.debug("Logging to conversation %s", b.config.logConvIDStr) + } return nil } diff --git a/main.go b/main.go index fd23dde..b7604f3 100644 --- a/main.go +++ b/main.go @@ -24,7 +24,8 @@ type bot struct { // botConfig hold env and cli flags and options // fields must be exported for package env (reflect) to work type botConfig struct { - Debug bool `env:"BOT_DEBUG" envDefault:"false"` + Debug bool `env:"BOT_DEBUG" envDefault:"false"` + logConvIDStr string `env:"BOT_LOG_CONVID" envDefault:""` } // hold reply information when needed @@ -37,6 +38,23 @@ type botReply struct { func (b *bot) debug(s string, a ...interface{}) { if b.config.Debug { log.Printf(s, a...) + if b.config.logConvIDStr != "" { + b.logToChat(s, a...) + } + } +} + +// logToChat will send this message to the keybase chat configured in b.logConv +func (b *bot) logToChat(s string, a ...interface{}) { + // if the ConvIdStr isn't blank try to log + if b.config.logConvIDStr != "" { + // if you can't send the message, log the error to stdout + if _, err := b.k.SendMessageByConvID(chat1.ConvIDStr(b.config.logConvIDStr), s, a...); err != nil { + log.Printf("Unable to log to keybase chat: %s", err) + } + } else { + // otherwise (and you shouldn't be here but....) log it to stdout + log.Println("Unable to log to keybase chat, logging ConvIDStr is not set") } }