Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for multiple workspaces #169

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"os"
"sort"

"github.com/erroneousboat/termui"
)
Expand All @@ -17,6 +18,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"`
Expand All @@ -28,7 +30,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)
Expand All @@ -40,6 +42,43 @@ func NewConfig(filepath 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 = workspaceConfig.SlackToken

if workspaceConfig.Notify != "" {
cfg.Notify = workspaceConfig.Notify
}

// TODO: There's no way to distinguish between falsy and unset.
// cfg.Emoji = workspaceConfig.Emoji

if workspaceConfig.SidebarWidth != 0 {
cfg.SidebarWidth = workspaceConfig.SidebarWidth
}
if workspaceConfig.MainWidth != 0 {
cfg.MainWidth = workspaceConfig.MainWidth
}
if workspaceConfig.KeyMap != nil {
cfg.KeyMap = workspaceConfig.KeyMap
}
if workspaceConfig.Theme != *new(Theme) {
cfg.Theme = workspaceConfig.Theme
}

if cfg.SidebarWidth < 1 || cfg.SidebarWidth > 11 {
return &cfg, errors.New("please specify the 'sidebar_width' between 1 and 11")
}
Expand Down
12 changes: 4 additions & 8 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -47,19 +47,15 @@ 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
}

// 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
Expand Down
27 changes: 17 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -51,21 +51,28 @@ func init() {
log.Fatal(err)
}

// Parse flags
// Parse flags.

// Path to the config file to use.
// Defaults to ~/.slack-term
flag.StringVar(
&flgConfig,
"config",
path.Join(usr.HomeDir, ".slack-term"),
"location of config file",
)

// 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(
&flgToken,
"token",
&flgWorkspace,
"workspace",
"",
"the slack token",
"the slack workspace to use",
)

// Toggle debug mode.
flag.BoolVar(
&flgDebug,
"debug",
Expand Down Expand Up @@ -100,7 +107,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()
Expand Down