mirror of
https://github.com/mikefarah/yq.git
synced 2026-07-10 16:55:40 +00:00
Add internal quoting style switch to @sh
This commit is contained in:
parent
c2f7e0d82c
commit
8ebc0e5801
@ -12,10 +12,11 @@ import (
|
|||||||
var unsafeChars = regexp.MustCompile(`[^\w@%+=:,./-]`)
|
var unsafeChars = regexp.MustCompile(`[^\w@%+=:,./-]`)
|
||||||
|
|
||||||
type shEncoder struct {
|
type shEncoder struct {
|
||||||
|
quoteAll bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewShEncoder() Encoder {
|
func NewShEncoder() Encoder {
|
||||||
return &shEncoder{}
|
return &shEncoder{false}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *shEncoder) CanHandleAliases() bool {
|
func (e *shEncoder) CanHandleAliases() bool {
|
||||||
@ -39,7 +40,7 @@ func (e *shEncoder) Encode(writer io.Writer, originalNode *yaml.Node) error {
|
|||||||
return writeString(writer, e.encode(originalNode.Value))
|
return writeString(writer, e.encode(originalNode.Value))
|
||||||
}
|
}
|
||||||
|
|
||||||
// put any shell-unsafe characters into a single-quoted block, close the block lazily
|
// put any (shell-unsafe) characters into a single-quoted block, close the block lazily
|
||||||
func (e *shEncoder) encode(input string) string {
|
func (e *shEncoder) encode(input string) string {
|
||||||
const quote = '\''
|
const quote = '\''
|
||||||
var inQuoteBlock = false
|
var inQuoteBlock = false
|
||||||
@ -57,8 +58,8 @@ func (e *shEncoder) encode(input string) string {
|
|||||||
// escape the quote with a backslash
|
// escape the quote with a backslash
|
||||||
encoded.WriteRune('\\')
|
encoded.WriteRune('\\')
|
||||||
} else {
|
} else {
|
||||||
if unsafeChars.MatchString(string(ir)) && !inQuoteBlock {
|
if e.shouldQuote(ir) && !inQuoteBlock {
|
||||||
// start a quote block on an unsafe character
|
// start a quote block for any (unsafe) characters
|
||||||
encoded.WriteRune(quote)
|
encoded.WriteRune(quote)
|
||||||
inQuoteBlock = !inQuoteBlock
|
inQuoteBlock = !inQuoteBlock
|
||||||
}
|
}
|
||||||
@ -72,3 +73,7 @@ func (e *shEncoder) encode(input string) string {
|
|||||||
}
|
}
|
||||||
return encoded.String()
|
return encoded.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (e *shEncoder) shouldQuote(ir rune) bool {
|
||||||
|
return e.quoteAll || unsafeChars.MatchString(string(ir))
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user