From 1d6e1b0b9abb7d4af6cb5a5301fc6788f424d133 Mon Sep 17 00:00:00 2001 From: Navid Shaikh Date: Thu, 4 Apr 2019 22:14:20 +0530 Subject: [PATCH] Adds more columns in service list command output This commit adds basic details about service in service list command output. text/tabwriter is used to print columns on stdout. Fixes #https://github.com/knative/client/issues/40 --- pkg/kn/commands/service_list.go | 46 ++++++++++++++++++++++++--------- pkg/util/printers/tabwriter.go | 34 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 12 deletions(-) create mode 100644 pkg/util/printers/tabwriter.go diff --git a/pkg/kn/commands/service_list.go b/pkg/kn/commands/service_list.go index c8a0bb4bd1..02560db291 100644 --- a/pkg/kn/commands/service_list.go +++ b/pkg/kn/commands/service_list.go @@ -15,6 +15,12 @@ package commands import ( + "fmt" + "strings" + "text/tabwriter" + + printers "github.com/knative/client/pkg/util/printers" + servingv1alpha1 "github.com/knative/serving/pkg/apis/serving/v1alpha1" "github.com/spf13/cobra" v1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime/schema" @@ -23,11 +29,8 @@ import ( var serviceListPrintFlags *genericclioptions.PrintFlags -// listCmd represents the list command +// NewServiceListCommand represents the list command func NewServiceListCommand(p *KnParams) *cobra.Command { - - serviceListPrintFlags := genericclioptions.NewPrintFlags("").WithDefaultOutput( - "jsonpath={range .items[*]}{.metadata.name}{\"\\n\"}{end}") serviceListCommand := &cobra.Command{ Use: "list", Short: "List available services.", @@ -42,21 +45,40 @@ func NewServiceListCommand(p *KnParams) *cobra.Command { return err } - printer, err := serviceListPrintFlags.ToPrinter() - if err != nil { - return err - } service.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{ Group: "knative.dev", Version: "v1alpha1", Kind: "Service"}) - err = printer.PrintObj(service, cmd.OutOrStdout()) - if err != nil { + + printer := printers.GetNewTabWriter(cmd.OutOrStdout()) + + if err := printServiceList(printer, *service); err != nil { return err } - return nil + printer.Flush() + + return err }, } - serviceListPrintFlags.AddFlags(serviceListCommand) return serviceListCommand } + +func printServiceList(printer *tabwriter.Writer, services servingv1alpha1.ServiceList) error { + // case where no services are present + if len(services.Items) < 1 { + fmt.Fprintln(printer, "No resources found.") + return nil + } + columnNames := []string{"NAME", "DOMAIN", "LATESTCREATED", "LATESTREADY"} + if _, err := fmt.Fprintf(printer, "%s\n", strings.Join(columnNames, "\t")); err != nil { + return err + } + for _, ksvc := range services.Items { + _, err := fmt.Fprintf(printer, "%s\n", strings.Join([]string{ksvc.Name, ksvc.Status.Domain, + ksvc.Status.LatestCreatedRevisionName, ksvc.Status.LatestReadyRevisionName}, "\t")) + if err != nil { + return err + } + } + return nil +} diff --git a/pkg/util/printers/tabwriter.go b/pkg/util/printers/tabwriter.go new file mode 100644 index 0000000000..9d3c07879e --- /dev/null +++ b/pkg/util/printers/tabwriter.go @@ -0,0 +1,34 @@ +// Copyright © 2018 The Knative Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implie +// See the License for the specific language governing permissions and +// limitations under the License. + +package printers + +import ( + "io" + + "text/tabwriter" +) + +const ( + tabwriterMinWidth = 6 + tabwriterWidth = 4 + tabwriterPadding = 3 + tabwriterPadChar = ' ' + tabwriterFlags = tabwriter.TabIndent +) + +// GetNewTabWriter returns a tabwriter that translates tabbed columns in input into properly aligned text. +func GetNewTabWriter(output io.Writer) *tabwriter.Writer { + return tabwriter.NewWriter(output, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags) +}