buildr/internal/config/buildr.go
Peter e60726ef9e
All checks were successful
continuous-integration/drone/pr Build is passing
continuous-integration/drone/push Build is passing
feat: implement new and man for plugin modules
- use extracted shared libraries
2023-08-23 22:06:26 +02:00

103 lines
2.4 KiB
Go

package config
import (
"net/url"
"os"
"path/filepath"
)
const defaultDirectoryPermissions = 0o755
type PluginReference struct {
Checksum *string `hcl:"checksum,optional"`
Name string `hcl:",label"`
URL string `hcl:"url"`
}
type Buildr struct {
BinariesDirectory string `hcl:"bin_dir,optional"`
OutDirectory string `hcl:"out_dir,optional"`
LogsDirectory string `hcl:"logs_dir,optional"`
CacheDirectory string
Plugins []PluginReference `hcl:"plugin,block"`
LogToStderr bool `hcl:"log_to_stdout,optional"`
}
func (c Buildr) PluginURLs() (pluginUrls []*url.URL, err error) {
pluginUrls = make([]*url.URL, 0, len(c.Plugins))
for _, p := range c.Plugins {
if u, err := url.Parse(p.URL); err != nil {
return nil, err
} else {
pluginUrls = append(pluginUrls, u)
}
}
return pluginUrls, nil
}
func (c *Buildr) SetupDirectories(buildRDir, repoRoot string, cleanDirectories bool) error {
if ucd, err := os.UserCacheDir(); err != nil {
return err
} else {
c.CacheDirectory = filepath.Join(ucd, "buildr")
}
if err := createCleanDir(c.CacheDirectory, false); err != nil {
return err
}
if c.BinariesDirectory == "" {
c.BinariesDirectory = filepath.Join(buildRDir, "bin")
} else if !filepath.IsAbs(c.BinariesDirectory) {
c.BinariesDirectory = filepath.Join(repoRoot, c.BinariesDirectory)
}
if err := createCleanDir(c.BinariesDirectory, false); err != nil {
return err
}
if c.OutDirectory == "" {
c.OutDirectory = filepath.Join(buildRDir, "out")
} else if !filepath.IsAbs(c.OutDirectory) {
c.OutDirectory = filepath.Join(repoRoot, c.OutDirectory)
}
if err := createCleanDir(c.OutDirectory, cleanDirectories); err != nil {
return err
}
if c.LogsDirectory == "" {
c.LogsDirectory = filepath.Join(buildRDir, "logs")
} else if !filepath.IsAbs(c.LogsDirectory) {
c.LogsDirectory = filepath.Join(repoRoot, c.LogsDirectory)
}
if err := createCleanDir(c.LogsDirectory, cleanDirectories); err != nil {
return err
}
return nil
}
func createCleanDir(dir string, clean bool) error {
if err := os.MkdirAll(dir, defaultDirectoryPermissions); err != nil {
return err
}
if clean {
matches, err := filepath.Glob(filepath.Join(dir, "*"))
if err != nil {
return err
}
for i := range matches {
if err := os.RemoveAll(matches[i]); err != nil {
return err
}
}
}
return nil
}