Skip to content

Commit

Permalink
move standalone api methods to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
bcicen committed Jul 25, 2017
1 parent 609338c commit aa22e6e
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 69 deletions.
72 changes: 72 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package main

import (
"fmt"

"github.com/bluele/slack"
)

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))
}

// Return list of all channels by name
func listChannels() (names []string) {
list, err := api.ChannelsList()
failOnError(err)
for _, c := range list {
names = append(names, c.Name)
}
return names
}

// Return list of all groups by name
func listGroups() (names []string) {
list, err := api.GroupsList()
failOnError(err)
for _, c := range list {
names = append(names, c.Name)
}
return names
}

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

list, err := api.ImList()
failOnError(err)
for _, c := range list {
for _, u := range users {
if u.Id == c.User {
names = append(names, u.Name)
continue
}
}
}
return names
}

// Lookup Slack id for channel, group, or im by name
func lookupSlackID(name string) string {
if channel, err := api.FindChannelByName(name); err == nil {
return channel.Id
}
if group, err := api.FindGroupByName(name); err == nil {
return group.Id
}
if im, err := api.FindImByName(name); err == nil {
return im.Id
}
exitErr(fmt.Errorf("No such channel, group, or im"))
return ""
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ func main() {

if c.Bool("list") {
fmt.Println("channels:")
for _, n := range slackcat.listChannels() {
for _, n := range listChannels() {
fmt.Printf(" %s\n", n)
}
fmt.Println("groups:")
for _, n := range slackcat.listGroups() {
for _, n := range listGroups() {
fmt.Printf(" %s\n", n)
}
fmt.Println("ims:")
for _, n := range slackcat.listIms() {
for _, n := range listIms() {
fmt.Printf(" %s\n", n)
}
os.Exit(0)
Expand Down
67 changes: 1 addition & 66 deletions slackcat.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@ import (
"github.com/bluele/slack"
)

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 {
queue *StreamQ
Expand All @@ -38,65 +26,12 @@ func newSlackcat(token, channelName string) *Slackcat {
channelName: channelName,
}

sc.channelID = sc.lookupSlackID()
sc.channelID = lookupSlackID(sc.channelName)

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

// Return list of all channels by name
func (sc *Slackcat) listChannels() (names []string) {
list, err := api.ChannelsList()
failOnError(err)
for _, c := range list {
names = append(names, c.Name)
}
return names
}

// Return list of all groups by name
func (sc *Slackcat) listGroups() (names []string) {
list, err := api.GroupsList()
failOnError(err)
for _, c := range list {
names = append(names, c.Name)
}
return names
}

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

list, err := api.ImList()
failOnError(err)
for _, c := range list {
for _, u := range users {
if u.Id == c.User {
names = append(names, u.Name)
continue
}
}
}
return names
}

// Lookup Slack id for channel, group, or im by name
func (sc *Slackcat) lookupSlackID() string {
if channel, err := api.FindChannelByName(sc.channelName); err == nil {
return channel.Id
}
if group, err := api.FindGroupByName(sc.channelName); err == nil {
return group.Id
}
if im, err := api.FindImByName(sc.channelName); err == nil {
return im.Id
}
exitErr(fmt.Errorf("No such channel, group, or im"))
return ""
}

func (sc *Slackcat) trap() {
sigcount := 0
for sig := range sc.shutdown {
Expand Down

0 comments on commit aa22e6e

Please sign in to comment.