Skip to content

Commit

Permalink
Ensure consistent colouring across get and list commands
Browse files Browse the repository at this point in the history
  • Loading branch information
100mik committed May 31, 2022
1 parent 3beaddc commit b237b35
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 32 deletions.
31 changes: 26 additions & 5 deletions cli/pkg/kctrl/cmd/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
package app

import (
"fmt"

"github.com/cppforlife/color"
"github.com/spf13/cobra"
kcv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
kcpkgv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
Expand All @@ -29,7 +28,29 @@ func isOwnedByPackageInstall(app *kcv1alpha1.App) bool {
return false
}

func getAppDescription(name string, namespace string) string {
description := fmt.Sprintf("app/%s (kappctrl.k14s.io/v1alpha1) namespace: %s", name, namespace)
return description
func appStatusString(app *kcv1alpha1.App) string {
if len(app.Status.Conditions) < 1 {
return ""
}
if app.Spec.Canceled {
return color.RedString("Camceled")
}
if app.Spec.Paused {
return color.YellowString("Paused")
}
for _, condition := range app.Status.Conditions {
switch condition.Type {
case kcv1alpha1.ReconcileFailed:
return color.RedString("Reconcile failed")
case kcv1alpha1.ReconcileSucceeded:
return color.GreenString("Reconcile succeeded")
case kcv1alpha1.DeleteFailed:
return color.RedString("Deletion failed")
case kcv1alpha1.Reconciling:
return "Reconciling"
case kcv1alpha1.Deleting:
return "Deleting"
}
}
return app.Status.FriendlyDescription
}
23 changes: 1 addition & 22 deletions cli/pkg/kctrl/cmd/app/app_tailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ func (o *AppTailer) printInfo(app kcv1alpha1.App) {
Rows: [][]uitable.Value{{
uitable.NewValueString(app.Name),
uitable.NewValueString(app.Namespace),
uitable.NewValueString(o.statusString(app.Status)),
uitable.NewValueString(appStatusString(&app)),
uitable.NewValueString(o.metricString(app.Status)),
}},
}
Expand All @@ -155,27 +155,6 @@ func (o *AppTailer) metricString(status kcv1alpha1.AppStatus) string {
}
}

func (o *AppTailer) statusString(status kcv1alpha1.AppStatus) string {
if len(status.Conditions) < 1 {
return ""
}
for _, condition := range status.Conditions {
switch condition.Type {
case kcv1alpha1.ReconcileFailed:
return color.RedString("Reconcile failed")
case kcv1alpha1.ReconcileSucceeded:
return color.GreenString("Reconcile succeeded")
case kcv1alpha1.DeleteFailed:
return color.RedString("Deletion failed")
case kcv1alpha1.Reconciling:
return "Reconciling"
case kcv1alpha1.Deleting:
return "Deleting"
}
}
return status.FriendlyDescription
}

func (o *AppTailer) hasReconciled(status kcv1alpha1.AppStatus) bool {
for _, condition := range status.Conditions {
if condition.Type == kcv1alpha1.ReconcileSucceeded && condition.Status == corev1.ConditionTrue {
Expand Down
2 changes: 1 addition & 1 deletion cli/pkg/kctrl/cmd/app/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (o *GetOptions) Run() error {
uitable.NewValueString(app.Namespace),
uitable.NewValueString(app.Name),
uitable.NewValueString(app.Spec.ServiceAccountName),
uitable.NewValueString(app.Status.FriendlyDescription),
uitable.NewValueString(appStatusString(app)),
uitable.NewValueInterface(o.formatOwnerReferences(app.OwnerReferences)),
uitable.NewValueInterface(app.Status.Conditions),
uitable.NewValueString(o.failingStage(app.Status)),
Expand Down
2 changes: 1 addition & 1 deletion cli/pkg/kctrl/cmd/app/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (o *ListOptions) Run() error {
table.Rows = append(table.Rows, []uitable.Value{
cmdcore.NewValueNamespace(app.Namespace),
uitable.NewValueString(app.Name),
uitable.NewValueString(app.Status.FriendlyDescription),
uitable.NewValueString(appStatusString(&app)),
sinceDeployAge,
uitable.NewValueString(o.owner(app.OwnerReferences)),
cmdcore.NewValueAge(app.CreationTimestamp.Time),
Expand Down
20 changes: 18 additions & 2 deletions cli/pkg/kctrl/cmd/package/installed/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/spf13/cobra"
cmdcore "github.com/vmware-tanzu/carvel-kapp-controller/cli/pkg/kctrl/cmd/core"
"github.com/vmware-tanzu/carvel-kapp-controller/cli/pkg/kctrl/logger"
kcv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/kappctrl/v1alpha1"
kcpkgv1alpha1 "github.com/vmware-tanzu/carvel-kapp-controller/pkg/apis/packaging/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
Expand Down Expand Up @@ -210,10 +211,25 @@ func (o *GetOptions) showValuesData(pkgi *kcpkgv1alpha1.PackageInstall) error {

func packageInstallStatus(pkgi *kcpkgv1alpha1.PackageInstall) string {
if pkgi.Spec.Canceled {
return "Canceled"
return color.RedString("Canceled")
}
if pkgi.Spec.Paused {
return "Paused"
return color.YellowString("Paused")
}

for _, condition := range pkgi.Status.Conditions {
switch condition.Type {
case kcv1alpha1.ReconcileFailed:
return color.RedString("Reconcile failed")
case kcv1alpha1.ReconcileSucceeded:
return color.GreenString("Reconcile succeeded")
case kcv1alpha1.DeleteFailed:
return color.RedString("Deletion failed")
case kcv1alpha1.Reconciling:
return "Reconciling"
case kcv1alpha1.Deleting:
return "Deleting"
}
}
return pkgi.Status.FriendlyDescription
}
2 changes: 1 addition & 1 deletion cli/test/e2e/app_commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ spec:
"namespace": "kctrl-test",
"owner_references": "",
"service_account": "kappctrl-e2e-ns-sa",
"status": "Canceled/paused",
"status": "Paused",
}}
require.Exactly(t, expectedOutputRows, replaceAgeAndSinceDeployed(output.Tables[0].Rows))
})
Expand Down

0 comments on commit b237b35

Please sign in to comment.