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"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
metadata "courgette/metadata"
|
||||||
)
|
)
|
||||||
|
|
||||||
const defaultCacheDirectory = "~/.cache/courgette"
|
|
||||||
|
|
||||||
type CacheConfiguration struct {
|
type CacheConfiguration struct {
|
||||||
Dir string `yaml:"dir"`
|
Dir string `yaml:"dir"`
|
||||||
}
|
}
|
||||||
|
@ -29,6 +28,11 @@ type Configuration struct {
|
||||||
Cache CacheConfiguration `yaml:"cache"`
|
Cache CacheConfiguration `yaml:"cache"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getDefaultCacheDirectory() string {
|
||||||
|
dir, _ := os.UserCacheDir()
|
||||||
|
return filepath.Join(dir, metadata.APPLICATION_NAME)
|
||||||
|
}
|
||||||
|
|
||||||
func (c Configuration) GetCacheDir() string {
|
func (c Configuration) GetCacheDir() string {
|
||||||
currentUser, _ := user.Current()
|
currentUser, _ := user.Current()
|
||||||
homeDir := currentUser.HomeDir
|
homeDir := currentUser.HomeDir
|
||||||
|
@ -53,7 +57,7 @@ func applyConfigDefaults(config Configuration) Configuration {
|
||||||
Driver: "podman",
|
Driver: "podman",
|
||||||
},
|
},
|
||||||
Cache: CacheConfiguration{
|
Cache: CacheConfiguration{
|
||||||
Dir: defaultCacheDirectory,
|
Dir: getDefaultCacheDirectory(),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +81,7 @@ func NewConfigFromYamlBytes(configRaw []byte) (*Configuration, error) {
|
||||||
|
|
||||||
config = applyConfigDefaults(config)
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,20 +3,14 @@ package commands
|
||||||
import (
|
import (
|
||||||
"os/user"
|
"os/user"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetCacheDirDefault(t *testing.T) {
|
func TestGetCacheDirDefault(t *testing.T) {
|
||||||
configuration, _ := NewConfigFromYamlBytes([]byte(""))
|
configuration, _ := NewConfigFromYamlBytes([]byte(""))
|
||||||
|
|
||||||
parts := strings.Split(defaultCacheDirectory, "/")
|
if configuration.GetCacheDir() != getDefaultCacheDirectory() {
|
||||||
user, _ := user.Current()
|
t.Errorf("Expected default %s, got %s instead.", getDefaultCacheDirectory(), configuration.GetCacheDir())
|
||||||
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())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
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