From a2fa1ef1b84a55c170f14b2748f87a1401545310 Mon Sep 17 00:00:00 2001 From: Takumasa Sakao Date: Sat, 24 Jul 2021 23:06:56 +0900 Subject: [PATCH] Fix environement variable substitution bug at plugin argument (#1198) --- internal/view/env.go | 7 +++++++ internal/view/env_test.go | 2 ++ 2 files changed, 9 insertions(+) diff --git a/internal/view/env.go b/internal/view/env.go index ef7e6266..29ecfeab 100644 --- a/internal/view/env.go +++ b/internal/view/env.go @@ -3,6 +3,7 @@ package view import ( "fmt" "regexp" + "sort" "strconv" "strings" ) @@ -20,6 +21,12 @@ func (e Env) Substitute(arg string) (string, error) { return arg, nil } + // To prevent the substitution starts with the shorter environment variable, + // sort with the length of the found environment variables. + sort.Slice(kk, func (i, j int) bool { + return len(kk[i]) > len(kk[j]) + }) + for _, k := range kk { key, inverse := k[1:], false if key[0] == '!' { diff --git a/internal/view/env_test.go b/internal/view/env_test.go index 1861c98e..d81e47c2 100644 --- a/internal/view/env_test.go +++ b/internal/view/env_test.go @@ -15,6 +15,7 @@ func TestEnvReplace(t *testing.T) { }{ "no-args": {arg: "blee blah", e: "blee blah"}, "simple": {arg: "$A", e: "10"}, + "substring": {arg: "$A and $AA", e: "10 and 20"}, "with-text": {arg: "Something $A", e: "Something 10"}, "noMatch": {arg: "blah blah and $BLEE", err: errors.New(`no environment matching key "$BLEE":"BLEE"`), e: ""}, "lower": {arg: "And then $b happened", e: "And then blee happened"}, @@ -27,6 +28,7 @@ func TestEnvReplace(t *testing.T) { e := Env{ "A": "10", + "AA": "20", "B": "blee", "COL0": "fred", "FRED": "fred",