Skip to content

Commit

Permalink
add regexp replacement macro post processor
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Mar 11, 2024
1 parent a7367ba commit 45c5256
Show file tree
Hide file tree
Showing 7 changed files with 396 additions and 205 deletions.
1 change: 1 addition & 0 deletions Changes
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ next:
- check_service: fix json error
- update windows build framework to wix 3.14
- improve wmi stability
- add regexp replacement macro post processor

0.19 Wed Feb 28 00:09:39 CET 2024
- write startup errors to default logfile
Expand Down
21 changes: 11 additions & 10 deletions docs/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,17 @@ Macro values can be altered by adding a colon separated suffix.

Support operators are:

| Suffix | Example | Description |
| ------------ | ------------------------------------- | ------------|
| `:lc` | Word -> word | make value lowercase
| `:uc` | test -> TEST | make value uppercase
| `:h` | 1000 -> 1k | make a number more human readably
| `:duration` | 125 -> 2m 5s | convert amount of seconds into human readable duration
| `:age` | 1700834034 -> 374 | substract value from current unix timestamp to get the age in seconds
| `:date` | 1700834034 -> 2023-11-24 14:53:54 CET | convert unix timestamp into human readable date (local timezone)
| `:utc` | 1700834034 -> 2023-11-24 13:53:54 UTC | convert unix timestamp into human readable date (utc timezone)
| `:fmt=<fmt>` | 123.45 -> 123.4 | apply format, ex.: $(total | fmt=%.1f) (using GOs fmt.Sprintf)
| Suffix | Example | Description |
| --------------------- | ------------------------------------- | ------------|
| `:lc` | Word -> word | make value lowercase |
| `:uc` | test -> TEST | make value uppercase |
| `:h` | 1000 -> 1k | make a number more human readably |
| `:duration` | 125 -> 2m 5s | convert amount of seconds into human readable duration |
| `:age` | 1700834034 -> 374 | substract value from current unix timestamp to get the age in seconds |
| `:date` | 1700834034 -> 2023-11-24 14:53:54 CET | convert unix timestamp into human readable date (local timezone) |
| `:utc` | 1700834034 -> 2023-11-24 13:53:54 UTC | convert unix timestamp into human readable date (utc timezone) |
| `:fmt=<fmt>` | 123.45 -> 123.4 | apply format, ex.: $(total \| fmt=%.1f) (using GOs fmt.Sprintf) |
| : s/regexp/replace/` | C:\ % -> C | apply regexp replacement, ex.: $(name \| 's/\\W//' ) (using GOs regexp.Compile) |

for example, define a dummy command which prints the hostname in lower case letters:

Expand Down
2 changes: 1 addition & 1 deletion pkg/snclient/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ func (config *Config) SectionNamesSorted() []string {
* %(/settings/section/attribute)
*/
func (config *Config) ReplaceOnDemandConfigMacros(value string) string {
value = reMacro.ReplaceAllStringFunc(value, func(macro string) string {
value = reOnDemandMacro.ReplaceAllStringFunc(value, func(macro string) string {
orig := macro
macro = extractMacroString(macro)

Expand Down
34 changes: 0 additions & 34 deletions pkg/snclient/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"runtime"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -351,39 +350,6 @@ func TestEmptyConfig(t *testing.T) {
require.NoErrorf(t, err, "empty ini parsed")
}

func TestMacros(t *testing.T) {
macros := map[string]string{
"seconds": "130",
"unix time": "1700834034",
"float": "170.05",
"yesterday": fmt.Sprintf("%d", time.Now().Add(-24*time.Hour).Unix()),
}

tests := []struct {
In string
Expect string
}{
{In: "$(seconds)", Expect: "130"},
{In: "-${seconds}-", Expect: "-130-"},
{In: "$(seconds:duration)", Expect: "2m 10s"},
{In: "$(unix time:utc)", Expect: "2023-11-24 13:53:54 UTC"},
{In: "$(unix time:utc:uc)", Expect: "2023-11-24 13:53:54 UTC"},
{In: "$(unix time:utc:lc)", Expect: "2023-11-24 13:53:54 utc"},
{In: "$(unix time : utc : lc)", Expect: "2023-11-24 13:53:54 utc"},
{In: "$(unix time | utc | lc)", Expect: "2023-11-24 13:53:54 utc"},
{In: "$(something else | utc | lc)", Expect: "$(something else | utc | lc)"},
{In: "$(float | fmt=%d)", Expect: "170"},
{In: "$(float | fmt=%.1f)", Expect: "170.1"},
{In: "$(float | fmt=%.2f)", Expect: "170.05"},
{In: "$(yesterday | age | duration)", Expect: "1d 00:00h"},
}

for _, tst := range tests {
res := ReplaceMacros(tst.In, macros)
assert.Equalf(t, tst.Expect, res, "replacing: %s", tst.In)
}
}

func TestConfigAppend(t *testing.T) {
testDir, _ := os.Getwd()
pkgDir := filepath.Join(testDir, "t", "configs")
Expand Down
74 changes: 74 additions & 0 deletions pkg/snclient/macro_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package snclient

import (
"fmt"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestMacros(t *testing.T) {
macros := map[string]string{
"seconds": "130",
"unix time": "1700834034",
"float": "170.05",
"yesterday": fmt.Sprintf("%d", time.Now().Add(-24*time.Hour).Unix()),
"characters": `öäüß@€utf`,
}

tests := []struct {
In string
Expect string
}{
{In: "$(seconds)", Expect: "130"},
{In: "-${seconds}-", Expect: "-130-"},
{In: "$(seconds:duration)", Expect: "2m 10s"},
{In: "$(unix time:utc)", Expect: "2023-11-24 13:53:54 UTC"},
{In: "$(unix time:utc:uc)", Expect: "2023-11-24 13:53:54 UTC"},
{In: "$(unix time:utc:lc)", Expect: "2023-11-24 13:53:54 utc"},
{In: "$(unix time : utc : lc)", Expect: "2023-11-24 13:53:54 utc"},
{In: "$(unix time | utc | lc)", Expect: "2023-11-24 13:53:54 utc"},
{In: "$(something else | utc | lc)", Expect: "$(something else | utc | lc)"},
{In: "$(float | fmt=%d)", Expect: "170"},
{In: "$(float | fmt=%.1f)", Expect: "170.1"},
{In: "$(float | fmt=%.2f)", Expect: "170.05"},
{In: "$(yesterday | age | duration)", Expect: "1d 00:00h"},
{In: "$(characters | s/[^a-zA-Z]//g )", Expect: "utf"},
{In: "$(characters | 's/^(.*?)([a-z]+)$/$2$1/g' )", Expect: "utföäüß@€"},
{In: "${characters | 's/^(.*)ß.*?([a-z]+)/$2$1/g' }", Expect: "utföäü"},
{In: "${characters | 's/.*(u{1}).*/U/g' }", Expect: "U"},
{In: "${characters | 's/\\W//' }", Expect: "utf"},
{In: "$(seconds)$(seconds)", Expect: "130130"},
{In: "...'$(seconds)'...", Expect: "...'130'..."},
}

for i, tst := range tests {
res := ReplaceMacros(tst.In, macros)
assert.Equalf(t, tst.Expect, res, "[%d] replacing: %s", i, tst.In)
}
}

func TestMacroToken(t *testing.T) {
tests := []struct {
In string
Expect []string
}{
{In: "...'$(seconds)'...", Expect: []string{"...'", "$(seconds)", "'..."}},
{In: "... $(seconds | 's/a/b/' ) ...", Expect: []string{"... ", "$(seconds | 's/a/b/' )", " ..."}},
{In: "... $(var | 's/\\)//' ) ...", Expect: []string{"... ", "$(var | 's/\\)//' )", " ..."}},
}

splitBy := map[string]string{
"$(": ")",
"${": "}",
"%(": ")",
"%{": "}",
}
for i, tst := range tests {
token, err := splitToken(tst.In, splitBy)
require.NoErrorf(t, err, "[%d] text: %s", i, tst.In)
assert.Equalf(t, tst.Expect, token, "replacing: %s", tst.In)
}
}
Loading

0 comments on commit 45c5256

Please sign in to comment.