erroneousboat-slack-term/main.go
Lyle Hanson ce1d8ce03f Respect the XDG Base Directory specification for configs
Rather than assuming a configuration file at `~/.slack-term`, respect
the user's choice per the XDG Base Directory specification
(https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html)
to find the configuration.

Uses a cross-platform XDG library to abstract the details of the
specification and encompass any future changes
(https://github.com/OpenPeeDeeP/xdg, https://godoc.org/github.com/OpenPeeDeeP/xdg).

After merging, the [wiki home page](https://github.com/erroneousboat/slack-term/wiki)
should be updated to reflect the new configuration location.

This closes #170 and closes #203.
2020-01-08 11:39:16 +01:00

125 lines
2.0 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"os"
"os/user"
"path"
"github.com/OpenPeeDeeP/xdg"
"github.com/erroneousboat/termui"
termbox "github.com/nsf/termbox-go"
"github.com/erroneousboat/slack-term/context"
"github.com/erroneousboat/slack-term/handlers"
)
const (
VERSION = "master"
USAGE = `NAME:
slack-term - slack client for your terminal
USAGE:
slack-term -config [path-to-config]
VERSION:
%s
WEBSITE:
https://github.com/erroneousboat/slack-term
GLOBAL OPTIONS:
-config [path-to-config-file]
-token [slack-token]
-debug
-help, -h
`
)
var (
flgConfig string
flgToken string
flgDebug bool
flgUsage bool
)
func init() {
// Get home dir for config file default
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
// Find the default config file
xdg := xdg.New("slack-term", "")
configFile := xdg.QueryConfig("config")
if configFile == "" {
// Fall back to $HOME/.slack_term for legacy compatibility
configFile = path.Join(usr.HomeDir, ".slack-term")
}
// Parse flags
flag.StringVar(
&flgConfig,
"config",
configFile,
"location of config file",
)
flag.StringVar(
&flgToken,
"token",
"",
"the slack token",
)
flag.BoolVar(
&flgDebug,
"debug",
false,
"turn on debugging",
)
flag.Usage = func() {
fmt.Printf(USAGE, VERSION)
}
flag.Parse()
}
func main() {
// Start terminal user interface
err := termui.Init()
if err != nil {
log.Fatal(err)
}
defer termui.Close()
// Create custom event stream for termui because
// termui's one has data race conditions with its
// event handling. We're circumventing it here until
// it has been fixed.
customEvtStream := &termui.EvtStream{
Handlers: make(map[string]func(termui.Event)),
}
termui.DefaultEvtStream = customEvtStream
// Create context
usage := fmt.Sprintf(USAGE, VERSION)
ctx, err := context.CreateAppContext(
flgConfig, flgToken, flgDebug, VERSION, usage,
)
if err != nil {
termbox.Close()
log.Println(err)
os.Exit(0)
}
// Initialize handlers
handlers.Initialize(ctx)
termui.Loop()
}