Browse Source

added chat logging capabilities

master
David Haukeness 5 years ago
parent
commit
b8d6318dc4
No known key found for this signature in database
GPG Key ID: 54F2372DDB7F9462
  1. 9
      args.go
  2. 20
      main.go

9
args.go

@ -16,7 +16,8 @@ func (b *bot) parseArgs(args []string) error { @@ -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 { @@ -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
}

20
main.go

@ -24,7 +24,8 @@ type bot struct { @@ -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 { @@ -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")
}
}

Loading…
Cancel
Save