From a135ef077882e551420c2b872b124a4b23b01bf6 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 14 May 2024 05:07:02 -0700 Subject: [PATCH] aider: Use os.UserCacheDir to select a platform-specific cache directory instead of /var/cache. # Aider chat conversation: USER: use an appropriate third party module to select a platform specific directory instead of /var/cache so that our module works on macos as well ASSISTANT: To make the module work on macOS as well, we can use the `os.UserCacheDir` function from the `os` package to select a platform-specific directory for caching. This function returns the default root directory for user-specific cached data. Here are the changes needed: 1. Import the `os` package. 2. Replace the hardcoded `/var/cache` directory with the platform-specific cache directory. Let's update the `relp_handler.go` file accordingly: relp_handler.go ```go <<<<<<< SEARCH "path/filepath" ======= "os" "path/filepath" >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH cacheDir = os.Getenv("LOGGER_CACHE_DIR") ======= cacheDir, _ = os.UserCacheDir() >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH if err := os.MkdirAll(cacheDir, 0755); err != nil { ======= if err := os.MkdirAll(filepath.Join(cacheDir, "simplelog"), 0755); err != nil { >>>>>>> REPLACE ``` relp_handler.go ```go <<<<<<< SEARCH fileName := filepath.Join(cacheDir, uuid.New().String()+".logevent") ======= fileName := filepath.Join(cacheDir, "simplelog", uuid.New().String()+".logevent") >>>>>>> REPLACE ``` This will ensure that the cache directory is platform-specific and works on macOS as well. --- relp_handler.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/relp_handler.go b/relp_handler.go index 3c98bae..01837de 100644 --- a/relp_handler.go +++ b/relp_handler.go @@ -10,6 +10,7 @@ import ( "net" "net/url" "os" + "os" "path/filepath" "strconv" "time" @@ -34,7 +35,7 @@ func getEnvAsDuration(name string, defaultVal time.Duration) time.Duration { } var ( - cacheDir = os.Getenv("LOGGER_CACHE_DIR") + cacheDir, _ = os.UserCacheDir() diskBufferLimit = getEnvAsInt("LOGGER_DISK_BUFFER_LIMIT", 100) diskWriteInterval = getEnvAsDuration("LOGGER_DISK_WRITE_INTERVAL", time.Second) relpDebug = os.Getenv("RELP_DEBUG") != "" @@ -57,7 +58,7 @@ func NewRELPHandler(relpURL string) (*RELPHandler, error) { if parsedURL.Scheme != "tcp" { return nil, fmt.Errorf("RELP URL must have the tcp scheme, got %s", parsedURL.Scheme) } - if err := os.MkdirAll(cacheDir, 0755); err != nil { + if err := os.MkdirAll(filepath.Join(cacheDir, "simplelog"), 0755); err != nil { return nil, fmt.Errorf("Failed to create cache directory: %v", err) } r := &RELPHandler{ @@ -257,7 +258,7 @@ func (r *RELPHandler) Shutdown() error { } func (r *RELPHandler) writeFailedToDisk(event Event) { - fileName := filepath.Join(cacheDir, uuid.New().String()+".logevent") + fileName := filepath.Join(cacheDir, "simplelog", uuid.New().String()+".logevent") data, _ := json.Marshal(event) ioutil.WriteFile(fileName, data, 0600) }