From 01c75f05ad7058d1bef0827ef626f70c2f0a275d Mon Sep 17 00:00:00 2001 From: ayntgl Date: Sun, 23 Oct 2022 21:19:24 +0530 Subject: [PATCH] Switch to kong --- go.mod | 3 +-- go.sum | 4 ++++ main.go | 42 +++++++++++++++++------------------------- 3 files changed, 22 insertions(+), 27 deletions(-) diff --git a/go.mod b/go.mod index e7d90c9..b1d82fa 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ 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 @@ -15,7 +16,6 @@ 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 @@ -24,7 +24,6 @@ require ( github.com/mattn/go-runewidth v0.0.14 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/rivo/uniseg v0.4.2 // indirect - github.com/stretchr/testify v1.7.2 // indirect golang.org/x/sys v0.1.0 // indirect golang.org/x/term v0.0.0-20221017184919-83659145692c // indirect golang.org/x/text v0.4.0 // indirect diff --git a/go.sum b/go.sum index 36b8cfd..897f261 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,7 @@ +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= diff --git a/main.go b/main.go index b29f530..3d976d1 100644 --- a/main.go +++ b/main.go @@ -1,33 +1,30 @@ package main import ( - "flag" "log" "os" + "github.com/alecthomas/kong" "github.com/ayntgl/discordo/config" "github.com/ayntgl/discordo/ui" "github.com/zalando/go-keyring" ) -var ( - flagToken string - flagConfig string - flagLog string -) - -func init() { - flag.StringVar(&flagToken, "token", "", "The authentication token.") - flag.StringVar(&flagConfig, "config", config.DefaultConfigPath(), "The path to the configuration file.") - flag.StringVar(&flagLog, "log", config.DefaultLogPath(), "The path to the log file.") +var cli struct { + Token string `name:"token" short:"t" help:"The authentication token."` + ConfigPath string `name:"config" short:"c" help:"The path to the configuration file" type:"path" default:"${configPath}"` + LogPath string `name:"log" short:"l" help:"The path to the log file" type:"path" default:"${logPath}"` } func main() { - flag.Parse() + kong.Parse(&cli, kong.Vars{ + "configPath": config.DefaultConfigPath(), + "logPath": config.DefaultLogPath(), + }) - if flagLog != "" { + if cli.LogPath != "" { // Set the standard logger output to the provided log file. - f, err := os.OpenFile(flagLog, os.O_CREATE|os.O_WRONLY, 0666) + f, err := os.OpenFile(cli.LogPath, os.O_CREATE|os.O_WRONLY, 0666) if err != nil { log.Fatal(err) } @@ -37,25 +34,20 @@ func main() { } cfg := config.New() - if err := cfg.Load(flagConfig); err != nil { + if err := cfg.Load(cli.ConfigPath); err != nil { log.Fatal(err) } - var ( - token string - err error - ) - - if flagToken != "" { - token = flagToken - go keyring.Set(config.Name, "token", token) + if cli.Token != "" { + go keyring.Set(config.Name, "token", cli.Token) } else { - token, err = keyring.Get(config.Name, "token") + var err error + cli.Token, err = keyring.Get(config.Name, "token") if err != nil { log.Println(err) } } app := ui.NewApplication(cfg) - app.Run(token) + app.Run(cli.Token) }