refactor: remove constants package

This commit is contained in:
ayn2op 2024-10-13 11:34:19 -04:00
parent da30c2e8fd
commit 37a8be5885
8 changed files with 25 additions and 40 deletions

View file

@ -4,7 +4,6 @@ import (
"log/slog"
"github.com/ayn2op/discordo/internal/config"
"github.com/ayn2op/discordo/internal/constants"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/zalando/go-keyring"
@ -110,7 +109,7 @@ func (l *Layout) onFlexInputCapture(event *tcell.EventKey) *tcell.EventKey {
case l.cfg.Keys.Logout:
l.app.Stop()
if err := keyring.Delete(constants.Name, "token"); err != nil {
if err := keyring.Delete(config.Name, "token"); err != nil {
slog.Error("failed to delete token from keyring", "err", err)
return nil
}

View file

@ -4,7 +4,6 @@ import (
"errors"
"github.com/ayn2op/discordo/internal/config"
"github.com/ayn2op/discordo/internal/constants"
"github.com/diamondburned/arikawa/v3/api"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
@ -85,7 +84,7 @@ func (lf *loginForm) login() {
rememberMe := lf.GetFormItem(3).(*tview.Checkbox).IsChecked()
if rememberMe {
go func() {
if err := keyring.Set(constants.Name, "token", lr.Token); err != nil {
if err := keyring.Set(config.Name, "token", lr.Token); err != nil {
lf.done("", err)
}
}()

View file

@ -8,7 +8,6 @@ import (
"github.com/atotto/clipboard"
"github.com/ayn2op/discordo/internal/config"
"github.com/ayn2op/discordo/internal/constants"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/utils/json/option"
@ -16,6 +15,8 @@ import (
"github.com/rivo/tview"
)
const tmpFilePattern = config.Name + "_*.md"
type MessageInput struct {
*tview.TextArea
cfg *config.Config
@ -121,7 +122,7 @@ func (mi *MessageInput) editor() {
e = os.Getenv("EDITOR")
}
f, err := os.CreateTemp("", constants.TmpFilePattern)
f, err := os.CreateTemp("", tmpFilePattern)
if err != nil {
slog.Error("failed to create temporary file", "err", err)
return

View file

@ -7,7 +7,6 @@ import (
"slices"
"github.com/ayn2op/discordo/internal/config"
"github.com/ayn2op/discordo/internal/constants"
"github.com/diamondburned/arikawa/v3/api"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/diamondburned/arikawa/v3/gateway"
@ -17,12 +16,16 @@ import (
"github.com/rivo/tview"
)
const userAgent = config.Name + "/0.1 (https://github.com/diamondburned/arikawa, v3)"
func init() {
api.UserAgent = constants.UserAgent
api.UserAgent = userAgent
gateway.DefaultIdentity = gateway.IdentifyProperties{
OS: runtime.GOOS,
Browser: constants.Name,
Device: "",
OS: runtime.GOOS,
Device: "",
Browser: config.Name,
BrowserUserAgent: userAgent,
}
}

View file

@ -6,9 +6,10 @@ import (
"time"
"github.com/BurntSushi/toml"
"github.com/ayn2op/discordo/internal/constants"
)
const Name = "discordo"
type Config struct {
Mouse bool `toml:"mouse"`
HideBlockedUsers bool `toml:"hide_blocked_users"`
@ -44,7 +45,12 @@ func defaultConfig() *Config {
// Reads the configuration file and parses it.
func Load() (*Config, error) {
path := filepath.Join(constants.ConfigDirPath, "config.toml")
path, err := os.UserConfigDir()
if err != nil {
path = "."
}
path = filepath.Join(path, Name, "config.toml")
f, err := os.Open(path)
cfg := defaultConfig()

View file

@ -1,23 +0,0 @@
package constants
import (
"os"
"path/filepath"
)
const Name = "discordo"
const UserAgent = Name + "/0.1 (https://github.com/diamondburned/arikawa, v3)"
const TmpFilePattern = Name + "_*.md"
var ConfigDirPath string
func init() {
path, err := os.UserConfigDir()
if err != nil {
path = "."
}
ConfigDirPath = filepath.Join(path, Name)
}

View file

@ -5,7 +5,7 @@ import (
"os"
"path/filepath"
"github.com/ayn2op/discordo/internal/constants"
"github.com/ayn2op/discordo/internal/config"
)
// Recursively creates the log directory if it does not exist already and returns the path to the log file.
@ -15,7 +15,7 @@ func initialize() (string, error) {
return "", err
}
path = filepath.Join(path, constants.Name)
path = filepath.Join(path, config.Name)
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return "", err
}

View file

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