Ver Fonte

refactor: move config.go to util pkg

ayntgl há 4 anos atrás
pai
commit
9eede1a547
2 ficheiros alterados com 35 adições e 34 exclusões
  1. 3 2
      main.go
  2. 32 32
      util/config.go

+ 3 - 2
main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	"github.com/ayntgl/discordgo"
+	"github.com/ayntgl/discordo/util"
 	"github.com/gdamore/tcell/v2"
 	"github.com/rivo/tview"
 	"github.com/zalando/go-keyring"
@@ -17,12 +18,12 @@ var (
 	messageInputField *tview.InputField
 	mainFlex          *tview.Flex
 
-	conf    *config
+	conf    *util.Config
 	session *discordgo.Session
 )
 
 func main() {
-	conf = loadConfig()
+	conf = util.LoadConfig()
 
 	tview.Borders = conf.Borders
 

+ 32 - 32
config.go → util/config.go

@@ -1,4 +1,4 @@
-package main
+package util
 
 import (
 	"os"
@@ -58,7 +58,7 @@ type borders struct {
 	BottomRightFocus rune
 }
 
-type config struct {
+type Config struct {
 	Token            string `toml:"token"`
 	UserAgent        string `toml:"user_agent"`
 	Mouse            bool   `toml:"mouse"`
@@ -70,15 +70,14 @@ type config struct {
 	Borders     borders     `toml:"borders"`
 }
 
-func loadConfig() *config {
+func LoadConfig() *Config {
 	configPath, err := os.UserConfigDir()
 	if err != nil {
 		panic(err)
 	}
 
 	configPath += "/discordo.toml"
-
-	var c config
+	var c Config
 	if _, err = os.Stat(configPath); os.IsNotExist(err) {
 		f, err := os.Create(configPath)
 		if err != nil {
@@ -86,34 +85,35 @@ func loadConfig() *config {
 		}
 		defer f.Close()
 
-		c.Mouse = true
-		c.Notifications = true
-		c.UserAgent = userAgent
-		c.GetMessagesLimit = 50
-		c.Theme = theme{
-			Background: "black",
-
-			Border:   "white",
-			Title:    "white",
-			Graphics: "white",
-			Text:     "white",
-		}
-		c.Keybindings = keybindings{
-			FocusChannelsTree:      "Alt+Left",
-			FocusMessagesView:      "Alt+Right",
-			FocusMessageInputField: "Alt+Down",
-
-			SelectPreviousMessage:       "Up",
-			SelectNextMessage:           "Down",
-			SelectFirstMessage:          "Home",
-			SelectLastMessage:           "End",
-			ReplySelectedMessage:        "Rune[r]",
-			MentionReplySelectedMessage: "Rune[R]",
-			CopySelectedMessage:         "Rune[c]",
-			SelectMessageReference:      "Rune[m]",
+		c = Config{
+			Mouse:            true,
+			Notifications:    true,
+			UserAgent:        userAgent,
+			GetMessagesLimit: 50,
+			Borders:          tview.Borders,
+			Theme: theme{
+				Background: "black",
+
+				Border:   "white",
+				Title:    "white",
+				Graphics: "white",
+				Text:     "white",
+			},
+			Keybindings: keybindings{
+				FocusChannelsTree:      "Alt+Left",
+				FocusMessagesView:      "Alt+Right",
+				FocusMessageInputField: "Alt+Down",
+
+				SelectPreviousMessage:       "Up",
+				SelectNextMessage:           "Down",
+				SelectFirstMessage:          "Home",
+				SelectLastMessage:           "End",
+				ReplySelectedMessage:        "Rune[r]",
+				MentionReplySelectedMessage: "Rune[R]",
+				CopySelectedMessage:         "Rune[c]",
+				SelectMessageReference:      "Rune[m]",
+			},
 		}
-		c.Borders = tview.Borders
-
 		err = toml.NewEncoder(f).Encode(c)
 		if err != nil {
 			panic(err)