Skip to content
This repository has been archived by the owner on Jun 28, 2023. It is now read-only.

Add ability to render output in json and yaml #717

Closed
wants to merge 1 commit into from

Conversation

stmcginnis
Copy link
Contributor

What this PR does / why we need it

This adds the ability to select from the default table output, or have
more machine readable output in yaml or json format.

Which issue(s) this PR fixes

Fixes: #626

Describe testing done for PR

Built plugins locally and tested output:

❯ tanzu package list
  NAME                             VERSION         DESCRIPTION
  cert-manager.tce.vmware.com      1.1.0-vmware0   This package provides certificate management functionality.
  contour-operator.tce.vmware.com  1.11.0-vmware0  This package provides an ingress controller.
  external-dns.tce.vmware.com      0.7.6-vmware0   This package provides external DNS capabilities.
  fluent-bit.tce.vmware.com        1.7.2-vmware0   Fluent Bit is an open source Log Processor and Forwarder.
  gatekeeper.tce.vmware.com        3.2.3-vmware0   This package provides custom admission control.
  grafana.tce.vmware.com           7.4.3-vmware0   Grafana is open source visualization and analytics software.
  knative-serving.tce.vmware.com   0.22.0-vmware0  This package provides serverless functionality.
  prometheus.tce.vmware.com        2.25.0-vmware0  A time series database for your metrics.
  velero.tce.vmware.com            1.5.2-vmware0   This package provides disaster recovery capabilities.
❯ tanzu package list -o yaml
- description: This package provides certificate management functionality.
  name: cert-manager.tce.vmware.com
  version: 1.1.0-vmware0
- description: This package provides an ingress controller.
  name: contour-operator.tce.vmware.com
  version: 1.11.0-vmware0
- description: This package provides external DNS capabilities.
  name: external-dns.tce.vmware.com
  version: 0.7.6-vmware0
- description: Fluent Bit is an open source Log Processor and Forwarder.
  name: fluent-bit.tce.vmware.com
  version: 1.7.2-vmware0
- description: This package provides custom admission control.
  name: gatekeeper.tce.vmware.com
  version: 3.2.3-vmware0
- description: Grafana is open source visualization and analytics software.
  name: grafana.tce.vmware.com
  version: 7.4.3-vmware0
- description: This package provides serverless functionality.
  name: knative-serving.tce.vmware.com
  version: 0.22.0-vmware0
- description: A time series database for your metrics.
  name: prometheus.tce.vmware.com
  version: 2.25.0-vmware0
- description: This package provides disaster recovery capabilities.
  name: velero.tce.vmware.com
  version: 1.5.2-vmware0
❯ tanzu package list -o json
[
  {
    "description": "This package provides certificate management functionality.",
    "name": "cert-manager.tce.vmware.com",
    "version": "1.1.0-vmware0"
  },
  {
    "description": "This package provides an ingress controller.",
    "name": "contour-operator.tce.vmware.com",
    "version": "1.11.0-vmware0"
  },
  {
    "description": "This package provides external DNS capabilities.",
    "name": "external-dns.tce.vmware.com",
    "version": "0.7.6-vmware0"
  },
  {
    "description": "Fluent Bit is an open source Log Processor and Forwarder.",
    "name": "fluent-bit.tce.vmware.com",
    "version": "1.7.2-vmware0"
  },
  {
    "description": "This package provides custom admission control.",
    "name": "gatekeeper.tce.vmware.com",
    "version": "3.2.3-vmware0"
  },
  {
    "description": "Grafana is open source visualization and analytics software.",
    "name": "grafana.tce.vmware.com",
    "version": "7.4.3-vmware0"
  },
  {
    "description": "This package provides serverless functionality.",
    "name": "knative-serving.tce.vmware.com",
    "version": "0.22.0-vmware0"
  },
  {
    "description": "A time series database for your metrics.",
    "name": "prometheus.tce.vmware.com",
    "version": "2.25.0-vmware0"
  },
  {
    "description": "This package provides disaster recovery capabilities.",
    "name": "velero.tce.vmware.com",
    "version": "1.5.2-vmware0"
  }
]

Special notes for your reviewer

We should decide if this is enough, or if we would like to emit additional data in these formats that we wouldn't necessarily display in the table output.

Does this PR introduce a user-facing change?

Added ability to format output in YAML and JSON formats.

This adds the ability to select from the default table output, or have
more machine readable output in yaml or json format.

Signed-off-by: Sean McGinnis <[email protected]>
Copy link
Contributor

@jpmcb jpmcb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Tested on a standalone cluster, installed the default package repository and listed with table, yaml, and json. All look great! Very neat implementation too

// renderJSON prints output as json
func renderJSON(ow *outputwriter) {
data := ow.dataStruct()
bytesJSON, err := json.MarshalSafeCollections(data)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that github.com/helloeave/json forwards to github.com/homelight/json (probably an internal change of ownership). It looks to be a fork of go's encoding/json library but with a few additional "safe" methods for handling null, primarily through the MarshalSafeCollections method. But it hasn't been updated in ~1/2 year. Would it be worth including logic in our own writer in order to handle null cases so we can instead use the core encoding/json library?

Hmmm. But, on deeper inspection, it seems this isn't really handled well in go at all. If this gets around an imperfection in the go library, makes sense to me 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stmcginnis - Circling back on this one. I think it's reasonable to introduce this library so we don't have to deal with Go's handling of null cases in json. Let me know your thoughts and we can merge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for bringing this up again. I should mark this as WIP for now.

Based on discussion in Slack, the core repo really is a more appropriate place for something like this. Then our CLI plugins can just use that common functionality so we're consistent across all plugins.

That change has now merged to core. We just need to wait to use a more recent commit from there and then I can update this PR to switch over to using the common functionality.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! Thanks for the update - good to hear it's made it's way into core!

@stmcginnis stmcginnis changed the title PoC: Add ability to render output in json and yaml Add ability to render output in json and yaml Jun 9, 2021
@stmcginnis stmcginnis changed the title Add ability to render output in json and yaml WIP: Add ability to render output in json and yaml Jun 23, 2021
@jpmcb jpmcb changed the title WIP: Add ability to render output in json and yaml Add ability to render output in json and yaml Jul 8, 2021
@jpmcb jpmcb marked this pull request as draft July 8, 2021 20:53
@nrb nrb added the owner/core-eng Work executed by TCE's core engineering team label Jul 9, 2021
@stmcginnis
Copy link
Contributor Author

No longer needed now. Changes have been merged to tanzu-framework, but we no longer have commands that need the different output formats in the TCE repo.

@stmcginnis stmcginnis closed this Jul 14, 2021
@davidvonthenen davidvonthenen deleted the format-output branch September 28, 2021 17:43
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
owner/core-eng Work executed by TCE's core engineering team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Question: Do we support json and yaml output for package list?
3 participants