feat: add cache dir to configuration + getter

This commit is contained in:
Marc 2024-08-23 13:20:40 -04:00
parent 1aa62019cb
commit c989aba61b
Signed by: marc
GPG key ID: 048E042F22B5DC79
2 changed files with 100 additions and 9 deletions

View file

@ -3,8 +3,18 @@ package commands
import (
"gopkg.in/yaml.v3"
"io/ioutil"
"os"
"os/user"
"path/filepath"
"strings"
)
const defaultCacheDirectory = "~/.cache/courgette"
type CacheConfiguration struct {
Dir string `yaml:"dir"`
}
type RunnerConfiguration struct {
Labels map[string]string `yaml:"labels"`
}
@ -16,6 +26,25 @@ type ContainerConfiguration struct {
type Configuration struct {
Containers ContainerConfiguration `yaml:"containers"`
Runner RunnerConfiguration `yaml:"runner"`
Cache CacheConfiguration `yaml:"cache"`
}
func (c Configuration) GetCacheDir() string {
currentUser, _ := user.Current()
homeDir := currentUser.HomeDir
cacheDir := c.Cache.Dir
if filepath.HasPrefix(cacheDir, "~") {
parts := strings.Split(cacheDir, "/")
finalParts := []string{homeDir}
finalParts = append(finalParts, parts[1:]...)
cacheDir = filepath.Join(finalParts...)
}
return cacheDir
}
func applyConfigDefaults(config Configuration) Configuration {
@ -23,15 +52,38 @@ func applyConfigDefaults(config Configuration) Configuration {
Containers: ContainerConfiguration{
Driver: "podman",
},
Cache: CacheConfiguration{
Dir: defaultCacheDirectory,
},
}
if config.Containers.Driver == "" {
config.Containers.Driver = defaults.Containers.Driver
}
if config.Cache.Dir == "" {
config.Cache.Dir = defaults.Cache.Dir
}
return config
}
func NewConfigFromYamlBytes(configRaw []byte) (*Configuration, error) {
var config Configuration
if yamlError := yaml.Unmarshal(configRaw, &config); yamlError != nil {
return nil, yamlError
}
config = applyConfigDefaults(config)
if err := os.MkdirAll(config.Cache.Dir, 0755); err != nil {
return nil, err
}
return &config, nil
}
func NewConfigFromFile(configPath string) (*Configuration, error) {
configRaw, err := ioutil.ReadFile(configPath)
@ -39,13 +91,5 @@ func NewConfigFromFile(configPath string) (*Configuration, error) {
return nil, err
}
var config Configuration
if yamlError := yaml.Unmarshal(configRaw, &config); yamlError != nil {
return nil, yamlError
}
config = applyConfigDefaults(config)
return &config, nil
return NewConfigFromYamlBytes(configRaw)
}

View file

@ -0,0 +1,47 @@
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())
}
}
func TestGetCacheDirExpandsTilde(t *testing.T) {
configuration, _ := NewConfigFromYamlBytes([]byte(""))
user, _ := user.Current()
cacheDir := configuration.GetCacheDir()
if filepath.HasPrefix(configuration.GetCacheDir(), "~") {
t.Errorf("Expected tilde to be expanded, was not.")
}
if !filepath.HasPrefix(cacheDir, user.HomeDir) {
t.Errorf("Expected tilder to be expanded, was not.")
}
}
func TestGetCacheDirReturnsCachePath(t *testing.T) {
sample := `
cache:
dir: "/tmp/cache-dir"
`
configuration, _ := NewConfigFromYamlBytes([]byte(sample))
cacheDir := configuration.GetCacheDir()
if cacheDir != "/tmp/cache-dir" {
t.Errorf("Unexpected cache path: %s", cacheDir)
}
}