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() {
29 29
30 internal.SetGlobalAnime(&anime) 30 internal.SetGlobalAnime(&anime)
31 31
32 var homeDir string 32 configDir, err := os.UserConfigDir()
33 if runtime.GOOS == "windows" { 33 if err != nil {
34 homeDir = os.Getenv("USERPROFILE") 34 // Fallback if UserConfigDir fails
35 } else { 35 if runtime.GOOS == "windows" {
36 homeDir = os.Getenv("HOME") 36 configDir = filepath.Join(os.Getenv("USERPROFILE"), "AppData", "Roaming")
37 } else {
38 configDir = filepath.Join(os.Getenv("HOME"), ".config")
39 }
37 } 40 }
38 41
39 configFilePath := filepath.Join(homeDir, ".config", "curd", "curd.conf") 42 configFilePath := filepath.Join(configDir, "curd", "curd.conf")
40 43
41 // load curd userCurdConfig 44 // load curd userCurdConfig
42 userCurdConfig, err := internal.LoadConfig(configFilePath) 45 userCurdConfig, err := internal.LoadConfig(configFilePath)
internal/anime_list.go +1 −6
@@ -6,7 +6,6 @@ import (
6 "fmt" 6 "fmt"
7 "io" 7 "io"
8 "net/http" 8 "net/http"
9 "os"
10 "path/filepath" 9 "path/filepath"
11 "strings" 10 "strings"
12) 11)
@@ -94,11 +93,7 @@ func searchAllAnime(query, mode string) ([]SelectionOption, error) {
94 93
95func searchAnimeByMode(query, mode, preferredMode string) ([]SelectionOption, error) { 94func searchAnimeByMode(query, mode, preferredMode string) ([]SelectionOption, error) {
96 userCurdConfig := GetGlobalConfig() 95 userCurdConfig := GetGlobalConfig()
97 if userCurdConfig == nil { 96 logFile = filepath.Join(GetStoragePath(), "debug.log")
98 logFile = os.ExpandEnv("$HOME/.local/share/curd/debug.log")
99 } else {
100 logFile = filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "debug.log")
101 }
102 const ( 97 const (
103 agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0" 98 agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/121.0"
104 allanimeRef = "https://allanime.to" 99 allanimeRef = "https://allanime.to"
internal/config.go +10
@@ -63,6 +63,13 @@ type CurdConfig struct {
63 Provider string `config:"Provider"` 63 Provider string `config:"Provider"`
64} 64}
65 65
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
66// Default configuration values as a map 73// Default configuration values as a map
67func defaultConfigMap() map[string]string { 74func defaultConfigMap() map[string]string {
68 return map[string]string{ 75 return map[string]string{
@@ -124,9 +131,12 @@ func parseStringArray(value string) []string {
124 return result 131 return result
125} 132}
126 133
134var GlobalConfigPath string
135
127// LoadConfig reads or creates the config file, adds missing fields, and returns the populated CurdConfig struct 136// LoadConfig reads or creates the config file, adds missing fields, and returns the populated CurdConfig struct
128func LoadConfig(configPath string) (CurdConfig, error) { 137func LoadConfig(configPath string) (CurdConfig, error) {
129 configPath = os.ExpandEnv(configPath) // Substitute environment variables like $HOME 138 configPath = os.ExpandEnv(configPath) // Substitute environment variables like $HOME
139 GlobalConfigPath = configPath
130 140
131 // Check if config file exists 141 // Check if config file exists
132 if _, err := os.Stat(configPath); os.IsNotExist(err) { 142 if _, err := os.Stat(configPath); os.IsNotExist(err) {
internal/curd.go +1 −1
@@ -2109,7 +2109,7 @@ func ChangeProvider(userCurdConfig *CurdConfig) {
2109 CurrentProvider = nil // reset the provider instance 2109 CurrentProvider = nil // reset the provider instance
2110 2110
2111 // Save to config file 2111 // Save to config file
2112 configPath := filepath.Join(os.ExpandEnv("$HOME"), ".config", "curd", "curd.conf") 2112 configPath := GlobalConfigPath
2113 configMap, err := LoadConfigFromFile(configPath) 2113 configMap, err := LoadConfigFromFile(configPath)
2114 if err == nil { 2114 if err == nil {
2115 configMap["Provider"] = selected.Key 2115 configMap["Provider"] = selected.Key
internal/provider_animepahe.go +5 −1
@@ -19,15 +19,19 @@ import (
19) 19)
20 20
21var animepaheCookiesBypassed bool 21var 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}
23 25
24func saveCookies(cookies []*http.Cookie) { 26func saveCookies(cookies []*http.Cookie) {
27 cookieFilePath := getCookieFilePath()
25 os.MkdirAll(filepath.Dir(cookieFilePath), 0755) 28 os.MkdirAll(filepath.Dir(cookieFilePath), 0755)
26 b, _ := json.Marshal(cookies) 29 b, _ := json.Marshal(cookies)
27 os.WriteFile(cookieFilePath, b, 0644) 30 os.WriteFile(cookieFilePath, b, 0644)
28} 31}
29 32
30func loadCookies() []*http.Cookie { 33func loadCookies() []*http.Cookie {
34 cookieFilePath := getCookieFilePath()
31 b, err := os.ReadFile(cookieFilePath) 35 b, err := os.ReadFile(cookieFilePath)
32 if err != nil { 36 if err != nil {
33 return nil 37 return nil
internal/rofi.go +1 −6
@@ -3,7 +3,6 @@ package internal
3import ( 3import (
4 "bytes" 4 "bytes"
5 "fmt" 5 "fmt"
6 "os"
7 "os/exec" 6 "os/exec"
8 "path/filepath" 7 "path/filepath"
9 "strings" 8 "strings"
@@ -37,12 +36,8 @@ func GetTokenFromRofi() (string, error) {
37 36
38// GetUserInputFromRofi prompts the user for input using Rofi with a custom message 37// GetUserInputFromRofi prompts the user for input using Rofi with a custom message
39func GetUserInputFromRofi(message string) (string, error) { 38func GetUserInputFromRofi(message string) (string, error) {
40 userCurdConfig := GetGlobalConfig()
41 if userCurdConfig.StoragePath == "" {
42 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd")
43 }
44 // Create the Rofi command 39 // 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)
46 41
47 // Set up pipes for output 42 // Set up pipes for output
48 var out bytes.Buffer 43 var out bytes.Buffer
internal/selection_menu.go +3 −11
@@ -382,10 +382,7 @@ func DynamicSelectPreview(options map[string]RofiSelectPreview, addnewoption boo
382func DynamicSelectPreviewWithRefresh(options map[string]RofiSelectPreview, addnewoption bool, refreshConfig *PreviewSelectionRefreshConfig) (SelectionOption, error) { 382func DynamicSelectPreviewWithRefresh(options map[string]RofiSelectPreview, addnewoption bool, refreshConfig *PreviewSelectionRefreshConfig) (SelectionOption, error) {
383 go preDownloadImages(options, 14) 383 go preDownloadImages(options, 14)
384 384
385 userCurdConfig := GetGlobalConfig() 385 // Removed boilerplate check
386 if userCurdConfig.StoragePath == "" {
387 userCurdConfig.StoragePath = os.ExpandEnv("${HOME}/.local/share/curd")
388 }
389 386
390 currentOptions := options 387 currentOptions := options
391 388
@@ -409,7 +406,7 @@ func DynamicSelectPreviewWithRefresh(options map[string]RofiSelectPreview, addne
409 rofiInput.WriteString("Back\n") 406 rofiInput.WriteString("Back\n")
410 rofiInput.WriteString("Quit\n") 407 rofiInput.WriteString("Quit\n")
411 408
412 configPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanimepreview.rasi") 409 configPath := filepath.Join(GetStoragePath(), "selectanimepreview.rasi")
413 cmd := exec.Command("rofi", "-dmenu", "-theme", configPath, "-show-icons", "-p", "Select Anime", "-i", "-no-custom") 410 cmd := exec.Command("rofi", "-dmenu", "-theme", configPath, "-show-icons", "-p", "Select Anime", "-i", "-no-custom")
414 cmd.Stdin = strings.NewReader(rofiInput.String()) 411 cmd.Stdin = strings.NewReader(rofiInput.String())
415 var stdout, stderr bytes.Buffer 412 var stdout, stderr bytes.Buffer
@@ -561,16 +558,11 @@ func RofiSelect(options []SelectionOption, isHomeMenu bool) (SelectionOption, er
561} 558}
562 559
563func RofiSelectWithRefresh(options []SelectionOption, isHomeMenu bool, refreshConfig *SelectionRefreshConfig) (SelectionOption, error) { 560func 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
569 currentOptions := options 561 currentOptions := options
570 562
571 for { 563 for {
572 optionsString := buildRofiOptionsString(currentOptions, isHomeMenu) 564 optionsString := buildRofiOptionsString(currentOptions, isHomeMenu)
573 configPath := filepath.Join(os.ExpandEnv(userCurdConfig.StoragePath), "selectanime.rasi") 565 configPath := filepath.Join(GetStoragePath(), "selectanime.rasi")
574 cmd := exec.Command("rofi", "-dmenu", "-theme", configPath, "-i", "-p", "Select") 566 cmd := exec.Command("rofi", "-dmenu", "-theme", configPath, "-i", "-p", "Select")
575 cmd.Stdin = strings.NewReader(optionsString) 567 cmd.Stdin = strings.NewReader(optionsString)
576 var stdout, stderr bytes.Buffer 568 var stdout, stderr bytes.Buffer