Skip to content

Commit

Permalink
Decode base64-encoded fields during output
Browse files Browse the repository at this point in the history
Closes #247

Related to linode/linode-api-docs#468

The API returned kubeconfigs as a base64-encoded YAML string.  The
linked docs PR uses the appropriate JSON Schema fields to note the
encoding and actual media type, and this CLI PR adds support for that
information and handling it.

However, the output for `linode-cli lke kubeconfig-view $CLUSTER_ID` is
_pretty terrible_ with this change.  The `token` and `certificate-authority-data`
fields are _very long_, and cause wrapping in the terminal, which messes
up the entire output table.  This can be corrected by using `--text` as
the output format, which, paired with `--no-headers`, produces a
nicely-formatted YAML file as output; the default behavior being ugly
isn't great in my opinion though.

This change isn't strictly necessary, as a user can run
`linode-cli lke kubeconfig-view $CLUSTER_ID --text --no-headers | base64 -d`
to see the same output today; however, having the default output be
useful would be a nice touch.

I see the following options here:

 * Accept that the current output is fine and do not merge this PR
 * Accept that this PR's output is ugly by default and merge it anyway
 * Come up with a way to signal to the CLI that an operation should
   default to `--text --no-headers`-style output as part of this PR.

This change probably also represents a breaking change for the CLI, as
an existing script that does the decoding as noted above would stop
working with this change merged in.

Opinions appreciated; I will close/open/update this PR as relevant based
on discussion here.
  • Loading branch information
Dorthu committed Jun 21, 2021
1 parent b212eef commit 277288b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
3 changes: 2 additions & 1 deletion linodecli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ def _parse_properties(self, node, prefix=[]):
info.get('x-linode-cli-display') or False,
info.get('type') or 'string',
color_map=info.get('x-linode-cli-color'),
item_type=item_type))
item_type=item_type,
content_encoding=info.get("contentEncoding")))

return attrs

Expand Down
8 changes: 7 additions & 1 deletion linodecli/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""
from __future__ import print_function

import base64
from colorclass import Color


class ModelAttr:
def __init__(self, name, filterable, display, datatype, color_map=None, item_type=None):
def __init__(self, name, filterable, display, datatype, color_map=None, item_type=None, content_encoding=None):
self.name = name
self.value = None
self.filterable = filterable
Expand All @@ -17,6 +18,7 @@ def __init__(self, name, filterable, display, datatype, color_map=None, item_typ
self.datatype = datatype
self.color_map = color_map
self.item_type = item_type
self.content_encoding = content_encoding

def _get_value(self, model):
"""
Expand All @@ -29,6 +31,10 @@ def _get_value(self, model):
return None
value = value[part]

if self.content_encoding:
if self.content_encoding == "base64":
value = base64.b64decode(value.encode()).decode()

return value

def render_value(self, model, colorize=True):
Expand Down

0 comments on commit 277288b

Please sign in to comment.