package deploy import ( "fmt" "path/filepath" "regexp" "strings" ) type Puppetfile struct { Forge string Moduledir string Modules []Module } type Module struct { Owner string Name string Version string Git string RefKind string Ref string InstallPath string } // Path is the module's install location relative to the environment root. func (m Module) Path(pf *Puppetfile) string { dir := pf.Moduledir if m.InstallPath != "" { dir = m.InstallPath } return filepath.Join(dir, m.Name) } type tokKind int const ( tIdent tokKind = iota tString tSymbol tLabel tArrow tComma tLParen tRParen tNewline tEOF ) type token struct { kind tokKind val string line int } func isIdent(c byte) bool { return c == '_' || c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' } func lex(src string) ([]token, error) { var toks []token line := 1 for i := 0; i < len(src); { c := src[i] switch { case c == ' ' || c == '\t' || c == '\r': i++ case c == '#': for i < len(src) && src[i] != '\n' { i++ } case c == '\n': toks = append(toks, token{tNewline, "", line}) line++ i++ case c == '\'' || c == '"': var sb strings.Builder j := i + 1 for ; j < len(src) && src[j] != c; j++ { if src[j] == '\n' { break } if src[j] == '\\' && j+1 < len(src) { j++ } sb.WriteByte(src[j]) } if j >= len(src) || src[j] != c { return nil, fmt.Errorf("line %d: unterminated string", line) } toks = append(toks, token{tString, sb.String(), line}) i = j + 1 case c == ':' && i+1 < len(src) && isIdent(src[i+1]): j := i + 1 for j < len(src) && isIdent(src[j]) { j++ } toks = append(toks, token{tSymbol, src[i+1 : j], line}) i = j case isIdent(c): j := i for j < len(src) && isIdent(src[j]) { j++ } if j < len(src) && src[j] == ':' && (j+1 >= len(src) || src[j+1] != ':') { toks = append(toks, token{tLabel, src[i:j], line}) i = j + 1 } else { toks = append(toks, token{tIdent, src[i:j], line}) i = j } case strings.HasPrefix(src[i:], "=>"): toks = append(toks, token{tArrow, "", line}) i += 2 case c == ',': toks = append(toks, token{tComma, "", line}) i++ case c == '(': toks = append(toks, token{tLParen, "", line}) i++ case c == ')': toks = append(toks, token{tRParen, "", line}) i++ default: return nil, fmt.Errorf("line %d: unexpected %q", line, c) } } return append(toks, token{tEOF, "", line}), nil } type call struct { name string line int args []token opts map[string]token } func parseCalls(toks []token) ([]call, error) { var calls []call i := 0 next := func() token { t := toks[i]; i++; return t } peek := func() token { return toks[i] } skipNL := func() { for peek().kind == tNewline { i++ } } for { skipNL() t := next() if t.kind == tEOF { return calls, nil } if t.kind != tIdent { return nil, fmt.Errorf("line %d: expected a statement", t.line) } c := call{name: t.val, line: t.line, opts: map[string]token{}} paren := peek().kind == tLParen if paren { i++ skipNL() } for k := peek().kind; k != tNewline && k != tEOF && k != tRParen; k = peek().kind { a := next() switch { case a.kind == tLabel: c.opts[a.val] = next() case a.kind == tSymbol && peek().kind == tArrow: i++ c.opts[a.val] = next() case a.kind == tString || a.kind == tSymbol: c.args = append(c.args, a) default: return nil, fmt.Errorf("line %d: unexpected token in %s", a.line, c.name) } if peek().kind != tComma { break } i++ skipNL() } if paren { skipNL() if next().kind != tRParen { return nil, fmt.Errorf("line %d: expected )", c.line) } } if k := peek().kind; k != tNewline && k != tEOF { return nil, fmt.Errorf("line %d: unexpected trailing tokens", c.line) } calls = append(calls, c) } } var ( moduleName = regexp.MustCompile(`^[a-z][a-z0-9_]*$`) ownerName = regexp.MustCompile(`^[A-Za-z0-9]+$`) versionStr = regexp.MustCompile(`^[0-9A-Za-z.+-]+$`) ) func ParsePuppetfile(src string) (*Puppetfile, error) { toks, err := lex(src) if err != nil { return nil, err } calls, err := parseCalls(toks) if err != nil { return nil, err } pf := &Puppetfile{Moduledir: "modules"} for _, c := range calls { switch c.name { case "forge", "moduledir": if len(c.args) != 1 || c.args[0].kind != tString || len(c.opts) > 0 { return nil, fmt.Errorf("line %d: %s takes one string", c.line, c.name) } if c.name == "forge" { pf.Forge = c.args[0].val } else if pf.Moduledir, err = localPath(c.args[0].val); err != nil { return nil, fmt.Errorf("line %d: moduledir: %w", c.line, err) } case "mod": m, err := parseMod(c) if err != nil { return nil, fmt.Errorf("line %d: %w", c.line, err) } pf.Modules = append(pf.Modules, m) default: return nil, fmt.Errorf("line %d: unsupported statement %q", c.line, c.name) } } seen := map[string]bool{} for _, m := range pf.Modules { p := m.Path(pf) if seen[p] { return nil, fmt.Errorf("duplicate module %s", p) } seen[p] = true } for p := range seen { for d := filepath.Dir(p); d != "."; d = filepath.Dir(d) { if seen[d] { return nil, fmt.Errorf("module %s is nested inside module %s", p, d) } } } return pf, nil } func localPath(p string) (string, error) { if !filepath.IsLocal(p) { return "", fmt.Errorf("%q must be a relative path inside the environment", p) } return filepath.Clean(p), nil } func parseMod(c call) (Module, error) { var m Module if len(c.args) == 0 || c.args[0].kind != tString { return m, fmt.Errorf("mod needs a name") } full := c.args[0].val if i := strings.IndexAny(full, "-/"); i >= 0 { m.Owner, m.Name = full[:i], full[i+1:] } else { m.Name = full } if !moduleName.MatchString(m.Name) { return m, fmt.Errorf("invalid module name %q", full) } for k, v := range c.opts { if v.kind != tString { return m, fmt.Errorf("mod %s: :%s must be a string", full, k) } switch k { case "git": if v.val == "" || strings.HasPrefix(v.val, "-") { return m, fmt.Errorf("mod %s: invalid :git %q", full, v.val) } m.Git = v.val case "tag", "branch", "ref", "commit": if v.val == "" || strings.HasPrefix(v.val, "-") { return m, fmt.Errorf("mod %s: invalid :%s %q", full, k, v.val) } if m.RefKind != "" { return m, fmt.Errorf("mod %s: only one of :tag, :branch, :ref, :commit allowed", full) } m.RefKind, m.Ref = k, v.val case "install_path": p, err := localPath(v.val) if err != nil { return m, fmt.Errorf("mod %s: install_path: %w", full, err) } m.InstallPath = p default: return m, fmt.Errorf("mod %s: unsupported option :%s", full, k) } } if m.Git != "" { if len(c.args) > 1 { return m, fmt.Errorf("mod %s: git modules take no version", full) } return m, nil } if m.RefKind != "" { return m, fmt.Errorf("mod %s: :%s requires :git", full, m.RefKind) } if !ownerName.MatchString(m.Owner) { return m, fmt.Errorf("forge module %q must be owner-name", full) } if len(c.args) > 2 { return m, fmt.Errorf("mod %s: too many arguments", full) } if len(c.args) == 2 { v := c.args[1] switch { case v.val == "latest": case v.kind == tString && versionStr.MatchString(v.val): m.Version = v.val default: return m, fmt.Errorf("mod %s: unsupported version %q", full, v.val) } } return m, nil }