goss-org / goss

Quick and Easy server testing/validation
https://goss.rocks
Apache License 2.0
5.55k stars 473 forks source link

please implement regexReplace in Templates #314

Closed themr0c closed 4 years ago

themr0c commented 6 years ago

Hello, here is the user story i would like to see in goss:

I would like to check the presence of some file, which is named with a pattern that can be calculated with a regex replacement from a variable that i already have.

For example I want to check the presence of a file /home/{{$prefixname}}/user/example, and $prefixname can be calculated from some variable (that i can get from readFile or from other means).

For the example lets say i can define $hostname:

{{ $hostname := readFile "/etc/hostname" }}

Now i would like to use this variable to calculate the content of the new variable $prefixname, with a regex replacement.

Some implementation one could think of would be something like following, except the fact that the strings package is not available:

{{ $prefixname := strings.Replace($hostname, "-sftp01", "", 1) }}

Is there already a way to achieve that? Is it something you would implement?

stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

aelsabbahy commented 4 years ago

The addition of sprig made this possible #553

Here's an example where "foo.txt" contains the string "foo"

{{ $fcontent := readFile "/tmp/foo.txt" }}
{{ $prefixname := regexReplaceAll "oo$" $fcontent "" }}

matching:
  test:
    content: {{ $prefixname }}
    matches: "f"
themr0c commented 4 years ago

Thanks!