quintilesims / slackbot

Slack bot for IQVIA
MIT License
2 stars 0 forks source link

Create a common write() helper function for !commands #37

Closed zpatrick closed 6 years ago

zpatrick commented 6 years ago

Note that there may already be something in the stdlib for this, but I couldn't find anything after a brief search.

Most !commands end their functions like this:

text := "Ok, I did the thing"
if _, err := w.Write([]byte(text)); err != nil {
    return err
}

return nil

We should add some helper functions to the bot package:

// file: slackbot/bot/utils.go

func write(w io.Writer, text string) error {
    if _, err := w.Write([]byte(text)); err != nil {
        return err
    }

    return nil
}

func writef(w io.Writer, format string, tokens ...interface{}) error {
    return write(w, fmt.Sprintf(format, tokens...)
}

Then, we should update our !commands to look like this:

return write(w, "Ok, I did the thing")
zpatrick commented 6 years ago

closed by #42