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.
This commit is contained in:
Jeffrey Paul 2024-05-14 05:07:02 -07:00
parent d020a1966e
commit a135ef0778
1 changed files with 4 additions and 3 deletions

View File

@ -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)
}