8
8
"crypto/sha512"
9
9
"crypto/tls"
10
10
"encoding/hex"
11
+ "errors"
11
12
"fmt"
12
13
"hash"
13
14
"io"
@@ -27,6 +28,7 @@ import (
27
28
28
29
"github.com/consol-monitoring/snclient/pkg/convert"
29
30
"github.com/kdar/factorlog"
31
+ st "github.com/sni/shelltoken"
30
32
)
31
33
32
34
var reMountPassword = regexp .MustCompile (`//.*:.*@` )
@@ -298,22 +300,25 @@ func TrimQuotesList(list []string) (res []string, err error) {
298
300
// TrimQuotes returns string with quotes removed
299
301
func TrimQuotes (str string ) (res string , err error ) {
300
302
switch {
301
- case strings .HasPrefix (str , "'" ):
302
- if ! strings .HasSuffix (str , "'" ) || len (str ) == 1 {
303
- return "" , fmt .Errorf ("unbalanced quotes in '%s'" , str )
303
+ case strings .HasPrefix (str , "'" ),
304
+ strings .HasSuffix (str , "'" ),
305
+ strings .HasPrefix (str , `"` ),
306
+ strings .HasSuffix (str , `"` ):
307
+ _ , err = st .SplitQuotes (str , st .Whitespace , st .SplitKeepAll )
308
+ if err != nil && errors .Is (err , & st.UnbalancedQuotesError {}) {
309
+ return str , fmt .Errorf ("unbalanced quotes in '%s'" , str )
304
310
}
305
311
str = strings .TrimPrefix (str , "'" )
306
312
str = strings .TrimSuffix (str , "'" )
307
- case strings .HasPrefix (str , `"` ):
308
- if ! strings .HasSuffix (str , `"` ) || len (str ) == 1 {
309
- return "" , fmt .Errorf ("unbalanced quotes in '%s'" , str )
310
- }
313
+ }
314
+
315
+ switch {
316
+ case strings .HasPrefix (str , "'" ) && strings .HasSuffix (str , "'" ) && len (str ) >= 2 :
317
+ str = strings .TrimPrefix (str , "'" )
318
+ str = strings .TrimSuffix (str , "'" )
319
+ case strings .HasPrefix (str , `"` ) && strings .HasSuffix (str , `"` ) && len (str ) >= 2 :
311
320
str = strings .TrimPrefix (str , `"` )
312
321
str = strings .TrimSuffix (str , `"` )
313
- case strings .HasSuffix (str , "'" ):
314
- return "" , fmt .Errorf ("unbalanced quotes in '%s'" , str )
315
- case strings .HasSuffix (str , `"` ):
316
- return "" , fmt .Errorf ("unbalanced quotes in '%s'" , str )
317
322
}
318
323
319
324
return str , nil
0 commit comments