Added shell string encoder (@sh) #1526

This commit is contained in:
Mike Farah 2023-02-02 12:22:52 +11:00
parent d119dbc239
commit d21bb920d6
7 changed files with 75 additions and 0 deletions

View File

@ -17,6 +17,7 @@ These operators are useful to process yaml documents that have stringified embed
| XML | from_xml/@xmld | to_xml(i)/@xml | | XML | from_xml/@xmld | to_xml(i)/@xml |
| Base64 | @base64d | @base64 | | Base64 | @base64d | @base64 |
| URI | @urid | @uri | | URI | @urid | @uri |
| Shell | | @sh |
See CSV and TSV [documentation](https://mikefarah.gitbook.io/yq/usage/csv-tsv) for accepted formats. See CSV and TSV [documentation](https://mikefarah.gitbook.io/yq/usage/csv-tsv) for accepted formats.
@ -464,6 +465,22 @@ will output
this has & special () characters * this has & special () characters *
``` ```
## Encode a string to sh
Sh/Bash friendly string
Given a sample.yml file of:
```yaml
coolData: strings with spaces and a 'quote'
```
then
```bash
yq '.coolData | @sh' sample.yml
```
will output
```yaml
'strings with spaces and a \'quote\''
```
## Decode a base64 encoded string ## Decode a base64 encoded string
Decoded data is assumed to be a string. Decoded data is assumed to be a string.

View File

@ -17,6 +17,7 @@ These operators are useful to process yaml documents that have stringified embed
| XML | from_xml/@xmld | to_xml(i)/@xml | | XML | from_xml/@xmld | to_xml(i)/@xml |
| Base64 | @base64d | @base64 | | Base64 | @base64d | @base64 |
| URI | @urid | @uri | | URI | @urid | @uri |
| Shell | | @sh |
See CSV and TSV [documentation](https://mikefarah.gitbook.io/yq/usage/csv-tsv) for accepted formats. See CSV and TSV [documentation](https://mikefarah.gitbook.io/yq/usage/csv-tsv) for accepted formats.

44
pkg/yqlib/encoder_sh.go Normal file
View File

@ -0,0 +1,44 @@
package yqlib
import (
"fmt"
"io"
"regexp"
"strings"
yaml "gopkg.in/yaml.v3"
)
var pattern = regexp.MustCompile(`[^\w@%+=:,./-]`)
type shEncoder struct {
}
func NewShEncoder() Encoder {
return &shEncoder{}
}
func (e *shEncoder) CanHandleAliases() bool {
return false
}
func (e *shEncoder) PrintDocumentSeparator(writer io.Writer) error {
return nil
}
func (e *shEncoder) PrintLeadingContent(writer io.Writer, content string) error {
return nil
}
func (e *shEncoder) Encode(writer io.Writer, originalNode *yaml.Node) error {
node := unwrapDoc(originalNode)
if guessTagFromCustomType(node) != "!!str" {
return fmt.Errorf("cannot encode %v as URI, can only operate on strings. Please first pipe through another encoding operator to convert the value to a string", node.Tag)
}
value := originalNode.Value
if pattern.MatchString(value) {
value = "'" + strings.ReplaceAll(value, "'", "\\'") + "'"
}
return writeString(writer, value)
}

View File

@ -80,6 +80,7 @@ var participleYqRules = []*participleYqRule{
{"Urid", `@urid`, decodeOp(UriInputFormat), 0}, {"Urid", `@urid`, decodeOp(UriInputFormat), 0},
{"Uri", `@uri`, encodeWithIndent(UriOutputFormat, 0), 0}, {"Uri", `@uri`, encodeWithIndent(UriOutputFormat, 0), 0},
{"SH", `@sh`, encodeWithIndent(ShOutputFormat, 0), 0},
{"LoadXML", `load_?xml|xml_?load`, loadOp(NewXMLDecoder(ConfiguredXMLPreferences), false), 0}, {"LoadXML", `load_?xml|xml_?load`, loadOp(NewXMLDecoder(ConfiguredXMLPreferences), false), 0},

View File

@ -28,6 +28,8 @@ func configureEncoder(format PrinterOutputFormat, indent int) Encoder {
return NewBase64Encoder() return NewBase64Encoder()
case UriOutputFormat: case UriOutputFormat:
return NewUriEncoder() return NewUriEncoder()
case ShOutputFormat:
return NewShEncoder()
} }
panic("invalid encoder") panic("invalid encoder")
} }

View File

@ -257,6 +257,15 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
"D0, P[], (!!str)::this has & special () characters *\n", "D0, P[], (!!str)::this has & special () characters *\n",
}, },
}, },
{
description: "Encode a string to sh",
subdescription: "Sh/Bash friendly string",
document: "coolData: strings with spaces and a 'quote'",
expression: ".coolData | @sh",
expected: []string{
"D0, P[coolData], (!!str)::'strings with spaces and a \\'quote\\''\n",
},
},
{ {
description: "Decode a base64 encoded string", description: "Decode a base64 encoded string",
subdescription: "Decoded data is assumed to be a string.", subdescription: "Decoded data is assumed to be a string.",

View File

@ -28,6 +28,7 @@ const (
XMLOutputFormat XMLOutputFormat
Base64OutputFormat Base64OutputFormat
UriOutputFormat UriOutputFormat
ShOutputFormat
) )
func OutputFormatFromString(format string) (PrinterOutputFormat, error) { func OutputFormatFromString(format string) (PrinterOutputFormat, error) {