-
-
Notifications
You must be signed in to change notification settings - Fork 600
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package yqlib | ||
|
||
import ( | ||
"bytes" | ||
"encoding/base64" | ||
"io" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
type base64Decoder struct { | ||
reader io.Reader | ||
finished bool | ||
encoding base64.Encoding | ||
} | ||
|
||
func NewBase64Decoder() Decoder { | ||
return &base64Decoder{finished: false, encoding: *base64.StdEncoding} | ||
} | ||
|
||
func (dec *base64Decoder) Init(reader io.Reader) { | ||
dec.reader = reader | ||
dec.finished = false | ||
} | ||
|
||
func (dec *base64Decoder) Decode(rootYamlNode *yaml.Node) error { | ||
if dec.finished { | ||
return io.EOF | ||
} | ||
base64Reader := base64.NewDecoder(&dec.encoding, dec.reader) | ||
buf := new(bytes.Buffer) | ||
|
||
if _, err := buf.ReadFrom(base64Reader); err != nil { | ||
return err | ||
} | ||
if buf.Len() == 0 { | ||
dec.finished = true | ||
return io.EOF | ||
} | ||
rootYamlNode.Kind = yaml.ScalarNode | ||
rootYamlNode.Tag = "!!str" | ||
rootYamlNode.Value = buf.String() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
|
||
{% hint style="warning" %} | ||
Note that versions prior to 4.18 require the 'eval/e' command to be specified.  | ||
|
||
`yq e <exp> <file>` | ||
{% endhint %} | ||
|
||
## Decode Base64 | ||
Decoded data is assumed to be a string. | ||
|
||
Given a sample.yml file of: | ||
```yml | ||
V29ya3Mgd2l0aCBVVEYtMTYg8J+Yig== | ||
``` | ||
then | ||
```bash | ||
yq -p=props sample.properties | ||
``` | ||
will output | ||
```yaml | ||
V29ya3Mgd2l0aCBVVEYtMTYg8J+Yig: = | ||
``` | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package yqlib | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"io" | ||
|
||
yaml "gopkg.in/yaml.v3" | ||
) | ||
|
||
type base64Encoder struct { | ||
encoding base64.Encoding | ||
} | ||
|
||
func NewBase64Encoder() Encoder { | ||
return &base64Encoder{encoding: *base64.StdEncoding} | ||
} | ||
|
||
func (e *base64Encoder) CanHandleAliases() bool { | ||
return false | ||
} | ||
|
||
func (e *base64Encoder) PrintDocumentSeparator(writer io.Writer) error { | ||
return nil | ||
} | ||
|
||
func (e *base64Encoder) PrintLeadingContent(writer io.Writer, content string) error { | ||
return nil | ||
} | ||
|
||
func (e *base64Encoder) Encode(writer io.Writer, originalNode *yaml.Node) error { | ||
node := unwrapDoc(originalNode) | ||
if guessTagFromCustomType(node) != "!!str" { | ||
return fmt.Errorf("cannot encode %v as base64, can only operate on strings. Please first pipe through another encoding operator to convert the value to a string", node.Tag) | ||
} | ||
_, err := writer.Write([]byte(e.encoding.EncodeToString([]byte(originalNode.Value)))) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters