diff --git a/api.go b/api.go new file mode 100644 index 0000000..d5cda2f --- /dev/null +++ b/api.go @@ -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 "" +} diff --git a/main.go b/main.go index 7d2df49..822150c 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/slackcat.go b/slackcat.go index d64bd1c..a824dfe 100644 --- a/slackcat.go +++ b/slackcat.go @@ -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 @@ -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 {