Skip to content

Commit fce5b92

Browse files
committed
add template function findSubmatch
1 parent 964a41b commit fce5b92

File tree

3 files changed

+67
-17
lines changed

3 files changed

+67
-17
lines changed

README.md

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -534,23 +534,29 @@ Additional "magic" vairables:
534534
#### Functions
535535

536536
* [Sprig template utilities](http://masterminds.github.io/sprig/) available.
537-
* Custom functions:
538-
* `getRootFile` (v1.2.1+) - (`{{getRootFile "myfile"}}`) get content of file with specific name in any of root
539-
folders. Example:
540-
541-
Current working directory: /foo/bar/xyz
542-
Looking for name: .gitignore
543-
Will check (and return content):
544-
/foo/bar/xyz/.gitignore
545-
/foo/bar/.gitignore
546-
/foo/.gitignore
547-
/.gitignore
548-
549-
If nothing found - `ErrNotExists` returned
550-
* `findRootFile` (v1.3.2+) - (`{{findRootFile "myfile"}}`) find path to file with specific name in any of root
551-
folders. Same as `getRootFile` but instead of returning content it is returning path to file.
552-
* `findRootDir` (v1.3.2+) - (`{{findRootDir "mydir"}}`) find path to directory with specific name in any of root
553-
folders. Same as `findRootFile` but instead of looking for file it is looking for directory.
537+
* Custom functions:
538+
* `getRootFile` (v1.2.1+) - (`{{getRootFile "myfile"}}`) get content of file with specific name in any of root
539+
folders. Example:
540+
541+
Current working directory: /foo/bar/xyz
542+
Looking for name: .gitignore
543+
Will check (and return content):
544+
/foo/bar/xyz/.gitignore
545+
/foo/bar/.gitignore
546+
/foo/.gitignore
547+
/.gitignore
548+
549+
If nothing found - `ErrNotExists` returned
550+
* `findRootFile` (v1.3.2+) - (`{{findRootFile "myfile"}}`) find path to file with specific name in any of root
551+
folders. Same as `getRootFile` but instead of returning content it is returning path to file.
552+
* `findRootDir` (v1.3.2+) - (`{{findRootDir "mydir"}}`) find path to directory with specific name in any of root
553+
folders. Same as `findRootFile` but instead of looking for file it is looking for directory.
554+
* `findSubmatch` (v1.3.3+) - capture all regex groups with matching pattern. Example:
555+
556+
Pattern: foo[ ]+([^ ]+)
557+
Text: foo bar foo baz
558+
Returns: [bar, baz]
559+
554560

555561
#### Flow
556562

internal/manifest.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"io/ioutil"
2525
"os"
2626
"path/filepath"
27+
"regexp"
2728
"strings"
2829
"text/template"
2930

@@ -297,6 +298,7 @@ func (r *renderContext) Render(value string) (string, error) {
297298
funcMap["getRootFile"] = getRootFile(r.workDir)
298299
funcMap["findRootFile"] = findRootFile(r.workDir)
299300
funcMap["findRootDir"] = findRootDir(r.workDir)
301+
funcMap["findSubmatch"] = findSubmatchAll
300302
p, err := template.New("").Delims(r.open, r.close).Funcs(funcMap).Parse(value)
301303
if err != nil {
302304
return "", err
@@ -397,3 +399,22 @@ func findRootDir(root string) func(string) (string, error) {
397399
}
398400
}
399401
}
402+
403+
// find all regex groups which matches pattern.
404+
//
405+
// Pattern: foo[ ]+([^ ]+)
406+
// Text: foo bar foo baz
407+
// Returns: [bar, baz]
408+
//
409+
// Workaround for https://github.com/Masterminds/sprig/pull/298
410+
func findSubmatchAll(pattern string, text string) ([]string, error) {
411+
p, err := regexp.Compile(pattern)
412+
if err != nil {
413+
return nil, err
414+
}
415+
var ans []string
416+
for _, group := range p.FindAllStringSubmatch(text, -1) {
417+
ans = append(ans, group[1:]...)
418+
}
419+
return ans, nil
420+
}

internal/manifest_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package internal
1818

1919
import (
20+
"fmt"
2021
"os"
2122
"path/filepath"
2223
"testing"
@@ -64,3 +65,25 @@ func TestGetRootFile(t *testing.T) {
6465
require.Contains(t, content, "module github.com/reddec/layout")
6566
})
6667
}
68+
69+
func TestSubmatch(t *testing.T) {
70+
content := `
71+
foo bar
72+
73+
foo barbaz
74+
bar=123,456
75+
`
76+
t.Run("match groups", func(t *testing.T) {
77+
matches, err := findSubmatchAll(`foo[ ]+(.+)`, content)
78+
require.NoError(t, err)
79+
require.Equal(t, []string{"bar", "barbaz"}, matches)
80+
})
81+
}
82+
83+
func ExampleFindSubmatch() {
84+
pattern := `foo[ ]+([^ ]+)`
85+
text := `foo bar foo baz`
86+
out, _ := findSubmatchAll(pattern, text)
87+
fmt.Println(out)
88+
// output: [bar baz]
89+
}

0 commit comments

Comments
 (0)