From 3a57ac35f43b3797abb6cc36129762c60f6a100e Mon Sep 17 00:00:00 2001 From: Jordan Matelsky Date: Tue, 13 Nov 2018 15:35:11 -0500 Subject: [PATCH 1/4] Add workspace config option --- config/config.go | 1 + main.go | 20 +++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/config/config.go b/config/config.go index 99efffd..6221285 100644 --- a/config/config.go +++ b/config/config.go @@ -17,6 +17,7 @@ const ( // Config is the definition of a Config struct type Config struct { SlackToken string `json:"slack_token"` + Workspaces map[string]Config `json:"workspaces"` Notify string `json:"notify"` Emoji bool `json:"emoji"` SidebarWidth int `json:"sidebar_width"` diff --git a/main.go b/main.go index 3f92141..683b941 100644 --- a/main.go +++ b/main.go @@ -31,17 +31,17 @@ WEBSITE: GLOBAL OPTIONS: -config [path-to-config-file] - -token [slack-token] + -workspace [slack-workspace] -debug -help, -h ` ) var ( - flgConfig string - flgToken string - flgDebug bool - flgUsage bool + flgConfig string + flgWorkspace string + flgDebug bool + flgUsage bool ) func init() { @@ -59,13 +59,15 @@ func init() { "location of config file", ) + // The name of the workspace to use. flag.StringVar( - &flgToken, - "token", + &flgWorkspace, + "workspace", "", - "the slack token", + "the slack workspace to use", ) + // Toggle debug mode. flag.BoolVar( &flgDebug, "debug", @@ -100,7 +102,7 @@ func main() { // Create context usage := fmt.Sprintf(USAGE, VERSION) ctx, err := context.CreateAppContext( - flgConfig, flgToken, flgDebug, VERSION, usage, + flgConfig, flgWorkspace, flgDebug, VERSION, usage, ) if err != nil { termbox.Close() From 198caa070831753fa9702495289c622b96702800 Mon Sep 17 00:00:00 2001 From: Jordan Matelsky Date: Tue, 13 Nov 2018 15:37:07 -0500 Subject: [PATCH 2/4] Propagate workspace flag to config parser --- config/config.go | 2 +- context/context.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 6221285..d9ff5c3 100644 --- a/config/config.go +++ b/config/config.go @@ -29,7 +29,7 @@ type Config struct { type keyMapping map[string]string // NewConfig loads the config file and returns a Config struct -func NewConfig(filepath string) (*Config, error) { +func NewConfig(filepath string, workspaceName string) (*Config, error) { cfg := getDefaultConfig() file, err := os.Open(filepath) diff --git a/context/context.go b/context/context.go index 91447a6..f143a42 100644 --- a/context/context.go +++ b/context/context.go @@ -36,7 +36,7 @@ type AppContext struct { // CreateAppContext creates an application context which can be passed // and referenced througout the application -func CreateAppContext(flgConfig string, flgToken string, flgDebug bool, version string, usage string) (*AppContext, error) { +func CreateAppContext(flgConfig string, flgWorkspace string, flgDebug bool, version string, usage string) (*AppContext, error) { if flgDebug { go func() { http.ListenAndServe(":6060", nil) @@ -47,7 +47,7 @@ func CreateAppContext(flgConfig string, flgToken string, flgDebug bool, version views.Loading() // Load config - config, err := config.NewConfig(flgConfig) + config, err := config.NewConfig(flgConfig, flgWorkspace) if err != nil { return nil, err } From 3295e939b09886f215debb44fa410da7cfd91915 Mon Sep 17 00:00:00 2001 From: Jordan Matelsky Date: Tue, 13 Nov 2018 15:37:28 -0500 Subject: [PATCH 3/4] Overwrite global values with workspace-specific values. Except emoji... --- config/config.go | 24 ++++++++++++++++++++++++ context/context.go | 8 ++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/config/config.go b/config/config.go index d9ff5c3..2df2b8f 100644 --- a/config/config.go +++ b/config/config.go @@ -41,6 +41,30 @@ func NewConfig(filepath string, workspaceName string) (*Config, error) { return &cfg, fmt.Errorf("the slack-term config file isn't valid json: %v", err) } + // Overwrite all options if they're specified in the + // workspace-specific config: + cfg.SlackToken = cfg.Workspaces[workspaceName].SlackToken + + if cfg.Workspaces[workspaceName].Notify != "" { + cfg.Notify = cfg.Workspaces[workspaceName].Notify + } + + // TODO: There's no way to distinguish between falsy and unset. + // cfg.Emoji = cfg.Workspaces[workspaceName].Emoji + + if cfg.Workspaces[workspaceName].SidebarWidth != 0 { + cfg.SidebarWidth = cfg.Workspaces[workspaceName].SidebarWidth + } + if cfg.Workspaces[workspaceName].MainWidth != 0 { + cfg.MainWidth = cfg.Workspaces[workspaceName].MainWidth + } + if cfg.Workspaces[workspaceName].KeyMap != nil { + cfg.KeyMap = cfg.Workspaces[workspaceName].KeyMap + } + if cfg.Workspaces[workspaceName].Theme != *new(Theme) { + cfg.Theme = cfg.Workspaces[workspaceName].Theme + } + 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 f143a42..be1b006 100644 --- a/context/context.go +++ b/context/context.go @@ -52,14 +52,10 @@ func CreateAppContext(flgConfig string, flgWorkspace string, flgDebug bool, vers return nil, err } - // When slack token isn't set in the config file, we'll check + // When slack token still 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") - } + config.SlackToken = os.Getenv("SLACK_TOKEN") } // Create desktop notifier From c255b592f754bd13f665d13eaec189926ce9049f Mon Sep 17 00:00:00 2001 From: Jordan Matelsky Date: Tue, 13 Nov 2018 15:46:51 -0500 Subject: [PATCH 4/4] Default select the first (alphabetical) workspace --- config/config.go | 38 ++++++++++++++++++++++++++------------ main.go | 7 ++++++- 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/config/config.go b/config/config.go index 2df2b8f..cd7cdb4 100644 --- a/config/config.go +++ b/config/config.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" "os" + "sort" "github.com/erroneousboat/termui" ) @@ -41,28 +42,41 @@ func NewConfig(filepath string, workspaceName string) (*Config, error) { return &cfg, fmt.Errorf("the slack-term config file isn't valid json: %v", err) } + // If no workspace is specified, select the first (ABC-order). + if workspaceName == "" { + keys := make([]string, len(cfg.Workspaces)) + i := 0 + for k := range cfg.Workspaces { + keys[i] = k + i++ + } + sort.Strings(keys) + workspaceName = keys[0] + } + workspaceConfig := cfg.Workspaces[workspaceName] + // Overwrite all options if they're specified in the // workspace-specific config: - cfg.SlackToken = cfg.Workspaces[workspaceName].SlackToken + cfg.SlackToken = workspaceConfig.SlackToken - if cfg.Workspaces[workspaceName].Notify != "" { - cfg.Notify = cfg.Workspaces[workspaceName].Notify + if workspaceConfig.Notify != "" { + cfg.Notify = workspaceConfig.Notify } // TODO: There's no way to distinguish between falsy and unset. - // cfg.Emoji = cfg.Workspaces[workspaceName].Emoji + // cfg.Emoji = workspaceConfig.Emoji - if cfg.Workspaces[workspaceName].SidebarWidth != 0 { - cfg.SidebarWidth = cfg.Workspaces[workspaceName].SidebarWidth + if workspaceConfig.SidebarWidth != 0 { + cfg.SidebarWidth = workspaceConfig.SidebarWidth } - if cfg.Workspaces[workspaceName].MainWidth != 0 { - cfg.MainWidth = cfg.Workspaces[workspaceName].MainWidth + if workspaceConfig.MainWidth != 0 { + cfg.MainWidth = workspaceConfig.MainWidth } - if cfg.Workspaces[workspaceName].KeyMap != nil { - cfg.KeyMap = cfg.Workspaces[workspaceName].KeyMap + if workspaceConfig.KeyMap != nil { + cfg.KeyMap = workspaceConfig.KeyMap } - if cfg.Workspaces[workspaceName].Theme != *new(Theme) { - cfg.Theme = cfg.Workspaces[workspaceName].Theme + if workspaceConfig.Theme != *new(Theme) { + cfg.Theme = workspaceConfig.Theme } if cfg.SidebarWidth < 1 || cfg.SidebarWidth > 11 { diff --git a/main.go b/main.go index 683b941..4b23d44 100644 --- a/main.go +++ b/main.go @@ -51,7 +51,10 @@ func init() { log.Fatal(err) } - // Parse flags + // Parse flags. + + // Path to the config file to use. + // Defaults to ~/.slack-term flag.StringVar( &flgConfig, "config", @@ -60,6 +63,8 @@ func init() { ) // The name of the workspace to use. + // If none is provided, will pick the alphabetical first (in other words, + // will pick the only workspace in a list of only one workspace). flag.StringVar( &flgWorkspace, "workspace",