Skip to content

Commit

Permalink
cmd: clean up printer implementations (#308)
Browse files Browse the repository at this point in the history
fixes #307

Signed-off-by: Nick Santos <[email protected]>
  • Loading branch information
nicks authored Aug 30, 2023
1 parent 8ca4793 commit a1d6c93
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 155 deletions.
112 changes: 0 additions & 112 deletions internal/printers/name.go

This file was deleted.

20 changes: 16 additions & 4 deletions pkg/api/accessors.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
package api

func (c *Cluster) GetName() string {
return c.Name
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (c *Cluster) GetObjectMeta() metav1.Object {
return &metav1.ObjectMeta{
Name: c.Name,
}
}
func (r *Registry) GetName() string {
return r.Name

func (r *Registry) GetObjectMeta() metav1.Object {
return &metav1.ObjectMeta{
Name: r.Name,
}
}

var _ metav1.ObjectMetaAccessor = &Cluster{}
var _ metav1.ObjectMetaAccessor = &Registry{}
2 changes: 1 addition & 1 deletion pkg/cmd/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (o *ApplyOptions) run() error {

ctx := context.TODO()

printer, err := toPrinter(o.PrintFlags)
printer, err := o.PrintFlags.ToPrinter()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (o *CreateClusterOptions) run(controller clusterCreator, product string) er
return err
}

printer, err := toPrinter(o.PrintFlags)
printer, err := o.PrintFlags.ToPrinter()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/create_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (o *CreateRegistryOptions) run(controller registryCreator, name string) err
return err
}

printer, err := toPrinter(o.PrintFlags)
printer, err := o.PrintFlags.ToPrinter()
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (o *DeleteOptions) run(args []string) error {
return err
}

printer, err := toPrinter(o.PrintFlags)
printer, err := o.PrintFlags.ToPrinter()
if err != nil {
return err
}
Expand Down
37 changes: 28 additions & 9 deletions pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/duration"
Expand Down Expand Up @@ -144,7 +145,7 @@ func (o *GetOptions) ToPrinter() (printers.ResourcePrinter, error) {
if !o.OutputFlagSpecified() {
return printers.NewTablePrinter(printers.PrintOptions{}), nil
}
return toPrinter(o.PrintFlags)
return o.PrintFlags.ToPrinter()
}

func (o *GetOptions) Print(obj runtime.Object) error {
Expand All @@ -158,9 +159,31 @@ func (o *GetOptions) Print(obj runtime.Object) error {
return err
}

err = printer.PrintObj(o.transformForOutput(obj), o.Out)
if err != nil {
return err
if !o.OutputFlagSpecified() {
err = printer.PrintObj(o.toTable(obj), o.Out)
if err != nil {
return err
}
} else {
// Name printer only supports UnstructuredList for mysterious reasons.
_, isNamePrinter := printer.(*printers.NamePrinter)
if isNamePrinter && meta.IsListType(obj) {
items, err := meta.ExtractList(obj)
if err != nil {
return err
}
for _, item := range items {
err = printer.PrintObj(item, o.Out)
if err != nil {
return err
}
}
} else {
err = printer.PrintObj(obj, o.Out)
if err != nil {
return err
}
}
}
return nil
}
Expand All @@ -169,11 +192,7 @@ func (o *GetOptions) OutputFlagSpecified() bool {
return o.PrintFlags.OutputFlagSpecified != nil && o.PrintFlags.OutputFlagSpecified()
}

func (o *GetOptions) transformForOutput(obj runtime.Object) runtime.Object {
if o.OutputFlagSpecified() {
return obj
}

func (o *GetOptions) toTable(obj runtime.Object) runtime.Object {
switch r := obj.(type) {
case *api.Registry:
return o.registriesAsTable([]api.Registry{*r})
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func TestDefaultPrint(t *testing.T) {
o.IOStreams = streams
o.StartTime = startTime

err := o.Print(o.transformForOutput(clusterList))
err := o.Print(o.toTable(clusterList))
require.NoError(t, err)
assert.Equal(t, out.String(), `CURRENT NAME PRODUCT AGE REGISTRY
* microk8s microk8s 3y none
Expand All @@ -100,7 +100,7 @@ func TestYAML(t *testing.T) {
err := o.Command().Flags().Set("output", "yaml")
require.NoError(t, err)

err = o.Print(o.transformForOutput(clusterList))
err = o.Print(clusterList)
require.NoError(t, err)
assert.Equal(t, `apiVersion: ctlptl.dev/v1alpha1
items:
Expand Down Expand Up @@ -129,7 +129,7 @@ func TestRegistryPrint(t *testing.T) {
o.IOStreams = streams
o.StartTime = startTime

err := o.Print(o.transformForOutput(registryList))
err := o.Print(o.toTable(registryList))
require.NoError(t, err)
assert.Equal(t, `NAME HOST ADDRESS CONTAINER ADDRESS AGE
ctlptl-registry 0.0.0.0:5001 172.17.0.2:5000 3y
Expand Down
23 changes: 0 additions & 23 deletions pkg/cmd/printer.go

This file was deleted.

0 comments on commit a1d6c93

Please sign in to comment.