From de0470ade133232e1014d9012dd4fcb0b56925a6 Mon Sep 17 00:00:00 2001 From: Sven Nierlein Date: Mon, 11 Mar 2024 17:23:30 +0100 Subject: [PATCH] add ascii macro post processor --- docs/configuration/index.md | 1 + pkg/snclient/macro_test.go | 1 + pkg/snclient/macros.go | 4 ++++ 3 files changed, 6 insertions(+) diff --git a/docs/configuration/index.md b/docs/configuration/index.md index 51ff782..cdbda98 100644 --- a/docs/configuration/index.md +++ b/docs/configuration/index.md @@ -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=` | 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: diff --git a/pkg/snclient/macro_test.go b/pkg/snclient/macro_test.go index 81d419a..340152d 100644 --- a/pkg/snclient/macro_test.go +++ b/pkg/snclient/macro_test.go @@ -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'..."}, } diff --git a/pkg/snclient/macros.go b/pkg/snclient/macros.go index d7475d0..5b38dd1 100644 --- a/pkg/snclient/macros.go +++ b/pkg/snclient/macros.go @@ -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). @@ -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) }