Fix environement variable substitution bug at plugin argument (#1198)

mine
Takumasa Sakao 2021-07-24 23:06:56 +09:00 committed by GitHub
parent 7bf85a6ba3
commit a2fa1ef1b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 0 deletions

View File

@ -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] == '!' {

View File

@ -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",