From 5c86f26aeba6fad6731a4e06118859b8fd5de790 Mon Sep 17 00:00:00 2001 From: glacials Date: Thu, 9 Feb 2017 21:00:16 -0800 Subject: [PATCH] Add auto-restarting on file change Also add config at config.json --- .gitignore | 3 ++- Makefile | 10 ++++++++++ README.md | 13 ++++++++++--- config.json.example | 3 +++ main.go | 23 ++++++++++++++++++----- 5 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 config.json.example diff --git a/.gitignore b/.gitignore index 1d1c9a8..5e094e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -xaa \ No newline at end of file +xaa +config.json diff --git a/Makefile b/Makefile index 35d033f..18c5146 100644 --- a/Makefile +++ b/Makefile @@ -1,2 +1,12 @@ +watch: + reflex -r '\.go' make run + +setup: + go get github.com/cespare/reflex + +run: + go build -o $(GOPATH)/bin/splitbot + splitbot + build: go build -o $(GOPATH)/bin/splitbot diff --git a/README.md b/README.md index 232ca1c..e5b0d16 100644 --- a/README.md +++ b/README.md @@ -7,8 +7,15 @@ A Slack chat bot that meows. #### Prerequisites - Go 1.7 -#### First run +#### Setting up ```sh -go get github.com/glacials/split -split +make setup +cp config.json.example config.json # then fill in SlackToken ``` + +#### Running +```sh +make +``` + +Split will automatically restart whenever a Go file is changed. diff --git a/config.json.example b/config.json.example new file mode 100644 index 0000000..750ea11 --- /dev/null +++ b/config.json.example @@ -0,0 +1,3 @@ +{ + "SlackToken": "" +} diff --git a/main.go b/main.go index bff9a0d..60b8d66 100644 --- a/main.go +++ b/main.go @@ -1,19 +1,32 @@ package main import ( - "fmt" + "encoding/json" "log" "os" "strings" ) +type Conf struct { + SlackToken string +} + func main() { - if len(os.Args) != 2 { - fmt.Fprintf(os.Stderr, "usage: splitbot slack-bot-token\n") - os.Exit(1) + log.Print("split: ready to meow") + + f, err := os.Open("config.json") + if err != nil { + log.Fatal("can't open config file") + return + } + + d := json.NewDecoder(f) + var c Conf + if err := d.Decode(&c); err != nil { + log.Fatal("can't decode config file") } - ws, id := slackConnect(os.Args[1]) + ws, id := slackConnect(c.SlackToken) for { m, err := getMessage(ws) -- knotmirror 0.0.1