From a98409901d9718f63ff76fdbd50336bc770d6d52 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 7 Apr 2018 11:17:25 +0200 Subject: [PATCH] Set slack token as flag or env variable Fixes #133 --- config/config.go | 4 ---- context/context.go | 13 ++++++++++++- main.go | 10 +++++++++- service/slack.go | 2 +- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/config/config.go b/config/config.go index a4d190a..abde6e4 100644 --- a/config/config.go +++ b/config/config.go @@ -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") } diff --git a/context/context.go b/context/context.go index 3d75b64..ffad4f9 100644 --- a/context/context.go +++ b/context/context.go @@ -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 { diff --git a/main.go b/main.go index 61f5588..23515b1 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/service/slack.go b/service/slack.go index 8ef1d53..7ef28c2 100644 --- a/service/slack.go +++ b/service/slack.go @@ -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