@@ -2,6 +2,7 @@ package expression
22
33import (
44 "bytes"
5+ "strings"
56 "text/template"
67
78 "github.com/drone/go-task/task/common"
@@ -27,11 +28,77 @@ func (r *TemplateResolver) Resolve(data []byte) ([]byte, error) {
2728 secretMap [secret .ID ] = secret .Value
2829 }
2930
31+ // Extract and process template expressions
32+ input := string (data )
33+ start := 0
34+ processed := ""
35+
36+ for {
37+ // Find start of template expression
38+ startIdx := strings .Index (input [start :], RESOLVER_DELIM_START )
39+ if startIdx == - 1 {
40+ // No more template expressions found
41+ processed += input [start :]
42+ break
43+ }
44+ startIdx += start
45+
46+ // Find end of template expression
47+ endIdx := strings .Index (input [startIdx :], RESOLVER_DELIM_END )
48+ if endIdx == - 1 {
49+ // No matching end delimiter found
50+ processed += input [start :]
51+ break
52+ }
53+ endIdx += startIdx + len (RESOLVER_DELIM_END )
54+
55+ // Add everything before the template expression
56+ processed += input [start :startIdx ]
57+
58+ // Get the template expression content
59+ expr := input [startIdx :endIdx ]
60+ if strings .Contains (expr , "getAsBase64" ) {
61+ // Extract content between delimiters
62+ content := expr [len (RESOLVER_DELIM_START ) : len (expr )- len (RESOLVER_DELIM_END )]
63+ // Split by pipe operator
64+ parts := strings .Split (content , "|" )
65+ if len (parts ) > 0 {
66+ // Get the quoted string part and trim spaces
67+ quotedStr := strings .TrimSpace (parts [0 ])
68+
69+ // Handle the quoted string
70+ var unescaped string
71+
72+ // handl case use input contains quotes i.e. "abcd"
73+ if strings .HasPrefix (quotedStr , "\\ \" " ) && strings .HasSuffix (quotedStr , "\\ \" " ) {
74+ // Remove only first and last escaped quotes
75+ unescaped = "\" " + quotedStr [2 :len (quotedStr )- 2 ] + "\" "
76+ } else if strings .HasPrefix (quotedStr , "\" " ) && strings .HasSuffix (quotedStr , "\" " ) {
77+ unescaped = quotedStr
78+ }else {
79+ unescaped = "\" " + quotedStr + "\" "
80+ }
81+
82+ // Reconstruct the expression
83+ expr = RESOLVER_DELIM_START + " " + unescaped
84+ if len (parts ) > 1 {
85+ expr += " |" + parts [1 ]
86+ }
87+ expr += RESOLVER_DELIM_END
88+ }
89+ }
90+ processed += expr
91+
92+ // Move start to after this template expression
93+ start = endIdx
94+ }
95+
3096 // Parse the template with custom delimiters
3197 tmpl , err := template .New ("resolver" ).
3298 Delims (RESOLVER_DELIM_START , RESOLVER_DELIM_END ).
3399 Funcs (TemplateFunctions ()). // Include template functions
34- Parse (string (data ))
100+ Parse (processed )
101+
35102 if err != nil {
36103 return nil , err
37104 }
@@ -42,6 +109,5 @@ func (r *TemplateResolver) Resolve(data []byte) ([]byte, error) {
42109 if err != nil {
43110 return nil , err
44111 }
45-
46112 return buf .Bytes (), nil
47113}
0 commit comments