From fcd52c361e4454f858d7edcb82949aa8a3f6648b Mon Sep 17 00:00:00 2001 From: ayntgl <90600421+ayntgl@users.noreply.github.com> Date: Tue, 30 Aug 2022 19:26:10 +0530 Subject: [PATCH] fix: load configuration file if token is not provided (#199) --- README.md | 8 +++---- go.mod | 5 +++-- go.sum | 6 ++--- main.go | 66 +++++++++++++++++++++++++++++-------------------------- 4 files changed, 44 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 89097a9..09f1808 100644 --- a/README.md +++ b/README.md @@ -62,17 +62,17 @@ sudo mv ./discordo /usr/local/bin ## Usage -1. Run the `discordo` executable with no arguments to login using email and password. If you are using token to login, set the `token` flag to the token that you want to login with. +1. Run the `discordo` executable with no arguments to log in using email and password. If you are using a token to log in, set the `token` flag to the token that you want to log in with. -- Note: If you are logging in with a bot account, prefix the token with `Bot ` (eg: `--token "Bot OTI2MDU5NTQxNDE2Nzc5ODA2.Yc2KKA.2iZ-5JxgxG-9Ub8GHzBSn-NJjNg"`). +- Note: If you are logging in with a bot account, prefix the token with `Bot ` (eg: `discordo --token "Bot OTI2MDU5NTQxNDE2Nzc5ODA2.Yc2KKA.2iZ-5JxgxG-9Ub8GHzBSn-NJjNg"`). Furthermore, it is strongly recommended to change the user agent for HTTP requests from the configuration file if you are using a bot account to log in since the default user agent is that of a browser. -2. If you are logging in with email and password, enter your email and password (first-time login) and click on the "Login" button to continue. +2. If you are logging in with your email and password, enter your email and password (first-time login) and click on the "Login" button to continue. - Note: Your email and password are not stored locally. Most of the Discord third-party clients store the token in a configuration file unencrypted. Discordo securely stores the token in the default OS-specific keyring. ### Configuration -A default configuration file is created on first start-up at `$HOME/.config/discordo/config.lua` on Unix, `$HOME/Library/Application Support/discordo/config.lua` on Darwin, and `%AppData%/discordo/config.lua` on Windows. You can configure the default configuration path using the `config` command-line flag. +A default configuration file is created on first start-up at `$HOME/.config/discordo/config.lua` on Unix, `$HOME/Library/Application Support/discordo/config.lua` on Darwin, and `%AppData%/discordo/config.lua` on Windows. You can configure the default configuration directory path using the `config` command-line flag (eg: `discordo --config $HOME/my-custom-dir). ## Disclaimer diff --git a/go.mod b/go.mod index 62ff86d..f0cced2 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module github.com/ayntgl/discordo go 1.19 require ( - github.com/alecthomas/kong v0.6.1 github.com/atotto/clipboard v0.1.4 github.com/diamondburned/arikawa/v3 v3.1.0 github.com/gdamore/tcell/v2 v2.5.3 @@ -17,6 +16,7 @@ require ( require ( github.com/alessio/shellescape v1.4.1 // indirect github.com/danieljoos/wincred v1.1.2 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/gdamore/encoding v1.0.0 // indirect github.com/godbus/dbus/v5 v5.1.0 // indirect github.com/gorilla/schema v1.2.0 // indirect @@ -25,7 +25,8 @@ require ( github.com/mattn/go-runewidth v0.0.13 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/rivo/uniseg v0.3.4 // indirect - golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect + github.com/stretchr/testify v1.7.2 // indirect + golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 // indirect golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect golang.org/x/text v0.3.7 // indirect golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect diff --git a/go.sum b/go.sum index 0e465d2..c9a5002 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,3 @@ -github.com/alecthomas/kong v0.6.1 h1:1kNhcFepkR+HmasQpbiKDLylIL8yh5B5y1zPp5bJimA= -github.com/alecthomas/kong v0.6.1/go.mod h1:JfHWDzLmbh/puW6I3V7uWenoh56YNVONW+w8eKeUr9I= -github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142 h1:8Uy0oSf5co/NZXje7U1z8Mpep++QJOldL2hs/sBQf48= -github.com/alecthomas/repr v0.0.0-20210801044451-80ca428c5142/go.mod h1:2kn6fqh/zIyPLmm3ugklbEi5hg5wS435eygvNfaDQL8= github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0= github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30= github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= @@ -65,6 +61,8 @@ golang.org/x/sys v0.0.0-20211001092434-39dca1131b70/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 h1:UiNENfZ8gDvpiWw7IpOMQ27spWmThO1RwwdQVbJahJM= golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220829200755-d48e67d00261 h1:v6hYoSR9T5oet+pMXwUWkbiVqx/63mlHjefrHmxwfeY= +golang.org/x/sys v0.0.0-20220829200755-d48e67d00261/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 h1:Q5284mrmYTpACcm+eAKjKJH48BBwSyfJqmmGDTtT8Vc= diff --git a/main.go b/main.go index 991374d..672938e 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,11 @@ package main import ( + "flag" "log" "os" "path/filepath" - "github.com/alecthomas/kong" "github.com/ayntgl/discordo/ui" "github.com/gdamore/tcell/v2" "github.com/rivo/tview" @@ -14,40 +14,39 @@ import ( ) const ( - name = "discordo" - usage = "A lightweight, secure, and feature-rich Discord terminal client" + name = "discordo" ) -var cli struct { - Token string `help:"The authentication token."` - Config string `help:"The path to the configuration directory." type:"path"` +var ( + token string + configPath string +) + +func init() { + flag.StringVar(&token, "token", "", "The client authentication token.") + // If the token is provided via a command-line flag, store it in the default keyring. + if token != "" { + go keyring.Set(name, "token", token) + } + + if token == "" { + token, _ = keyring.Get(name, "token") + } + + configDirPath, err := os.UserConfigDir() + if err != nil { + log.Fatal(err) + } + + flag.StringVar(&configPath, "config", filepath.Join(configDirPath, name), "The path to the configuration directory.") } func main() { - kong.Parse(&cli, kong.Name(name), kong.Description(usage), kong.UsageOnError()) + flag.Parse() - // If the authentication token is provided via a flag, store it in the default keyring. - if cli.Token != "" { - go keyring.Set(name, "token", cli.Token) - } - - // Defaults - if cli.Config == "" { - path, err := os.UserConfigDir() - if err != nil { - log.Fatal(err) - } - - cli.Config = filepath.Join(path, name) - } - - if cli.Token == "" { - cli.Token, _ = keyring.Get(name, "token") - } - - c := ui.NewCore(cli.Config) - if cli.Token != "" { - err := c.Run(cli.Token) + c := ui.NewCore(configPath) + if token != "" { + err := c.Run(token) if err != nil { log.Fatal(err) } @@ -127,9 +126,14 @@ func main() { tview.Borders.Horizontal = 0 tview.Borders.Vertical = 0 + err := c.Config.Load() + if err != nil { + return + } + themeTable, ok := c.Config.State.GetGlobal("theme").(*lua.LTable) if !ok { - return + themeTable = c.Config.State.NewTable() } background := themeTable.RawGetString("background") @@ -140,7 +144,7 @@ func main() { tview.Styles.BorderColor = tcell.GetColor(lua.LVAsString(border)) tview.Styles.TitleColor = tcell.GetColor(lua.LVAsString(title)) - err := c.Application.Run() + err = c.Application.Run() if err != nil { log.Fatal(err) }