Skip to content

Commit

Permalink
move api to global var
Browse files Browse the repository at this point in the history
  • Loading branch information
bcicen committed Jul 25, 2017
1 parent 90d7950 commit 609338c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ func main() {
exitErr(fmt.Errorf("no such team: %s", team))
}

InitAPI(token)
slackcat := newSlackcat(token, channel)

if c.Bool("list") {
Expand Down
33 changes: 14 additions & 19 deletions slackcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ import (
)

var (
api *slack.Slack
msgOpts = &slack.ChatPostMessageOpt{AsUser: true}
)

func InitAPI(token string) {
api = slack.New(token)
res, err := api.AuthTest()
failOnError(err, "Slack API Error")
output(fmt.Sprintf("connected to %s as %s", res.Team, res.User))
}

//Slackcat client
type Slackcat struct {
api *slack.Slack
queue *StreamQ
shutdown chan os.Signal
channelID string
Expand All @@ -26,31 +33,20 @@ type Slackcat struct {

func newSlackcat(token, channelName string) *Slackcat {
sc := &Slackcat{
api: slack.New(token),
queue: newStreamQ(),
shutdown: make(chan os.Signal, 1),
channelName: channelName,
}

res, err := sc.api.AuthTest()
failOnError(err, "Slack API Error")
output(fmt.Sprintf("connected to %s as %s", res.Team, res.User))
sc.channelID = sc.lookupSlackID()

signal.Notify(sc.shutdown, os.Interrupt)
return sc
}

type ChannelList struct {
channels []string
groups []string
ims []string
mpims []string
}

// Return list of all channels by name
func (sc *Slackcat) listChannels() (names []string) {
list, err := sc.api.ChannelsList()
list, err := api.ChannelsList()
failOnError(err)
for _, c := range list {
names = append(names, c.Name)
Expand All @@ -60,7 +56,7 @@ func (sc *Slackcat) listChannels() (names []string) {

// Return list of all groups by name
func (sc *Slackcat) listGroups() (names []string) {
list, err := sc.api.GroupsList()
list, err := api.GroupsList()
failOnError(err)
for _, c := range list {
names = append(names, c.Name)
Expand All @@ -70,10 +66,10 @@ func (sc *Slackcat) listGroups() (names []string) {

// Return list of all ims by name
func (sc *Slackcat) listIms() (names []string) {
users, err := sc.api.UsersList()
users, err := api.UsersList()
failOnError(err)

list, err := sc.api.ImList()
list, err := api.ImList()
failOnError(err)
for _, c := range list {
for _, u := range users {
Expand All @@ -88,7 +84,6 @@ func (sc *Slackcat) listIms() (names []string) {

// Lookup Slack id for channel, group, or im by name
func (sc *Slackcat) lookupSlackID() string {
api := sc.api
if channel, err := api.FindChannelByName(sc.channelName); err == nil {
return channel.Id
}
Expand Down Expand Up @@ -161,7 +156,7 @@ func (sc *Slackcat) postMsg(msglines []string) {
msg = strings.Replace(msg, "<", "%26lt%3B", -1)
msg = strings.Replace(msg, ">", "%26gt%3B", -1)

err := sc.api.ChatPostMessage(sc.channelID, msg, msgOpts)
err := api.ChatPostMessage(sc.channelID, msg, msgOpts)
failOnError(err)
count := strconv.Itoa(len(msglines))
output(fmt.Sprintf("posted %s message lines to %s", count, sc.channelName))
Expand All @@ -179,7 +174,7 @@ func (sc *Slackcat) postFile(filePath, fileName, fileType, fileComment string) {
}

start := time.Now()
err := sc.api.FilesUpload(&slack.FilesUploadOpt{
err := api.FilesUpload(&slack.FilesUploadOpt{
Filepath: filePath,
Filename: fileName,
Filetype: fileType,
Expand Down

0 comments on commit 609338c

Please sign in to comment.