af20148ea4
- write the deploy marker and module links through os.Root - extract forge tarballs through os.Root - bound each git call with w10k.git_timeout (default 10m) and http low-speed abort - count module refs from every worktree and basedir link, not only configured sources
140 lines
3.4 KiB
Go
140 lines
3.4 KiB
Go
package deploy
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
"time"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
type Config struct {
|
|
Cachedir string `yaml:"cachedir"`
|
|
Sources map[string]*Source `yaml:"sources"`
|
|
W10k struct {
|
|
GCGrace *time.Duration `yaml:"gc_grace"`
|
|
GitTimeout *time.Duration `yaml:"git_timeout"`
|
|
} `yaml:"w10k"`
|
|
}
|
|
|
|
type Source struct {
|
|
Name string `yaml:"-"`
|
|
Remote string `yaml:"remote"`
|
|
Basedir string `yaml:"basedir"`
|
|
PrefixRaw any `yaml:"prefix"`
|
|
IgnoreBranchPrefixes []string `yaml:"ignore_branch_prefixes"`
|
|
InvalidBranches string `yaml:"invalid_branches"`
|
|
Prefix string `yaml:"-"`
|
|
}
|
|
|
|
func LoadConfig(path string) (*Config, error) {
|
|
b, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return ParseConfig(b)
|
|
}
|
|
|
|
func ParseConfig(b []byte) (*Config, error) {
|
|
var n yaml.Node
|
|
if err := yaml.Unmarshal(b, &n); err != nil {
|
|
return nil, err
|
|
}
|
|
stripSymbols(&n)
|
|
var c Config
|
|
if err := n.Decode(&c); err != nil {
|
|
return nil, err
|
|
}
|
|
if c.Cachedir == "" {
|
|
return nil, fmt.Errorf("cachedir is required")
|
|
}
|
|
if c.W10k.GCGrace != nil && *c.W10k.GCGrace < 0 {
|
|
return nil, fmt.Errorf("w10k.gc_grace must not be negative")
|
|
}
|
|
if c.W10k.GitTimeout != nil && *c.W10k.GitTimeout <= 0 {
|
|
return nil, fmt.Errorf("w10k.git_timeout must be positive")
|
|
}
|
|
if len(c.Sources) == 0 {
|
|
return nil, fmt.Errorf("no sources defined")
|
|
}
|
|
var err error
|
|
if c.Cachedir, err = filepath.Abs(c.Cachedir); err != nil {
|
|
return nil, err
|
|
}
|
|
for name, s := range c.Sources {
|
|
if !wordOnly.MatchString(name) {
|
|
return nil, fmt.Errorf("source %q: name must match \\w+", name)
|
|
}
|
|
s.Name = name
|
|
if s.Remote == "" || s.Basedir == "" {
|
|
return nil, fmt.Errorf("source %q: remote and basedir are required", name)
|
|
}
|
|
if s.Basedir, err = filepath.Abs(s.Basedir); err != nil {
|
|
return nil, err
|
|
}
|
|
switch p := s.PrefixRaw.(type) {
|
|
case nil:
|
|
case bool:
|
|
if p {
|
|
s.Prefix = name
|
|
}
|
|
case string:
|
|
s.Prefix = p
|
|
default:
|
|
return nil, fmt.Errorf("source %q: prefix must be a bool or string", name)
|
|
}
|
|
s.InvalidBranches = strings.TrimPrefix(s.InvalidBranches, ":")
|
|
switch s.InvalidBranches {
|
|
case "":
|
|
s.InvalidBranches = "correct_and_warn"
|
|
case "correct_and_warn", "correct", "error":
|
|
default:
|
|
return nil, fmt.Errorf("source %q: invalid_branches must be correct_and_warn, correct or error", name)
|
|
}
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
func stripSymbols(n *yaml.Node) {
|
|
if n.Kind == yaml.MappingNode {
|
|
for i := 0; i < len(n.Content); i += 2 {
|
|
n.Content[i].Value = strings.TrimPrefix(n.Content[i].Value, ":")
|
|
}
|
|
}
|
|
for _, c := range n.Content {
|
|
stripSymbols(c)
|
|
}
|
|
}
|
|
|
|
var (
|
|
wordOnly = regexp.MustCompile(`^\w+$`)
|
|
nonWord = regexp.MustCompile(`\W`)
|
|
)
|
|
|
|
func (s *Source) Ignored(branch string) bool {
|
|
for _, p := range s.IgnoreBranchPrefixes {
|
|
if strings.HasPrefix(branch, p) {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
|
|
// EnvName applies prefix and invalid_branches handling; corrected reports a \W substitution.
|
|
func (s *Source) EnvName(branch string) (name string, corrected bool, err error) {
|
|
name = branch
|
|
if s.Prefix != "" {
|
|
name = s.Prefix + "_" + branch
|
|
}
|
|
if wordOnly.MatchString(name) {
|
|
return name, false, nil
|
|
}
|
|
if s.InvalidBranches == "error" {
|
|
return "", false, fmt.Errorf("branch %q is not a valid environment name", branch)
|
|
}
|
|
return nonWord.ReplaceAllString(name, "_"), true, nil
|
|
}
|