Skip to content

Commit

Permalink
add ascii macro post processor
Browse files Browse the repository at this point in the history
  • Loading branch information
sni committed Mar 11, 2024
1 parent 5cb236e commit de0470a
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ Support operators are:
| `: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) |
| `:ascii` | C:\ % -> C | remove any none-ascii characters |

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

Expand Down
1 change: 1 addition & 0 deletions pkg/snclient/macro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestMacros(t *testing.T) {
{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: "${characters | ascii }", Expect: "utf"},
{In: "$(seconds)$(seconds)", Expect: "130130"},
{In: "...'$(seconds)'...", Expect: "...'130'..."},
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/snclient/macros.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ var (
reRuntimeMacro = regexp.MustCompile(`%[` + runtimeMacroChars + `]+%|\$[` + runtimeMacroChars + `]+\$`)

reFloatFormat = regexp.MustCompile(`^%[.\d]*f$`)

reAsciiOnly = regexp.MustCompile(`\W`)
)

/* replaceMacros replaces variables in given string (config ini file style macros).
Expand Down Expand Up @@ -182,6 +184,8 @@ func replaceMacroOperators(value string, flags []string) string {
value = utils.DurationString(time.Duration(convert.Float64(value) * float64(time.Second)))
case "age":
value = fmt.Sprintf("%d", time.Now().Unix()-convert.Int64(value))
case "ascii":
value = reAsciiOnly.ReplaceAllString(value, "")
default:
value = replaceMacroOpString(value, flag)
}
Expand Down

0 comments on commit de0470a

Please sign in to comment.