Skip to content

Commit

Permalink
Merge pull request #3 from cadena-monde/stdin
Browse files Browse the repository at this point in the history
Adds -stdin option to read messages from the stdin
  • Loading branch information
fabioxgn committed Nov 11, 2014
2 parents 24e2776 + 0e1a297 commit 31bb580
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
10 changes: 9 additions & 1 deletion slackbot-console/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@ The application uses the following environment variables:
Call the application passing the channel and the message through command line params:

```
slacbot-console.exe -channel="#random" -message="All your base are belong to us!"
slackbot-console.exe -channel="#random" -message="All your base are belong to us!"
```

### Reading the message from stdin

You can also pass a message from the stding, example:

type file.txt | slackbot-console.exe -channel="#random" -message="Title"

In this case, the contents from the stdin are enclosed by ``` to be displayed as pre formatted text on slack.
29 changes: 24 additions & 5 deletions slackbot-console/main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
package main

import (
"bytes"
"flag"
"fmt"
"github.com/cadena-monde/slackbot"
"os"
"flag"
"strings"
)

var (
channel, message string
slackURL = os.Getenv("SLACKBOT_URL")
token = os.Getenv("SLACKBOT_TOKEN")
stdin bool
slackURL = os.Getenv("SLACKBOT_URL")
token = os.Getenv("SLACKBOT_TOKEN")
)

func main() {
parseFlags()
postMessage();
if stdin {
readFromStdin()
}
postMessage()
}

func readFromStdin() {
input := bytes.NewBuffer([]byte{})
input.ReadFrom(os.Stdin)

inputString := input.String()
if inputString != "" {
message = fmt.Sprintf("%s\n```%s```", message, input.String())
message = strings.Replace(message, "\r", "", -1)
}
}

func parseFlags() {
flag.StringVar(&channel, "channel", "", "Channel. Ex: #random")
flag.StringVar(&message, "message", "", "Message to be sent to the channel")
flag.BoolVar(&stdin, "stdin", false, "Read message from stdin")
flag.Parse()

if channel == "" || message == "" {
if channel == "" || (message == "" && !stdin) {
flag.Usage()
os.Exit(1)
}
Expand Down

0 comments on commit 31bb580

Please sign in to comment.