refactor: move token retrieval login to run.go

This commit is contained in:
ayn2op 2024-12-20 18:15:47 -05:00
parent 5fb4f6b1b1
commit e386077b96
2 changed files with 13 additions and 12 deletions

View file

@ -1,8 +1,11 @@
package cmd
import (
"log/slog"
"github.com/ayn2op/discordo/internal/config"
"github.com/ayn2op/discordo/internal/logger"
"github.com/zalando/go-keyring"
)
var (
@ -15,6 +18,16 @@ func Run(token string) error {
return err
}
// If no token was provided, look it up in the keyring.
if token == "" {
tok, err := keyring.Get(config.Name, "token")
if err != nil {
slog.Info("failed to get token from keyring", "err", err)
} else {
token = tok
}
}
cfg, err := config.Load()
if err != nil {
return err

12
main.go
View file

@ -5,24 +5,12 @@ import (
"log/slog"
"github.com/ayn2op/discordo/cmd"
"github.com/ayn2op/discordo/internal/config"
"github.com/zalando/go-keyring"
)
func main() {
token := flag.String("token", "", "authentication token")
flag.Parse()
// If no token was provided, look it up in the keyring
if *token == "" {
t, err := keyring.Get(config.Name, "token")
if err != nil {
slog.Info("failed to get token from keyring", "err", err)
} else {
*token = t
}
}
if err := cmd.Run(*token); err != nil {
slog.Error("failed to run", "err", err)
}