Commit 72b93bbe58

72b93bbe5854ffd30fd48e724c4939865b281293

parent: 638f98c938

Unsigned

Wraient <wraient.911@gmail.com> · 2026-04-28 01:01 UTC

refactor: Standardize storage path resolution

Layout: unified · split

cmd/curd/main.go +9 −6
@@ -29,14 +29,17 @@ func main() {
2929
3030 internal.SetGlobalAnime(&anime)
3131
32 var homeDir string
33 if runtime.GOOS == "windows" {
34 homeDir = os.Getenv("USERPROFILE")
35 } else {
36 homeDir = os.Getenv("HOME")
32 configDir, err := os.UserConfigDir()
33 if err != nil {
34 // Fallback if UserConfigDir fails
35 if runtime.GOOS == "windows" {
36 configDir = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Roaming")
37 } else {
38 configDir = filepath.Join(os.Getenv("HOME"), ".config")
39 }
3740 }
3841
39 configFilePath := filepath.Join(homeDir, ".config", "curd", "curd.conf")
42 configFilePath := filepath.Join(configDir, "curd", "curd.conf")
4043
4144 // load curd userCurdConfig
4245 userCurdConfig, err := internal.LoadConfig(configFilePath)
internal/anime_list.go +1 −6
@@ -6,7 +6,6 @@ import (
66 "fmt"
77 "io"
88 "net/http"
9 "os"
109 "path/filepath"
1110 "strings"
1211)
@@ -94,11 +93,7 @@ func searchAllAnime(query, mode string) ([]SelectionOption, error) {
9493
9594func searchAnimeByMode(query, mode, preferredMode string) ([]SelectionOption, error) {
9695 userCurdConfig := GetGlobalConfig()
97 if userCurdConfig == nil {
98 logFile = os.ExpandEnv("$HOME/.local/share/curd/debug.log")
99 } else {
100 logFile = filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "debug.log")
101 }
96 logFile = filepath.Join(GetStoragePath(), "debug.log")
10297 const (
10398 agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0"
10499 allanimeRef = "https://allanime.to"
internal/config.go +10
@@ -63,6 +63,13 @@ type CurdConfig struct {
6363 Provider string `config:"Provider"`
6464}
6565
66func GetStoragePath() string {
67 if globalConfig != nil && globalConfig.StoragePath != "" {
68 return os.ExpandEnv(globalConfig.StoragePath)
69 }
70 return filepath.Join(os.ExpandEnv("$HOME"), ".local", "share", "curd")
71}
72
6673// Default configuration values as a map
6774func defaultConfigMap() map[string]string {
6875 return map[string]string{
@@ -124,9 +131,12 @@ func parseStringArray(value string) []string {
124131 return result
125132}
126133
134var GlobalConfigPath string
135
127136// LoadConfig reads or creates the config file, adds missing fields, and returns the populated CurdConfig struct
128137func LoadConfig(configPath string) (CurdConfig, error) {
129138 configPath = os.ExpandEnv(configPath) // Substitute environment variables like $HOME
139 GlobalConfigPath = configPath
130140
131141 // Check if config file exists
132142 if _, err := os.Stat(configPath); os.IsNotExist(err) {
internal/curd.go +1 −1
@@ -2109,7 +2109,7 @@ func ChangeProvider(userCurdConfig *CurdConfig) {
21092109 CurrentProvider = nil // reset the provider instance
21102110
21112111 // Save to config file
2112 configPath := filepath.Join(os.ExpandEnv("$HOME"), ".config", "curd", "curd.conf")
2112 configPath := GlobalConfigPath
21132113 configMap, err := LoadConfigFromFile(configPath)
21142114 if err == nil {
21152115 configMap["Provider"] = selected.Key
internal/provider_animepahe.go +5 −1
@@ -19,15 +19,19 @@ import (
1919)
2020
2121var animepaheCookiesBypassed bool
22var cookieFilePath = filepath.Join(os.ExpandEnv("$HOME"), ".local", "share", "curd", "animepahe_cookies.json")
22func getCookieFilePath() string {
23 return filepath.Join(GetStoragePath(), "animepahe_cookies.json")
24}
2325
2426func saveCookies(cookies []*http.Cookie) {
27 cookieFilePath := getCookieFilePath()
2528 os.MkdirAll(filepath.Dir(cookieFilePath), 0755)
2629 b, _ := json.Marshal(cookies)
2730 os.WriteFile(cookieFilePath, b, 0644)
2831}
2932
3033func loadCookies() []*http.Cookie {
34 cookieFilePath := getCookieFilePath()
3135 b, err := os.ReadFile(cookieFilePath)
3236 if err != nil {
3337 return nil
internal/rofi.go +1 −6
@@ -3,7 +3,6 @@ package internal
33import (
44 "bytes"
55 "fmt"
6 "os"
76 "os/exec"
87 "path/filepath"
98 "strings"
@@ -37,12 +36,8 @@ func GetTokenFromRofi() (string, error) {
3736
3837// GetUserInputFromRofi prompts the user for input using Rofi with a custom message
3938func GetUserInputFromRofi(message string) (string, error) {
40 userCurdConfig := GetGlobalConfig()
41 if userCurdConfig.StoragePath == "" {
42 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd")
43 }
4439 // Create the Rofi command
45 cmd := exec.Command("rofi", "-dmenu", "-theme", filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "userinput.rasi"), "-p", "Input", "-mesg", message)
40 cmd := exec.Command("rofi", "-dmenu", "-theme", filepath.Join(GetStoragePath(), "userinput.rasi"), "-p", "Input", "-mesg", message)
4641
4742 // Set up pipes for output
4843 var out bytes.Buffer
internal/selection_menu.go +3 −11
@@ -382,10 +382,7 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
382382func DynamicSelectPreviewWithRefresh(options map[string]RofiSelectPreview, addnewoption bool, refreshConfig *PreviewSelectionRefreshConfig) (SelectionOption, error) {
383383 go preDownloadImages(options, 14)
384384
385 userCurdConfig := GetGlobalConfig()
386 if userCurdConfig.StoragePath == "" {
387 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd")
388 }
385 // Removed boilerplate check
389386
390387 currentOptions := options
391388
@@ -409,7 +406,7 @@ func DynamicSelectPreviewWithRefresh(options map[string]RofiSelectPreview, addne
409406 rofiInput.WriteString("Back\n")
410407 rofiInput.WriteString("Quit\n")
411408
412 configPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanimepreview.rasi")
409 configPath := filepath.Join(GetStoragePath(), "selectanimepreview.rasi")
413410 cmd := exec.Command("rofi", "-dmenu", "-theme", configPath, "-show-icons", "-p", "Select Anime", "-i", "-no-custom")
414411 cmd.Stdin = strings.NewReader(rofiInput.String())
415412 var stdout, stderr bytes.Buffer
@@ -561,16 +558,11 @@ func RofiSelect(options []SelectionOption, isHomeMenu bool) (SelectionOption, er
561558}
562559
563560func RofiSelectWithRefresh(options []SelectionOption, isHomeMenu bool, refreshConfig *SelectionRefreshConfig) (SelectionOption, error) {
564 userCurdConfig := GetGlobalConfig()
565 if userCurdConfig.StoragePath == "" {
566 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd")
567 }
568
569561 currentOptions := options
570562
571563 for {
572564 optionsString := buildRofiOptionsString(currentOptions, isHomeMenu)
573 configPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanime.rasi")
565 configPath := filepath.Join(GetStoragePath(), "selectanime.rasi")
574566 cmd := exec.Command("rofi", "-dmenu", "-theme", configPath, "-i", "-p", "Select")
575567 cmd.Stdin = strings.NewReader(optionsString)
576568 var stdout, stderr bytes.Buffer