Skip to content

improved template support for variables and filepaths - #35

Merged
scott-cotton merged 2 commits into
mainfrom
template-parser
Mar 21, 2023
Merged

scott-cotton merged 2 commits into
mainfrom
template-parser

Conversation

@scott-cotton

Copy link
Copy Markdown
Member

This commit fixes 2 problems with the regexp based template parser: variable names needed to match the same pattern as file paths, but the matching patterns are different, and the filepath matcher was too strict.

This commit permits filepaths which contain anything except '}' supposing that leading and trailing spaces are not part of the path. I think a proper lexer would be needed to allow '}' (multiple expansions in one string trigger a problem for greedy @{.*}).

In addition, this commit extends the [yaml] argument into a generalized entity which permits raw, yaml, or binary encoding applied to either variables or operations like embed. The idea is that the value taken from a variable or operation is subsequently encoded into the document either as-is, as-yaml (including strings, numbers, arrays, dicts,...), or as-binary (base64 standard encoding as string, so as to map to Go json encoding of []byte). By default, no [encoding] defaults to [raw]. I hope to be able to extend the encoding to include

  • including templates with expansion
  • including directories

The parsing has been restructured to go over the input yaml and then replace strings. Each string then is interpreted with @{...} being expanded according to variables and files and encoding provided. When the string being interpreted is exactly @{...}, encodings other than raw are permitted.

This commit fixes 2 problems with the regexp based
template parser:  variable names needed to match the
same pattern as file paths, but the matching patterns are different,
and the filepath matcher was too strict.

This commit permits filepaths which contain anything except '}'
supposing that leading and trailing spaces are not part of the path.

In addition, this commit extends the `[yaml]` argument into a
generalized entity which permits raw, yaml, or binary encoding
applied to either variables or operations like embed.  The idea
is that the value taken from a variable or operation is subsequently
_encoded_ into the document either as-is, as-yaml (including strings,
numbers, arrays, dicts,...), or as-binary (base64 standard encoding
as string, so as to map to Go json encoding of []byte).  By default,
no `[encoding]` defaults to `[raw]`.

The parsing has been restructured to go over the input yaml
and then replace strings.  Each string then is interpreted
with  `@{...}` being expanded according to variables and files
and encoding provided.
(utils.FileReader was much more verbose than just doing it inline)

@daniel-de-vera daniel-de-vera left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@scott-cotton
scott-cotton merged commit e52cf73 into main Mar 21, 2023
@scott-cotton
scott-cotton deleted the template-parser branch March 21, 2023 18:53

@jmsktm jmsktm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it's merged now, but I didn't have much in review other than a few nits related to readability.

As for the approach, I feel that while a single regex gets too complex as we extend our support for more templating features, working with regex in layers could take us all the way as much as character operations would in a cleaner and less error prone way. But I'm fine with this implementation as well.

}
wdir := filepath.Dir(absPath)
vars := map[string]struct{}{}
err = substTemplateRec(rpt, substMap, vars, wdir)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good choice passing just the working directory and not the complete template path as we have resolved the template content already, and hence the template name may not be need here onwards. 👍

if len(matches) == 1 {
begin, end := matches[0][0], matches[0][1]
if begin == 0 && end == len(s) {
d, t, e := getValue(substMap, vars, getReplSpec(s, 0, len(s)), wd)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The single letter variables names aren't intuitive at all. Suggest expanding them at least a bit.

Placeholder string
Operation *string
Operand string
func getReplSpec(s string, begin, end int) string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Its intent wasn't intuitive until I looked up the code. Suggest renaming it for clarity, or else plucking out this one liner to the caller instead.

foxish added a commit that referenced this pull request Sep 2, 2026
…ot) (#366)

Resolves all 10 open [Dependabot
alerts](https://github.com/signadot/cli/security/dependabot) in the root
`go.mod`.

## google.golang.org/grpc v1.79.3 -> v1.83.1
- #43 (high): HTTP/2 DATA frame flood can cause out-of-memory
(GHSA-vp52-pcj8-j9qc)
- #40 (high): xDS RBAC / HTTP/2 vulnerability (<1.82.1)

## github.com/go-git/go-git/v5 v5.18.0 -> v5.19.2
- #42: symlink following
- #41: ref-name file modification
- #39: malformed object panics
- #38: `.git` directory modification
- #37: SSH quote escaping
- #34: object parsing

## github.com/go-git/go-billy/v5 v5.8.0 -> v5.9.0 (indirect)
- #36: path traversal
- #35: symlink cycle resource exhaustion

## Verification
- `go build ./...` passes
- `go test ./... -count=1`: all packages pass except
`internal/repoconfig`, whose 3 failures also occur at the base commit
with the old dependencies (they stem from running tests inside a linked
git worktree, not from this bump)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Fable 5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants