@@ -123,23 +123,13 @@ func Parse(b []byte) ([]instructions.Stage, []instructions.ArgCommand, error) {
123123// stripEnclosingQuotes removes quotes enclosing the value of each instructions.ArgCommand in a slice
124124// if the quotes are escaped it leaves them
125125func stripEnclosingQuotes (metaArgs []instructions.ArgCommand ) ([]instructions.ArgCommand , error ) {
126- dbl := byte ('"' )
127- sgl := byte ('\'' )
128-
129126 for i := range metaArgs {
130127 arg := metaArgs [i ]
131128 v := arg .Value
132129 if v != nil {
133- val := * v
134- fmt .Printf ("val %s\n " , val )
135- if (val [0 ] == dbl && val [len (val )- 1 ] == dbl ) || (val [0 ] == sgl && val [len (val )- 1 ] == sgl ) {
136- val = val [1 : len (val )- 1 ]
137- } else if val [:2 ] == `\"` && val [len (val )- 2 :] == `\"` {
138- continue
139- } else if val [:2 ] == `\'` && val [len (val )- 2 :] == `\'` {
140- continue
141- } else if val [0 ] == dbl || val [0 ] == sgl || val [len (val )- 1 ] == dbl || val [len (val )- 1 ] == sgl {
142- return nil , errors .New ("quotes wrapping arg values must be matched" )
130+ val , err := extractValFromQuotes (* v )
131+ if err != nil {
132+ return nil , err
143133 }
144134
145135 arg .Value = & val
@@ -149,6 +139,55 @@ func stripEnclosingQuotes(metaArgs []instructions.ArgCommand) ([]instructions.Ar
149139 return metaArgs , nil
150140}
151141
142+ func extractValFromQuotes (val string ) (string , error ) {
143+ backSlash := byte ('\\' )
144+ if len (val ) < 2 {
145+ return val , nil
146+ }
147+
148+ var leader string
149+ var tail string
150+
151+ switch char := val [0 ]; char {
152+ case '\'' , '"' :
153+ leader = string ([]byte {char })
154+ case backSlash :
155+ switch char := val [1 ]; char {
156+ case '\'' , '"' :
157+ leader = string ([]byte {backSlash , char })
158+ }
159+ }
160+
161+ // If the length of leader is greater than one then it must be an escaped
162+ // character.
163+ if len (leader ) < 2 {
164+ switch char := val [len (val )- 1 ]; char {
165+ case '\'' , '"' :
166+ tail = string ([]byte {char })
167+ }
168+ } else {
169+ switch char := val [len (val )- 2 :]; char {
170+ case `\'` , `\"` :
171+ tail = char
172+ }
173+ }
174+
175+ if leader != tail {
176+ logrus .Infof ("leader %s tail %s" , leader , tail )
177+ return "" , errors .New ("quotes wrapping arg values must be matched" )
178+ }
179+
180+ if leader == "" {
181+ return val , nil
182+ }
183+
184+ if len (leader ) == 2 {
185+ return val , nil
186+ }
187+
188+ return val [1 : len (val )- 1 ], nil
189+ }
190+
152191// targetStage returns the index of the target stage kaniko is trying to build
153192func targetStage (stages []instructions.Stage , target string ) (int , error ) {
154193 if target == "" {
0 commit comments