Skip to content

Commit d2165bd

Browse files
committed
envsubst: skip on ! prefix
1 parent 6ade1ff commit d2165bd

2 files changed

Lines changed: 25 additions & 0 deletions

File tree

context_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package carapace
22

33
import (
4+
"fmt"
45
"os"
56
"path/filepath"
67
"strings"
@@ -101,6 +102,11 @@ func TestEnvsubst(t *testing.T) {
101102
t.Fail()
102103
}
103104

105+
if s, err := c.Envsubst("start${!example}end"); s != "start${example}end" || err != nil {
106+
fmt.Println(s)
107+
t.Fail()
108+
}
109+
104110
if s, err := c.Envsubst("start${example:-default}end"); s != "startdefaultend" || err != nil {
105111
t.Fail()
106112
}

third_party/github.com/drone/envsubst/parse/parse.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package parse
22

33
import (
44
"errors"
5+
"fmt"
6+
"strings"
57
)
68

79
var (
@@ -90,6 +92,8 @@ func (t *Tree) parseFunc() (Node, error) {
9092
// Turn on all escape characters
9193
t.scanner.escapeChars = escapeAll
9294
switch t.scanner.peek() {
95+
case '!':
96+
return t.parseIgnoredFunc()
9397
case '#':
9498
return t.parseLenFunc()
9599
}
@@ -355,6 +359,21 @@ func (t *Tree) parseCasingFunc(name string) (Node, error) {
355359
return node, t.consumeRbrack()
356360
}
357361

362+
// parses the ignored ${!...} function
363+
func (t *Tree) parseIgnoredFunc() (Node, error) {
364+
node := new(TextNode)
365+
366+
t.scanner.accept = acceptNotClosing
367+
t.scanner.mode = scanIdent
368+
switch t.scanner.scan() {
369+
case tokenIdent:
370+
node.Value = fmt.Sprintf("${%v}", strings.TrimPrefix(t.scanner.string(), "!"))
371+
default:
372+
return nil, ErrBadSubstitution
373+
}
374+
return node, t.consumeRbrack()
375+
}
376+
358377
// parses the ${#param} string function
359378
func (t *Tree) parseLenFunc() (Node, error) {
360379
node := new(FuncNode)

0 commit comments

Comments
 (0)