/go-patbu

A Go library and CLI to manipulate paths

Primary LanguageGo

Patbu

Generalized path glob patterns that are also builders. The main idea is that an expression like

a/b/{x}.txt

can be interpreted as a pattern or a template at the same time, for example the following expressions have the following intuitive meaning

  • a/b/c.txt corresponds to the literal file a/b/c.txt

  • {year}-{month}-{day}.txt will match something like 2023-01-20.txt

  • a/{var1}/c.txt won't match a/b/b/c.txt because the capture {var1} doesn't match /, the equivalent is that this throws an error when used as a template and var1 contains slashes.

  • {*module}/package.json, here the * modifier will also match /.

so this syntax can be used to

  • match against a given pattern and return a dictionary of captures

  • but also as a path template or builder that is given a dictionary of bindings

This duality can be used to modify paths for example. Using the provided CLI for this library we can do something like the following

echo 'blog/src/projects/go/patbu.html' 
| patbu --stdin 'blog/src/{*route}/{page}.html' 'blog/dist/{*route}/{page}/index.html'

Setup & Install

$ go build -v -o ./bin/patbu ./cmd/patbu
$ go install -v ./cmd/patbu

Notes & Todos

Other name ideas: blueprint, pathbp, template, patt

Add some flags to use as a bulk rename utility