This repository has been archived on 2026-11-27. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
unkin-agent a04351cfa1
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/test Pipeline was successful
ci/woodpecker/pr/pre-commit Pipeline was successful
ci/woodpecker/pr/lint Pipeline was successful
Model the toml gem's load for declared w10k.gems
Generate grafana_ldap_config when r10k.yaml declares toml; a module copy of a gem path, an unmodelled gem file or a module opening a gem namespace still skips.
2026-11-08 23:09:07 +11:00

160 lines
4.2 KiB
Go

package deploy
import (
"fmt"
"os"
"path/filepath"
"regexp"
"slices"
"strings"
"time"
"git.unkin.net/unkin/w10k/internal/gentypes"
"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"`
GenerateTypes bool `yaml:"generate_types"`
Aliases map[string]string `yaml:"aliases"`
Gems []string `yaml:"gems"`
} `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")
}
for alias, target := range c.W10k.Aliases {
if !wordOnly.MatchString(alias) || !wordOnly.MatchString(target) {
return nil, fmt.Errorf("w10k.aliases: %s -> %s: names must match \\w+", alias, target)
}
if _, ok := c.W10k.Aliases[target]; ok {
return nil, fmt.Errorf("w10k.aliases: %s -> %s: target is an alias", alias, target)
}
}
for _, g := range c.W10k.Gems {
if !gentypes.KnownGem(g) {
return nil, fmt.Errorf("w10k.gems: %q is not a gem w10k models", g)
}
}
slices.Sort(c.W10k.Gems)
c.W10k.Gems = slices.Compact(c.W10k.Gems)
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
}