Skip to content

Commit

Permalink
Adds more columns in service list command output
Browse files Browse the repository at this point in the history
 This commit adds basic details about service in
 service list command output.
 text/tabwriter is used to print columns on stdout.

 Fixes #knative#40
  • Loading branch information
navidshaikh committed Apr 9, 2019
1 parent 8736395 commit 1d6e1b0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 12 deletions.
46 changes: 34 additions & 12 deletions pkg/kn/commands/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.",
Expand All @@ -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
}
34 changes: 34 additions & 0 deletions pkg/util/printers/tabwriter.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 1d6e1b0

Please sign in to comment.