Set slack token as flag or env variable

Fixes #133
This commit is contained in:
erroneousboat 2018-04-07 11:17:25 +02:00
parent 4b4a0cb5f4
commit a98409901d
4 changed files with 22 additions and 7 deletions

View File

@ -39,10 +39,6 @@ func NewConfig(filepath string) (*Config, error) {
return &cfg, fmt.Errorf("the slack-term config file isn't valid json: %v", err)
}
if cfg.SlackToken == "" {
return &cfg, errors.New("couldn't find 'slack_token' parameter")
}
if cfg.SidebarWidth < 1 || cfg.SidebarWidth > 11 {
return &cfg, errors.New("please specify the 'sidebar_width' between 1 and 11")
}

View File

@ -3,6 +3,7 @@ package context
import (
"net/http"
_ "net/http/pprof"
"os"
"github.com/0xAX/notificator"
"github.com/erroneousboat/termui"
@ -32,7 +33,7 @@ type AppContext struct {
// CreateAppContext creates an application context which can be passed
// and referenced througout the application
func CreateAppContext(flgConfig string, flgDebug bool) (*AppContext, error) {
func CreateAppContext(flgConfig string, flgToken string, flgDebug bool) (*AppContext, error) {
if flgDebug {
go func() {
http.ListenAndServe(":6060", nil)
@ -48,6 +49,16 @@ func CreateAppContext(flgConfig string, flgDebug bool) (*AppContext, error) {
return nil, err
}
// When slack token isn't set in the config file, we'll check
// the command-line flag or the environment variable
if config.SlackToken == "" {
if flgToken != "" {
config.SlackToken = flgToken
} else {
config.SlackToken = os.Getenv("SLACK_TOKEN")
}
}
// Create Service
svc, err := service.NewSlackService(config)
if err != nil {

10
main.go
View File

@ -36,6 +36,7 @@ GLOBAL OPTIONS:
var (
flgConfig string
flgToken string
flgDebug bool
flgUsage bool
)
@ -55,6 +56,13 @@ func init() {
"location of config file",
)
flag.StringVar(
&flgToken,
"token",
"",
"the slack token",
)
flag.BoolVar(
&flgDebug,
"debug",
@ -87,7 +95,7 @@ func main() {
termui.DefaultEvtStream = customEvtStream
// Create context
ctx, err := context.CreateAppContext(flgConfig, flgDebug)
ctx, err := context.CreateAppContext(flgConfig, flgToken, flgDebug)
if err != nil {
termbox.Close()
log.Println(err)

View File

@ -47,7 +47,7 @@ func NewSlackService(config *config.Config) (*SlackService, error) {
// arrives
authTest, err := svc.Client.AuthTest()
if err != nil {
return nil, errors.New("not able to authorize client, check your connection and or slack-token")
return nil, errors.New("not able to authorize client, check your connection and if your slack-token is set correctly")
}
svc.CurrentUserID = authTest.UserID