2024-08-01 22:43:18 +00:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2024-09-01 17:31:30 +00:00
|
|
|
metadata "courgette/metadata"
|
2024-08-01 22:43:18 +00:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"io/ioutil"
|
2024-08-23 17:20:40 +00:00
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2024-08-01 22:43:18 +00:00
|
|
|
)
|
|
|
|
|
2024-08-23 17:20:40 +00:00
|
|
|
type CacheConfiguration struct {
|
|
|
|
Dir string `yaml:"dir"`
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:43:18 +00:00
|
|
|
type RunnerConfiguration struct {
|
|
|
|
Labels map[string]string `yaml:"labels"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ContainerConfiguration struct {
|
|
|
|
Driver string `yaml:"driver"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Configuration struct {
|
|
|
|
Containers ContainerConfiguration `yaml:"containers"`
|
|
|
|
Runner RunnerConfiguration `yaml:"runner"`
|
2024-08-23 17:20:40 +00:00
|
|
|
Cache CacheConfiguration `yaml:"cache"`
|
|
|
|
}
|
|
|
|
|
2024-09-01 17:18:13 +00:00
|
|
|
func getDefaultCacheDirectory() string {
|
|
|
|
dir, _ := os.UserCacheDir()
|
|
|
|
return filepath.Join(dir, metadata.APPLICATION_NAME)
|
|
|
|
}
|
|
|
|
|
2024-08-23 17:20:40 +00:00
|
|
|
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
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func applyConfigDefaults(config Configuration) Configuration {
|
|
|
|
defaults := Configuration{
|
|
|
|
Containers: ContainerConfiguration{
|
|
|
|
Driver: "podman",
|
|
|
|
},
|
2024-08-23 17:20:40 +00:00
|
|
|
Cache: CacheConfiguration{
|
2024-09-01 17:18:13 +00:00
|
|
|
Dir: getDefaultCacheDirectory(),
|
2024-08-23 17:20:40 +00:00
|
|
|
},
|
2024-08-01 22:43:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.Containers.Driver == "" {
|
|
|
|
config.Containers.Driver = defaults.Containers.Driver
|
|
|
|
}
|
|
|
|
|
2024-08-23 17:20:40 +00:00
|
|
|
if config.Cache.Dir == "" {
|
|
|
|
config.Cache.Dir = defaults.Cache.Dir
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:43:18 +00:00
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2024-08-23 17:20:40 +00:00
|
|
|
func NewConfigFromYamlBytes(configRaw []byte) (*Configuration, error) {
|
2024-08-01 22:43:18 +00:00
|
|
|
var config Configuration
|
|
|
|
|
|
|
|
if yamlError := yaml.Unmarshal(configRaw, &config); yamlError != nil {
|
|
|
|
return nil, yamlError
|
|
|
|
}
|
|
|
|
|
|
|
|
config = applyConfigDefaults(config)
|
|
|
|
|
2024-09-01 17:18:13 +00:00
|
|
|
if err := os.MkdirAll(config.GetCacheDir(), 0755); err != nil {
|
2024-08-23 17:20:40 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-01 22:43:18 +00:00
|
|
|
return &config, nil
|
|
|
|
}
|
2024-08-23 17:20:40 +00:00
|
|
|
|
|
|
|
func NewConfigFromFile(configPath string) (*Configuration, error) {
|
|
|
|
configRaw, err := ioutil.ReadFile(configPath)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return NewConfigFromYamlBytes(configRaw)
|
|
|
|
}
|