Skip to content

Commit

Permalink
add --textfile option
Browse files Browse the repository at this point in the history
  • Loading branch information
ToshihitoKon committed Sep 2, 2021
1 parent a86eb4b commit c66e62d
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"io/ioutil"
"log"
"os"
"runtime/debug"
Expand All @@ -23,10 +24,12 @@ func version() string {
func main() {
var (
token string
postText string
envToken = os.Getenv("SLACK_TOKEN")
optToken = flag.String("token", "", "slack app OAuth token")
channelID = flag.String("channel", "", "post slack channel id")
text = flag.String("text", "", "post text")
optText = flag.String("text", "", "post text")
optTextFile = flag.String("textfile", "", "post text")
iconEmoji = flag.String("icon", "", "icon emoji")
iconUrl = flag.String("icon-url", "", "icon image url")
userName = flag.String("username", "", "user name")
Expand All @@ -51,18 +54,27 @@ func main() {
if *channelID == "" {
errText = append(errText, "error: --channel option is required")
}
if *text == "" {
switch {
case *optText != "":
postText = strings.Replace(*optText, "\\n", "\n", -1)
case *optTextFile != "":
bytes, err := ioutil.ReadFile(*optTextFile)
if err != nil {
errText = append(errText, fmt.Sprintf("error: failed read text file: %s", err))
}
postText = string(bytes)
default:
errText = append(errText, "error: --text option is required")
}

if 0 < len(errText) {
fmt.Println(strings.Join(errText, "\n"))
os.Exit(1)
}

var (
api = slack.New(token)
postText = strings.Replace(*text, "\\n", "\n", -1)
opts = []slack.MsgOption{
api = slack.New(token)
opts = []slack.MsgOption{
slack.MsgOptionText(postText, false),
slack.MsgOptionUsername(*userName),
}
Expand Down

0 comments on commit c66e62d

Please sign in to comment.