vmware-tanzu / tanzu-cli

The Tanzu Core CLI project provides the core functionality of the Tanzu CLI. The CLI is based on a plugin architecture where CLI command functionality can be delivered through independently developed plugin binaries
Apache License 2.0
33 stars 21 forks source link

Add a new `tanzu context current [--short]` command to easily show the current context #750

Closed marckhouzam closed 4 months ago

marckhouzam commented 4 months ago

What this PR does / why we need it

This PR add a new tanzu context current [--short] command to easily show the current context.

In preparation for the fact we plan on deprecating having two context active at the same time, the command only shows a single active context, prioritizing the tanzu or k8s ones. This means that the new command command will only show an active TMC context if that is the only context active (if there are no tanzu or k8s context active).

The command has to forms: long and short. The long form, triggered by default is:

$ tz context current
  Name:            TAP_pre-integration-staging-d03c5c97
  Type:            tanzu
  Organization:    TAP pre-integration (ae93ebb4-a249-4553-aa1e-c87c4b7f75e5)
  Project:         spaces-beta3-validation (8185f2b6-8cfe-4f48-9484-f5b72e182daf)
  Kube Config:     /Users/kmarc/.kube/config
  Kube Context:    tanzu-cli-TAP_pre-integration-staging-d03c5c97

Note that the Organization and Project lines will only show for tanzu contexts.
The Kube Context and Kube Config lines will only show for tanzu or k8s contexts. If a tanzu context has a space or cluster group set, a line will be shown for it.

The short form (tanzu context current --short):

<contextName>:<project>:<space | clustergroup>

Note that :<project> and :<space | clustergroup> will only shown for tanzu context, and only if they are set.

The PR updates the go.mod file to make use of the latest tanzu-plugin-runtime that allows to access the OrgName.

Which issue(s) this PR fixes

Fixes #

Describe testing done for PR

# There are not active contexts
$ tz context list
  NAME                                  ISACTIVE  TYPE             PROJECT                  SPACE
  TAP_pre-integration-staging-d03c5c97  false     tanzu            spaces-beta3-validation  space-demo-2
  testNonTAPOrgCtx                      false     tanzu
  tkg3                                  false     kubernetes       n/a                      n/a
  tmc                                   false     mission-control  n/a                      n/a
  ucp-betap-saas-stgta2-e2e             false     tanzu

[i] Use '--wide' to view additional columns.

$ tz context current
There is no active context
$ tz context current --short
There is no active context

# Activate only a tmc context
$ tz context use tmc
[i] Successfully activated context 'tmc' (Type: mission-control)
[i] Fetching recommended plugins for active context 'tmc'...
[ok] All recommended plugins are already installed and up-to-date.

$ tz context current
  Name:        tmc
  Type:        mission-control
$ tz context current --short
tmc

# Activate a tanzu context
$ tz context use TAP_pre-integration-staging-d03c5c97
[i] Successfully activated context 'TAP_pre-integration-staging-d03c5c97' (Type: tanzu, Project: spaces-beta3-validation (8185f2b6-8cfe-4f48-9484-f5b72e182daf), Space: space-demo-2)
[i] Fetching recommended plugins for active context 'TAP_pre-integration-staging-d03c5c97'...
[ok] No recommended plugins found.

# We have two contexts active with a space an project set on the tanzu one
$ tz context list
  NAME                                  ISACTIVE  TYPE             PROJECT                  SPACE
  TAP_pre-integration-staging-d03c5c97  true      tanzu            spaces-beta3-validation  space-demo-2
  testNonTAPOrgCtx                      false     tanzu
  tkg3                                  false     kubernetes       n/a                      n/a
  tmc                                   true      mission-control  n/a                      n/a
  ucp-betap-saas-stgta2-e2e             false     tanzu

[i] Use '--wide' to view additional columns.

# Only the tanzu context is shown
# Space and project are shown
$ tz context current
  Name:            TAP_pre-integration-staging-d03c5c97
  Type:            tanzu
  Organization:    TAP pre-integration (ae93ebb4-a249-4553-aa1e-c87c4b7f75e5)
  Project:         spaces-beta3-validation (8185f2b6-8cfe-4f48-9484-f5b72e182daf)
  Space:           space-demo-2
  Kube Config:     /Users/kmarc/.kube/config
  Kube Context:    tanzu-cli-TAP_pre-integration-staging-d03c5c97
$ tz context current --short
TAP_pre-integration-staging-d03c5c97:spaces-beta3-validation:space-demo-2

# Activate a kubernetes context (which replaces the tanzu active context)
$ tz context use tkg3
[i] Successfully activated context 'tkg3' (Type: kubernetes)
[i] Fetching recommended plugins for active context 'tkg3'...
[i] Installing the following plugins recommended by context 'tkg3':
  NAME    TARGET      INSTALLING
  imgpkg  kubernetes  v0.3.5
[!] unable to automatically sync the plugins recommended by the active context. Please run 'tanzu plugin sync' to sync plugins manually, error: 'unable to find plugin 'imgpkg' matching version 'v0.3.5' for target 'kubernetes''

# Only the k8s context is shown, although the TMC is also active
$ tz context current
  Name:            tkg3
  Type:            kubernetes
  Kube Config:     /Users/kmarc/.k3d/kubeconfig-tkg3.yaml
  Kube Context:    k3d-tkg3
$ tz context current --short
tkg3

# Activate a tanzu context that has no project set
$ tz context use testNonTAPOrgCtx
[i] Successfully activated context 'testNonTAPOrgCtx' (Type: tanzu)
[i] Fetching recommended plugins for active context 'testNonTAPOrgCtx'...
[ok] No recommended plugins found.
$ tz context current
  Name:            testNonTAPOrgCtx
  Type:            tanzu
  Organization:    1_ARM_ESO_MAIN_FRESH (7753c388-914c-4085-88bc-1273f102c267)
  Project:         none set
  Kube Config:     /Users/kmarc/.kube/config
  Kube Context:    tanzu-cli-testNonTAPOrgCtx
$ tz context current --short
testNonTAPOrgCtx

# Unset the tanzu active context leaving only the TMC context active
$ tz context unset testNonTAPOrgCtx
The context 'testNonTAPOrgCtx' of type 'tanzu' has been set as inactive

$ tz context current
  Name:        tmc
  Type:        mission-control
$ tz context current --short
tmc

# Activate a tanzu context with a project set but unset the space
$ tz context use TAP_pre-integration-staging-d03c5c97
[i] Successfully activated context 'TAP_pre-integration-staging-d03c5c97' (Type: tanzu, Project: spaces-beta3-validation (8185f2b6-8cfe-4f48-9484-f5b72e182daf), Space: space-demo-2)
[i] Fetching recommended plugins for active context 'TAP_pre-integration-staging-d03c5c97'...
[ok] No recommended plugins found.
# unset the space
$ tz project use spaces-beta3-validation
✓ Successfully set project to spaces-beta3-validation

# Only Project is shown
$ tz context current
  Name:            TAP_pre-integration-staging-d03c5c97
  Type:            tanzu
  Organization:    TAP pre-integration (ae93ebb4-a249-4553-aa1e-c87c4b7f75e5)
  Project:         spaces-beta3-validation (8185f2b6-8cfe-4f48-9484-f5b72e182daf)
  Kube Config:     /Users/kmarc/.kube/config
  Kube Context:    tanzu-cli-TAP_pre-integration-staging-d03c5c97
$ tz context current --short
TAP_pre-integration-staging-d03c5c97:spaces-beta3-validation

# Set a cluster group
$ tz ops clustergroup list
  NAME     LABELS  INTEGRATIONS
  default
  run               TANZU_APPLICATION_ENGINE
$ tz ops clustergroup use run
ℹ  project has been set to spaces-beta3-validation
ℹ  successfully set clustergroup to run

$ tz context current
  Name:             TAP_pre-integration-staging-d03c5c97
  Type:             tanzu
  Organization:     TAP pre-integration (ae93ebb4-a249-4553-aa1e-c87c4b7f75e5)
  Project:          spaces-beta3-validation (8185f2b6-8cfe-4f48-9484-f5b72e182daf)
  Cluster Group:    run
  Kube Config:      /Users/kmarc/.kube/config
  Kube Context:     tanzu-cli-TAP_pre-integration-staging-d03c5c97
$ tz context current --short
TAP_pre-integration-staging-d03c5c97:spaces-beta3-validation:run

Release note

Add a new `tanzu context current [--short]` command to easily show the current context.

Additional information

Special notes for your reviewer

marckhouzam commented 4 months ago

LGTM.

I have a minor concern regarding the short form for "tanzu" context. The format shown as contextname[:<projectName>[:<spacename> | <clustername>]] without explicitly mentioning project: before the project name (and similarly for space name or clustergroup name) may be confusing to users. I understand that adding them would make this short form string longer, and it seems you chose this for brevity. Perhaps we can proceed with this format and address any feedback accordingly.

Correct, I opted for brevity. I imagined a user using the short output in their shell prompt and I felt that this information is supposed to help quickly know where the CLI is pointing but does not have to be perfect. I expect that a user should be familiar with the names of the context, project and space so they should be able to understand the short form. But let's see if there are other opinions.