瀏覽代碼

Switch to kong

ayntgl 3 年之前
父節點
當前提交
01c75f05ad
共有 3 個文件被更改,包括 22 次插入27 次删除
  1. 1 2
      go.mod
  2. 4 0
      go.sum
  3. 17 25
      main.go

+ 1 - 2
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

+ 4 - 0
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=

+ 17 - 25
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)
 }