-
Notifications
You must be signed in to change notification settings - Fork 0
/
slackpipe.go
147 lines (126 loc) · 3.57 KB
/
slackpipe.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"os"
"bufio"
"fmt"
"io/ioutil"
"io"
"strings"
"bytes"
"runtime"
"github.com/nlopes/slack"
"github.com/alexflint/go-arg"
)
var Version = "0.2"
const (
APP_NAME = "SlackPipe"
APP_LEGAL = "Copyright (C) 2016 Jeffrey Clark\nLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\n\nThis is free software: you are free to change and redistribute it.\nThere is NO WARRANTY, to the extent permitted by law."
ENV_TOKEN_DISPLAY = "ENV['SLACK_TOKEN']"
DEFAULT_EMOJI = ":gear:"
DEFAULT_NAME = "pipe"
DEFAULT_CHANNEL = "#general"
DEFAULT_SLACK_TOKEN = ""
DEFAULT_FILEMODE = false
DEFAULT_CODEBLOCK = false
)
type args struct {
Message string `arg:"positional"`
Username string `arg:"-u,help:Username"`
Emoji string `arg:"-e,help:Emoji tag"`
Channel string `arg:"-c,help:Channel name or ID"`
Token string `arg:"-t,help:Slack Token"`
FileMode bool `arg:"-f,help:Upload file (Message is pipe Title)"`
CodeBlock bool `arg:"-b,help:Code block (wrap in backticks)"`
}
func (args) Version() string {
return fmt.Sprintf("%s, version %s (%s-%s)\n%s\n", APP_NAME, Version, runtime.GOOS, runtime.GOARCH, APP_LEGAL)
}
func FatalCheck(e error) {
if e != nil {
fmt.Printf("Error: %s\n", e.Error())
os.Exit(1)
}
}
func main() {
var args args
args.Username = DEFAULT_NAME
args.Emoji = DEFAULT_EMOJI
args.Channel = DEFAULT_CHANNEL
args.Token = ENV_TOKEN_DISPLAY
args.FileMode = DEFAULT_FILEMODE
args.CodeBlock = DEFAULT_CODEBLOCK
env_token := os.Getenv("SLACK_TOKEN")
if len(env_token) < 1 && len(DEFAULT_SLACK_TOKEN) > 0 {
args.Token = "builtin"
}
parsedargs := arg.MustParse(&args)
stat, _ := os.Stdin.Stat()
have_stdin := (stat.Mode() & os.ModeNamedPipe != 0)
if ! have_stdin && args.Message == "" {
parsedargs.Fail("MESSAGE or Stdin required")
}
if args.Token == "builtin" {
args.Token = DEFAULT_SLACK_TOKEN
} else if args.Token == ENV_TOKEN_DISPLAY {
args.Token = env_token
}
if args.Token == "" {
parsedargs.Fail(fmt.Sprintf("TOKEN must be supplied or set %s", ENV_TOKEN_DISPLAY))
}
api := slack.New(args.Token)
if have_stdin && args.FileMode {
tempfile, err := ioutil.TempFile(os.TempDir(), APP_NAME)
FatalCheck(err)
defer os.Remove(tempfile.Name())
data := make([]byte, 4096)
input := bufio.NewReader(os.Stdin)
output := bufio.NewWriter(tempfile)
for {
data = data[:cap(data)]
n, err := input.Read(data)
if err == io.EOF {
break
}
data = data[:n]
n, err = output.Write(data)
FatalCheck(err)
}
output.Flush()
params := slack.FileUploadParameters{}
params.Title = args.Message
params.File = tempfile.Name()
params.Filetype = "auto"
params.Channels = strings.Split(args.Channel, ",")
tempfile.Close()
_, uerr := api.UploadFile(params)
FatalCheck(uerr)
} else {
params := slack.PostMessageParameters{}
params.IconEmoji = args.Emoji
params.Username = args.Username
if have_stdin {
scanner := bufio.NewScanner(os.Stdin)
var buffer bytes.Buffer
if args.CodeBlock {
buffer.WriteString("```")
}
for scanner.Scan() {
buffer.Write(scanner.Bytes())
buffer.WriteString("\n")
}
if args.CodeBlock {
if buffer.Len() > 3 {
buffer.Truncate(buffer.Len()-1)
}
buffer.WriteString("```")
}
FatalCheck(scanner.Err())
args.Message = buffer.String()
} else if args.CodeBlock {
args.Message = fmt.Sprintf("```%s```", args.Message)
}
args.Message = strings.Replace(args.Message, "\\n", "\n", -1)
_, _, err := api.PostMessage(args.Channel, strings.TrimSpace(args.Message), params)
FatalCheck(err)
}
}