Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base64 encode and decode methods #181

Open
malisetti opened this issue Aug 20, 2023 · 3 comments
Open

Add Base64 encode and decode methods #181

malisetti opened this issue Aug 20, 2023 · 3 comments

Comments

@malisetti
Copy link

No description provided.

@bitfield
Copy link
Owner

Hi @malisetti! Thanks for opening the issue. Could you give us a sample program showing some code that you'd like to write using this feature?

@alex-vsl
Copy link

alex-vsl commented Aug 30, 2023

@bitfield In my opinion this is very handy for sure- especialy in k8s ecosystem, where secrets are usually base64-encoded.

bash with kubectl:

kubectl get secret my-secret -o=jsonpath='{.data.myfield}' | base64 --decode

It's not hard to image how that could be used in conjuction with Exec() and JQ:

secretField := script.Exec("kubectl get secret --output json --namespace XYZ some-secret").JQ(".data.myfield").Base64Decode().String()

And vice versa- you might want to perform base64 encoding while setting some value.
What is more, base64 is widely used across the web, for instance sometimes HTML | CSS | JS values in the DOM Tree are encoded in base64

@bitfield
Copy link
Owner

That sounds totally reasonable. But your example program:

secretField := script.Exec("kubectl ...").JQ(".data.myfield").Base64Decode().String()

doesn't actually match the API in #182, which is:

func (p *Pipe) Base64Encode(enc *base64.Encoding) (string, error) {

In other words, Base64Encode, as implemented, returns string and error, rather than a pipe. I think making it a filter works better, don't you? For example, you might want to pass the base64-encoded data to some other command via Exec, or just do some further pipe filtering on it.

Also, in case we want to encode/decode other formats as well as base64, perhaps we should name the method EncodeBase64 instead of Base64Encode. What do you think?

It seems a shame to make users pass a *base64.Encoding as a mandatory parameter. That sounds like paperwork, doesn't it? Would it be more in the spirit of script to assume the user wants base64.StdEncoding by default?

What do you think about changing the PR to adopt an API something like this?

func (p *Pipe) EncodeBase64() *Pipe {

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants