Skip to content

Commit

Permalink
Update the UX for "context use" and "context list" commands to show P…
Browse files Browse the repository at this point in the history
…rojectID details

Signed-off-by: Prem Kumar Kalle <prem.kalle@broadcom.com>
  • Loading branch information
prkalle committed Apr 2, 2024
1 parent 50befed commit 7637a3f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
25 changes: 20 additions & 5 deletions pkg/command/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"reflect"
"regexp"
"slices"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -1182,10 +1183,13 @@ func useCtx(cmd *cobra.Command, args []string) error {
return err
}

suffixString := ""
suffixString := fmt.Sprintf("Type: %s", ctx.ContextType)
if ctx.ContextType == configtypes.ContextTypeTanzu {
if project, exists := ctx.AdditionalMetadata[config.ProjectNameKey]; exists && project != "" {
suffixString += fmt.Sprintf("Project: %s", project)
suffixString += fmt.Sprintf(", Project: %s", project)
}
if projectID, exists := ctx.AdditionalMetadata[config.ProjectIDKey]; exists && projectID != "" {
suffixString += fmt.Sprintf(", ProjectID: %s", projectID)
}
if space, exists := ctx.AdditionalMetadata[config.SpaceNameKey]; exists && space != "" {
suffixString += fmt.Sprintf(", Space: %s", space)
Expand All @@ -1195,10 +1199,10 @@ func useCtx(cmd *cobra.Command, args []string) error {
}
}
if suffixString != "" {
suffixString = " (" + suffixString + ")"
suffixString = "(" + suffixString + ")"
}

log.Infof("Successfully activated context '%s' of type '%s'%s.", ctxName, ctx.ContextType, suffixString)
log.Infof("Activated context '%s' %s successfully ", ctxName, suffixString)

// Sync all required plugins
_ = syncContextPlugins(cmd, ctx.ContextType, ctxName, true)
Expand Down Expand Up @@ -1366,6 +1370,7 @@ type ContextListOutputRow struct {
IsActive string
Type string
Project string
ProjectID string
Space string
ClusterGroup string
Endpoint string
Expand All @@ -1388,6 +1393,7 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
path := NA
context := NA
project := NA
projectID := NA
space := NA
clustergroup := NA

Expand All @@ -1401,6 +1407,7 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
case configtypes.ContextTypeTanzu:
tanzuContextExists = true
project = ""
projectID = ""
space = ""
clustergroup = ""
ep = ""
Expand All @@ -1414,6 +1421,9 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
if ctx.AdditionalMetadata[config.ProjectNameKey] != nil {
project = ctx.AdditionalMetadata[config.ProjectNameKey].(string)
}
if ctx.AdditionalMetadata[config.ProjectIDKey] != nil {
projectID = ctx.AdditionalMetadata[config.ProjectIDKey].(string)
}
if ctx.AdditionalMetadata[config.SpaceNameKey] != nil {
space = ctx.AdditionalMetadata[config.SpaceNameKey].(string)
}
Expand All @@ -1427,7 +1437,7 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
context = ctx.ClusterOpts.Context
}
}
row := ContextListOutputRow{ctx.Name, strconv.FormatBool(isCurrent), string(ctx.ContextType), project, space, clustergroup, ep, path, context}
row := ContextListOutputRow{ctx.Name, strconv.FormatBool(isCurrent), string(ctx.ContextType), project, projectID, space, clustergroup, ep, path, context}
rows = append(rows, row)
}

Expand All @@ -1438,6 +1448,11 @@ func displayContextListOutputWithDynamicColumns(cfg *configtypes.ClientConfig, w
dynamicColumns = append(dynamicColumns, "ClusterGroup")
}
if showAllColumns {
if tanzuContextExists {
// add "ProjectID" column after "Project"
projectIndex := slices.Index(requiredColumns, "Project")
requiredColumns = slices.Insert(requiredColumns, projectIndex+1, "ProjectID")
}
requiredColumns = append(requiredColumns, "Endpoint", "KubeconfigPath", "KubeContext")
requiredColumns = append(requiredColumns, dynamicColumns...)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/command/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ var _ = Describe("Test tanzu context command", func() {
columnsString := strings.Join(strings.Fields(lines[0]), " ")

Expect(err).To(BeNil())
Expect(columnsString).To(Equal("NAME ISACTIVE TYPE PROJECT SPACE CLUSTERGROUP ENDPOINT KUBECONFIGPATH KUBECONTEXT"))
Expect(columnsString).To(Equal("NAME ISACTIVE TYPE PROJECT PROJECTID SPACE CLUSTERGROUP ENDPOINT KUBECONFIGPATH KUBECONTEXT"))
})

It("should not return tanzu related columns when not listing tanzu contexts without --wide", func() {
Expand Down

0 comments on commit 7637a3f

Please sign in to comment.