Skip to content

Commit

Permalink
Added shell string encoder (@sh) #1526
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefarah committed Feb 2, 2023
1 parent d119dbc commit d21bb92
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 0 deletions.
17 changes: 17 additions & 0 deletions pkg/yqlib/doc/operators/encode-decode.md
Expand Up @@ -17,6 +17,7 @@ These operators are useful to process yaml documents that have stringified embed
| XML | from_xml/@xmld | to_xml(i)/@xml |
| Base64 | @base64d | @base64 |
| URI | @urid | @uri |
| Shell | | @sh |


See CSV and TSV [documentation](https://mikefarah.gitbook.io/yq/usage/csv-tsv) for accepted formats.
Expand Down Expand Up @@ -464,6 +465,22 @@ will output
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
Decoded data is assumed to be a string.

Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/doc/operators/headers/encode-decode.md
Expand Up @@ -17,6 +17,7 @@ These operators are useful to process yaml documents that have stringified embed
| XML | from_xml/@xmld | to_xml(i)/@xml |
| Base64 | @base64d | @base64 |
| URI | @urid | @uri |
| Shell | | @sh |


See CSV and TSV [documentation](https://mikefarah.gitbook.io/yq/usage/csv-tsv) for accepted formats.
Expand Down
44 changes: 44 additions & 0 deletions pkg/yqlib/encoder_sh.go
@@ -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)
}
1 change: 1 addition & 0 deletions pkg/yqlib/lexer_participle.go
Expand Up @@ -80,6 +80,7 @@ var participleYqRules = []*participleYqRule{

{"Urid", `@urid`, decodeOp(UriInputFormat), 0},
{"Uri", `@uri`, encodeWithIndent(UriOutputFormat, 0), 0},
{"SH", `@sh`, encodeWithIndent(ShOutputFormat, 0), 0},

{"LoadXML", `load_?xml|xml_?load`, loadOp(NewXMLDecoder(ConfiguredXMLPreferences), false), 0},

Expand Down
2 changes: 2 additions & 0 deletions pkg/yqlib/operator_encoder_decoder.go
Expand Up @@ -28,6 +28,8 @@ func configureEncoder(format PrinterOutputFormat, indent int) Encoder {
return NewBase64Encoder()
case UriOutputFormat:
return NewUriEncoder()
case ShOutputFormat:
return NewShEncoder()
}
panic("invalid encoder")
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/yqlib/operator_encoder_decoder_test.go
Expand Up @@ -257,6 +257,15 @@ var encoderDecoderOperatorScenarios = []expressionScenario{
"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",
subdescription: "Decoded data is assumed to be a string.",
Expand Down
1 change: 1 addition & 0 deletions pkg/yqlib/printer.go
Expand Up @@ -28,6 +28,7 @@ const (
XMLOutputFormat
Base64OutputFormat
UriOutputFormat
ShOutputFormat
)

func OutputFormatFromString(format string) (PrinterOutputFormat, error) {
Expand Down

0 comments on commit d21bb92

Please sign in to comment.