Skip to content

Commit a6451d2

Browse files
authored
regexFirstGroup return first group from regexp, useful with .env file… (#895)
* regexFirstGroup return first group from regexp, useful with .env files and another config files * documentation about regexFirstGroup and example * modify func regexFirstGroup to findStringSubmatch, return all the submatchs from regex * func findStringSubmatch(), returns map[string]interface{} for use with get and named parenthesized subexpressions or stringfied array string
1 parent 6a8fc96 commit a6451d2

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

docs/gossfile.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,31 @@ Available functions:
918918
`toUpper`
919919
: Changes piped input to UPPERCASE
920920

921+
922+
`findStringSubmatch regex string`
923+
: Returns map[string]interface{} with the names of the parenthesized subexpressions, like `(?P<first>[a-z])`
924+
925+
{{ $regexDBrc := "\\'mysql:\\/\\/(?P<login>[a-z0-9]+):(?P<password>[a-z0-9]+)@localhost\\/(?P<database>roundcube_[a-z0-9]+)\\';"}}
926+
927+
{{ $rcConf := readFile /home/user/roundcube/config.inc.php | findStringSubmatch $regexDBrc }}
928+
{{ $UserDBrc := get $rcConf "login" }}
929+
{{ $PassDBrc := get $rcConf "password" }}
930+
{{ $DBrc := get $rcConf "database" }}
931+
932+
If not exists named parenthesized subexps, returns stringfied array string:
933+
934+
{{ $regexDBrc := "\\'mysql:\\/\\/([a-z0-9]+):([a-z0-9]+)@localhost\\/(roundcube_[a-z0-9]+)\\';"}}
935+
936+
{{ $rcConf := readFile /home/user/roundcube/config.inc.php | findStringSubmatch $regexDBrc }}
937+
{{ $UserDBrc := get $rcConf "1" }}
938+
{{ $PassDBrc := get $rcConf "2" }}
939+
{{ $DBrc := get $rcConf "3" }}
940+
941+
NOTE: stringfied string array begins with "1" ("0" is all the string matched)
942+
943+
944+
945+
921946
!!! warning
922947

923948
gossfiles containing text/template `{{}}` controls will no longer work with `goss add/autoadd`.

template.go

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"os"
77
"regexp"
8+
"strconv"
89
"strings"
910
"text/template"
1011

@@ -76,11 +77,37 @@ func regexMatch(re, s string) (bool, error) {
7677
return compiled.MatchString(s), nil
7778
}
7879

80+
// return named parenthesized subexpresions, if received, or stringfied (Sprig "get" need strings) keys like array
81+
func findStringSubmatch(pattern, input string) map[string]interface{} {
82+
re := regexp.MustCompile(pattern)
83+
els := re.FindStringSubmatch(input)
84+
85+
elsMap := make(map[string]interface{})
86+
elsMapNamed := make(map[string]interface{})
87+
88+
// create always elsMaps but returns elsMapNamed if exists named parenthesized subexps
89+
for i := 0; i < len(els); i++ {
90+
// convert i to string according returned (https://github.com/goss-org/goss/pull/895#issuecomment-2075716706)
91+
elsMap[strconv.Itoa(i)] = els[i]
92+
93+
if re.SubexpNames()[i] != "" {
94+
elsMapNamed[re.SubexpNames()[i]] = els[i]
95+
}
96+
}
97+
98+
// returns elsMapNamed if exists named parenthesized subexps
99+
if len(elsMapNamed) > 0 {
100+
return elsMapNamed
101+
}
102+
return elsMap
103+
}
104+
79105
var funcMap = template.FuncMap{
80-
"mkSlice": mkSlice,
81-
"readFile": readFile,
82-
"getEnv": getEnv,
83-
"regexMatch": regexMatch,
84-
"toUpper": strings.ToUpper,
85-
"toLower": strings.ToLower,
106+
"mkSlice": mkSlice,
107+
"readFile": readFile,
108+
"getEnv": getEnv,
109+
"regexMatch": regexMatch,
110+
"toUpper": strings.ToUpper,
111+
"toLower": strings.ToLower,
112+
"findStringSubmatch": findStringSubmatch,
86113
}

0 commit comments

Comments
 (0)