Skip to content

Commit

Permalink
regexFirstGroup return first group from regexp, useful with .env file… (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
alexandregz authored Jun 8, 2024
1 parent 6a8fc96 commit a6451d2
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
25 changes: 25 additions & 0 deletions docs/gossfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,31 @@ Available functions:
`toUpper`
: Changes piped input to UPPERCASE


Check failure on line 921 in docs/gossfile.md

View workflow job for this annotation

GitHub Actions / Lint Documentation

Trailing spaces

docs/gossfile.md:921:1 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 7] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md

Check failure on line 921 in docs/gossfile.md

View workflow job for this annotation

GitHub Actions / Lint Documentation

Multiple consecutive blank lines

docs/gossfile.md:921 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md012.md
`findStringSubmatch regex string`
: Returns map[string]interface{} with the names of the parenthesized subexpressions, like `(?P<first>[a-z])`

Check failure on line 924 in docs/gossfile.md

View workflow job for this annotation

GitHub Actions / Lint Documentation

Trailing spaces

docs/gossfile.md:924:1 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 8] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md
{{ $regexDBrc := "\\'mysql:\\/\\/(?P<login>[a-z0-9]+):(?P<password>[a-z0-9]+)@localhost\\/(?P<database>roundcube_[a-z0-9]+)\\';"}}

{{ $rcConf := readFile /home/user/roundcube/config.inc.php | findStringSubmatch $regexDBrc }}
{{ $UserDBrc := get $rcConf "login" }}
{{ $PassDBrc := get $rcConf "password" }}
{{ $DBrc := get $rcConf "database" }}

If not exists named parenthesized subexps, returns stringfied array string:

{{ $regexDBrc := "\\'mysql:\\/\\/([a-z0-9]+):([a-z0-9]+)@localhost\\/(roundcube_[a-z0-9]+)\\';"}}

{{ $rcConf := readFile /home/user/roundcube/config.inc.php | findStringSubmatch $regexDBrc }}
{{ $UserDBrc := get $rcConf "1" }}
{{ $PassDBrc := get $rcConf "2" }}
{{ $DBrc := get $rcConf "3" }}

NOTE: stringfied string array begins with "1" ("0" is all the string matched)


Check failure on line 943 in docs/gossfile.md

View workflow job for this annotation

GitHub Actions / Lint Documentation

Trailing spaces

docs/gossfile.md:943:1 MD009/no-trailing-spaces Trailing spaces [Expected: 0 or 2; Actual: 8] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md009.md

Check failure on line 943 in docs/gossfile.md

View workflow job for this annotation

GitHub Actions / Lint Documentation

Multiple consecutive blank lines

docs/gossfile.md:943 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 2] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md012.md

Check failure on line 944 in docs/gossfile.md

View workflow job for this annotation

GitHub Actions / Lint Documentation

Multiple consecutive blank lines

docs/gossfile.md:944 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 3] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md012.md

Check failure on line 945 in docs/gossfile.md

View workflow job for this annotation

GitHub Actions / Lint Documentation

Multiple consecutive blank lines

docs/gossfile.md:945 MD012/no-multiple-blanks Multiple consecutive blank lines [Expected: 1; Actual: 4] https://github.com/DavidAnson/markdownlint/blob/v0.34.0/doc/md012.md
!!! warning

gossfiles containing text/template `{{}}` controls will no longer work with `goss add/autoadd`.
Expand Down
39 changes: 33 additions & 6 deletions template.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"regexp"
"strconv"
"strings"
"text/template"

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

// return named parenthesized subexpresions, if received, or stringfied (Sprig "get" need strings) keys like array
func findStringSubmatch(pattern, input string) map[string]interface{} {
re := regexp.MustCompile(pattern)
els := re.FindStringSubmatch(input)

elsMap := make(map[string]interface{})
elsMapNamed := make(map[string]interface{})

// create always elsMaps but returns elsMapNamed if exists named parenthesized subexps
for i := 0; i < len(els); i++ {
// convert i to string according returned (https://github.com/goss-org/goss/pull/895#issuecomment-2075716706)
elsMap[strconv.Itoa(i)] = els[i]

if re.SubexpNames()[i] != "" {
elsMapNamed[re.SubexpNames()[i]] = els[i]
}
}

// returns elsMapNamed if exists named parenthesized subexps
if len(elsMapNamed) > 0 {
return elsMapNamed
}
return elsMap
}

var funcMap = template.FuncMap{
"mkSlice": mkSlice,
"readFile": readFile,
"getEnv": getEnv,
"regexMatch": regexMatch,
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"mkSlice": mkSlice,
"readFile": readFile,
"getEnv": getEnv,
"regexMatch": regexMatch,
"toUpper": strings.ToUpper,
"toLower": strings.ToLower,
"findStringSubmatch": findStringSubmatch,
}

0 comments on commit a6451d2

Please sign in to comment.