fix: use dynamic user cache dir instead of hardcoding value
This commit is contained in:
parent
5410c7f6c3
commit
2e8d188246
3 changed files with 13 additions and 12 deletions
|
@ -7,10 +7,9 @@ import (
|
|||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
metadata "courgette/metadata"
|
||||
)
|
||||
|
||||
const defaultCacheDirectory = "~/.cache/courgette"
|
||||
|
||||
type CacheConfiguration struct {
|
||||
Dir string `yaml:"dir"`
|
||||
}
|
||||
|
@ -29,6 +28,11 @@ type Configuration struct {
|
|||
Cache CacheConfiguration `yaml:"cache"`
|
||||
}
|
||||
|
||||
func getDefaultCacheDirectory() string {
|
||||
dir, _ := os.UserCacheDir()
|
||||
return filepath.Join(dir, metadata.APPLICATION_NAME)
|
||||
}
|
||||
|
||||
func (c Configuration) GetCacheDir() string {
|
||||
currentUser, _ := user.Current()
|
||||
homeDir := currentUser.HomeDir
|
||||
|
@ -53,7 +57,7 @@ func applyConfigDefaults(config Configuration) Configuration {
|
|||
Driver: "podman",
|
||||
},
|
||||
Cache: CacheConfiguration{
|
||||
Dir: defaultCacheDirectory,
|
||||
Dir: getDefaultCacheDirectory(),
|
||||
},
|
||||
}
|
||||
|
||||
|
@ -77,7 +81,7 @@ func NewConfigFromYamlBytes(configRaw []byte) (*Configuration, error) {
|
|||
|
||||
config = applyConfigDefaults(config)
|
||||
|
||||
if err := os.MkdirAll(config.Cache.Dir, 0755); err != nil {
|
||||
if err := os.MkdirAll(config.GetCacheDir(), 0755); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
@ -3,20 +3,14 @@ package commands
|
|||
import (
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGetCacheDirDefault(t *testing.T) {
|
||||
configuration, _ := NewConfigFromYamlBytes([]byte(""))
|
||||
|
||||
parts := strings.Split(defaultCacheDirectory, "/")
|
||||
user, _ := user.Current()
|
||||
defaultPathParts := []string{user.HomeDir}
|
||||
defaultPathParts = append(defaultPathParts, parts[1:]...)
|
||||
defaultPath := filepath.Join(defaultPathParts...)
|
||||
if configuration.GetCacheDir() != defaultPath {
|
||||
t.Errorf("Expected default %s, got %s instead.", defaultPath, configuration.GetCacheDir())
|
||||
if configuration.GetCacheDir() != getDefaultCacheDirectory() {
|
||||
t.Errorf("Expected default %s, got %s instead.", getDefaultCacheDirectory(), configuration.GetCacheDir())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
3
metadata/constants.go
Normal file
3
metadata/constants.go
Normal file
|
@ -0,0 +1,3 @@
|
|||
package metadata
|
||||
|
||||
const APPLICATION_NAME = "courgette"
|
Loading…
Reference in a new issue