refactor: get rid of cobra

This commit is contained in:
ayn2op 2025-05-24 19:25:28 -04:00
parent af2b0995ba
commit 1dd5a347a4
4 changed files with 60 additions and 84 deletions

View file

@ -1,6 +1,7 @@
package cmd
import (
"flag"
"log/slog"
"github.com/ayn2op/discordo/internal/config"
@ -9,7 +10,6 @@ import (
"github.com/diamondburned/arikawa/v3/utils/ws"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/spf13/cobra"
"github.com/zalando/go-keyring"
)
@ -18,78 +18,65 @@ var (
app *App
)
var (
rootCmd = &cobra.Command{
PreRunE: func(cmd *cobra.Command, args []string) error {
flags := cmd.Flags()
var level slog.Level
switch s, _ := flags.GetString("log-level"); s {
case "debug":
ws.EnableRawEvents = true
level = slog.LevelDebug
case "info":
level = slog.LevelInfo
case "warn":
level = slog.LevelWarn
case "error":
level = slog.LevelError
}
var format logger.Format
switch s, _ := flags.GetString("log-format"); s {
case "text":
format = logger.FormatText
case "json":
format = logger.FormatJson
}
return logger.Load(format, level)
},
RunE: func(cmd *cobra.Command, args []string) error {
token, _ := cmd.Flags().GetString("token")
if token == "" {
var err error
token, err = keyring.Get(consts.Name, "token")
if err != nil {
slog.Info("failed to retrieve token from keyring", "err", err)
}
}
cfg, err := config.Load()
if err != nil {
return err
}
tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(cfg.Theme.BackgroundColor)
tview.Borders.Horizontal = cfg.Theme.Border.Preset.Horizontal
tview.Borders.Vertical = cfg.Theme.Border.Preset.Vertical
tview.Borders.TopLeft = cfg.Theme.Border.Preset.TopLeft
tview.Borders.TopRight = cfg.Theme.Border.Preset.TopRight
tview.Borders.BottomLeft = cfg.Theme.Border.Preset.BottomLeft
tview.Borders.BottomRight = cfg.Theme.Border.Preset.BottomRight
tview.Borders.HorizontalFocus = tview.Borders.Horizontal
tview.Borders.VerticalFocus = tview.Borders.Vertical
tview.Borders.TopLeftFocus = tview.Borders.TopLeft
tview.Borders.TopRightFocus = tview.Borders.TopRight
tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
tview.Borders.BottomRightFocus = tview.Borders.BottomRight
app = newApp(cfg)
return app.run(token)
},
func Run() error {
logLevel := flag.String("log-level", "info", "log level")
var level slog.Level
switch *logLevel {
case "debug":
ws.EnableRawEvents = true
level = slog.LevelDebug
case "info":
level = slog.LevelInfo
case "warn":
level = slog.LevelWarn
case "error":
level = slog.LevelError
}
Execute = rootCmd.Execute
)
logFormat := flag.String("log-format", "text", "log format")
var format logger.Format
switch *logFormat {
case "text":
format = logger.FormatText
case "json":
format = logger.FormatJson
}
func init() {
flags := rootCmd.Flags()
flags.StringP("token", "t", "", "the authentication token")
if err := logger.Load(format, level); err != nil {
return err
}
flags.String("log-level", "info", "log level")
flags.String("log-format", "text", "log format")
token := flag.String("token", "", "authentication token")
tok := *token
if tok == "" {
var err error
tok, err = keyring.Get(consts.Name, "token")
if err != nil {
slog.Info("failed to retrieve token from keyring", "err", err)
}
}
cfg, err := config.Load()
if err != nil {
return err
}
tview.Styles.PrimitiveBackgroundColor = tcell.GetColor(cfg.Theme.BackgroundColor)
tview.Borders.Horizontal = cfg.Theme.Border.Preset.Horizontal
tview.Borders.Vertical = cfg.Theme.Border.Preset.Vertical
tview.Borders.TopLeft = cfg.Theme.Border.Preset.TopLeft
tview.Borders.TopRight = cfg.Theme.Border.Preset.TopRight
tview.Borders.BottomLeft = cfg.Theme.Border.Preset.BottomLeft
tview.Borders.BottomRight = cfg.Theme.Border.Preset.BottomRight
tview.Borders.HorizontalFocus = tview.Borders.Horizontal
tview.Borders.VerticalFocus = tview.Borders.Vertical
tview.Borders.TopLeftFocus = tview.Borders.TopLeft
tview.Borders.TopRightFocus = tview.Borders.TopRight
tview.Borders.BottomLeftFocus = tview.Borders.BottomLeft
tview.Borders.BottomRightFocus = tview.Borders.BottomRight
app = newApp(cfg)
return app.run(tok)
}

3
go.mod
View file

@ -12,7 +12,6 @@ require (
github.com/gen2brain/beeep v0.0.0-20240516210008-9c006672e7f4
github.com/rivo/tview v0.0.0-20250501113434-0c592cd31026
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966
github.com/spf13/cobra v1.9.1
github.com/yuin/goldmark v1.7.11
github.com/zalando/go-keyring v0.2.6
)
@ -25,13 +24,11 @@ require (
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/gorilla/schema v1.4.1 // indirect
github.com/gorilla/websocket v1.5.3 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/spf13/pflag v1.0.6 // indirect
github.com/tadvi/systray v0.0.0-20190226123456-11a2b8fa57af // indirect
github.com/twmb/murmur3 v1.1.8 // indirect
go4.org v0.0.0-20230225012048-214862532bf5 // indirect

8
go.sum
View file

@ -28,7 +28,6 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
github.com/danieljoos/wincred v1.2.2 h1:774zMFJrqaeYCK2W57BgAem/MLi6mtSE47MB6BOJ0i0=
github.com/danieljoos/wincred v1.2.2/go.mod h1:w7w4Utbrz8lqeMbDAK0lkNJUv5sAOkFi7nd/ogr0Uh8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@ -89,8 +88,6 @@ github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
@ -117,14 +114,9 @@ github.com/rivo/uniseg v0.4.3/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUc
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966 h1:JIAuq3EEf9cgbU6AtGPK4CTG3Zf6CKMNqf0MHTggAUA=
github.com/skratchdot/open-golang v0.0.0-20200116055534-eef842397966/go.mod h1:sUM3LWHvSMaG192sy56D9F7CNvL7jUJVXoqM1QKLnog=
github.com/spf13/cobra v1.9.1 h1:CXSaggrXdbHK9CF+8ywj8Amf7PBRmPCOJugH954Nnlo=
github.com/spf13/cobra v1.9.1/go.mod h1:nDyEzZ8ogv936Cinf6g1RU9MRY64Ir93oCnqb9wxYW0=
github.com/spf13/pflag v1.0.6 h1:jFzHGLGAlb3ruxLB8MhbI6A8+AQX/2eW4qeyNZXNp2o=
github.com/spf13/pflag v1.0.6/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=

View file

@ -7,7 +7,7 @@ import (
)
func main() {
if err := cmd.Execute(); err != nil {
slog.Error("failed to execute command", "err", err)
if err := cmd.Run(); err != nil {
slog.Error("failed to run command", "err", err)
}
}