Skip to content

Commit 88d44f4

Browse files
authored
Merge pull request kubernetes#76681 from seans3/humanreadable-cleanup
Removed unused code in humanreadable.go
2 parents ca0c446 + b3237ed commit 88d44f4

File tree

3 files changed

+12
-165
lines changed

3 files changed

+12
-165
lines changed

pkg/printers/humanreadable.go

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
package printers
1818

1919
import (
20-
"bytes"
2120
"fmt"
2221
"io"
2322
"reflect"
@@ -30,7 +29,6 @@ import (
3029
metav1beta1 "k8s.io/apimachinery/pkg/apis/meta/v1beta1"
3130
"k8s.io/apimachinery/pkg/labels"
3231
"k8s.io/apimachinery/pkg/runtime"
33-
"k8s.io/apimachinery/pkg/runtime/schema"
3432
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3533
)
3634

@@ -88,15 +86,6 @@ func (a *HumanReadablePrinter) With(fns ...func(PrintHandler)) *HumanReadablePri
8886
return a
8987
}
9088

91-
// EnsurePrintHeaders sets the HumanReadablePrinter option "NoHeaders" to false
92-
// and removes the .lastType that was printed, which forces headers to be
93-
// printed in cases where multiple lists of the same resource are printed
94-
// consecutively, but are separated by non-printer related information.
95-
func (h *HumanReadablePrinter) EnsurePrintHeaders() {
96-
h.options.NoHeaders = false
97-
h.lastType = nil
98-
}
99-
10089
// TableHandler adds a print handler with a given set of columns to HumanReadablePrinter instance.
10190
// See ValidateRowPrintHandlerFunc for required method signature.
10291
func (h *HumanReadablePrinter) TableHandler(columnDefinitions []metav1beta1.TableColumnDefinition, printFunc interface{}) error {
@@ -163,51 +152,6 @@ func ValidateRowPrintHandlerFunc(printFunc reflect.Value) error {
163152
return nil
164153
}
165154

166-
// ValidatePrintHandlerFunc validates print handler signature.
167-
// printFunc is the function that will be called to print an object.
168-
// It must be of the following type:
169-
// func printFunc(object ObjectType, w io.Writer, options PrintOptions) error
170-
// where ObjectType is the type of the object that will be printed.
171-
// DEPRECATED: will be replaced with ValidateRowPrintHandlerFunc
172-
func ValidatePrintHandlerFunc(printFunc reflect.Value) error {
173-
if printFunc.Kind() != reflect.Func {
174-
return fmt.Errorf("invalid print handler. %#v is not a function", printFunc)
175-
}
176-
funcType := printFunc.Type()
177-
if funcType.NumIn() != 3 || funcType.NumOut() != 1 {
178-
return fmt.Errorf("invalid print handler." +
179-
"Must accept 3 parameters and return 1 value.")
180-
}
181-
if funcType.In(1) != reflect.TypeOf((*io.Writer)(nil)).Elem() ||
182-
funcType.In(2) != reflect.TypeOf((*PrintOptions)(nil)).Elem() ||
183-
funcType.Out(0) != reflect.TypeOf((*error)(nil)).Elem() {
184-
return fmt.Errorf("invalid print handler. The expected signature is: "+
185-
"func handler(obj %v, w io.Writer, options PrintOptions) error", funcType.In(0))
186-
}
187-
return nil
188-
}
189-
190-
func (h *HumanReadablePrinter) HandledResources() []string {
191-
keys := make([]string, 0)
192-
193-
for k := range h.handlerMap {
194-
// k.String looks like "*api.PodList" and we want just "pod"
195-
api := strings.Split(k.String(), ".")
196-
resource := api[len(api)-1]
197-
if strings.HasSuffix(resource, "List") {
198-
continue
199-
}
200-
resource = strings.ToLower(resource)
201-
keys = append(keys, resource)
202-
}
203-
return keys
204-
}
205-
206-
func (h *HumanReadablePrinter) unknown(data []byte, w io.Writer) error {
207-
_, err := fmt.Fprintf(w, "Unknown object: %s", string(data))
208-
return err
209-
}
210-
211155
func printHeader(columnNames []string, w io.Writer) error {
212156
if _, err := fmt.Fprintf(w, "%s\n", strings.Join(columnNames, "\t")); err != nil {
213157
return err
@@ -283,15 +227,6 @@ func (h *HumanReadablePrinter) PrintObj(obj runtime.Object, output io.Writer) er
283227
return fmt.Errorf("error: unknown type %#v", obj)
284228
}
285229

286-
func hasCondition(conditions []metav1beta1.TableRowCondition, t metav1beta1.RowConditionType) bool {
287-
for _, condition := range conditions {
288-
if condition.Type == t {
289-
return condition.Status == metav1beta1.ConditionTrue
290-
}
291-
}
292-
return false
293-
}
294-
295230
// PrintTable prints a table to the provided output respecting the filtering rules for options
296231
// for wide columns and filtered rows. It filters out rows that are Completed. You should call
297232
// DecorateTable if you receive a table from a remote server before calling PrintTable.
@@ -565,65 +500,6 @@ func printRows(output io.Writer, rows []metav1beta1.TableRow, options PrintOptio
565500
}
566501
}
567502

568-
// TODO: this method assumes the meta/v1 server API, so should be refactored out of this package
569-
func printUnstructured(unstructured runtime.Unstructured, w io.Writer, additionalFields []string, options PrintOptions) error {
570-
metadata, err := meta.Accessor(unstructured)
571-
if err != nil {
572-
return err
573-
}
574-
575-
if options.WithNamespace {
576-
if _, err := fmt.Fprintf(w, "%s\t", metadata.GetNamespace()); err != nil {
577-
return err
578-
}
579-
}
580-
581-
content := unstructured.UnstructuredContent()
582-
kind := "<missing>"
583-
if objKind, ok := content["kind"]; ok {
584-
if str, ok := objKind.(string); ok {
585-
kind = str
586-
}
587-
}
588-
if objAPIVersion, ok := content["apiVersion"]; ok {
589-
if str, ok := objAPIVersion.(string); ok {
590-
version, err := schema.ParseGroupVersion(str)
591-
if err != nil {
592-
return err
593-
}
594-
kind = kind + "." + version.Version + "." + version.Group
595-
}
596-
}
597-
598-
name := FormatResourceName(options.Kind, metadata.GetName(), options.WithKind)
599-
600-
if _, err := fmt.Fprintf(w, "%s\t%s", name, kind); err != nil {
601-
return err
602-
}
603-
for _, field := range additionalFields {
604-
if value, ok := content[field]; ok {
605-
var formattedValue string
606-
switch typedValue := value.(type) {
607-
case []interface{}:
608-
formattedValue = fmt.Sprintf("%d item(s)", len(typedValue))
609-
default:
610-
formattedValue = fmt.Sprintf("%v", value)
611-
}
612-
if _, err := fmt.Fprintf(w, "\t%s", formattedValue); err != nil {
613-
return err
614-
}
615-
}
616-
}
617-
if _, err := fmt.Fprint(w, AppendLabels(metadata.GetLabels(), options.ColumnLabels)); err != nil {
618-
return err
619-
}
620-
if _, err := fmt.Fprint(w, AppendAllLabels(options.ShowLabels, metadata.GetLabels())); err != nil {
621-
return err
622-
}
623-
624-
return nil
625-
}
626-
627503
func formatLabelHeaders(columnLabels []string) []string {
628504
formHead := make([]string, len(columnLabels))
629505
for i, l := range columnLabels {
@@ -664,42 +540,3 @@ func appendLabelCells(values []interface{}, itemLabels map[string]string, opts P
664540
}
665541
return values
666542
}
667-
668-
// FormatResourceName receives a resource kind, name, and boolean specifying
669-
// whether or not to update the current name to "kind/name"
670-
func FormatResourceName(kind schema.GroupKind, name string, withKind bool) string {
671-
if !withKind || kind.Empty() {
672-
return name
673-
}
674-
675-
return strings.ToLower(kind.String()) + "/" + name
676-
}
677-
678-
func AppendLabels(itemLabels map[string]string, columnLabels []string) string {
679-
var buffer bytes.Buffer
680-
681-
for _, cl := range columnLabels {
682-
buffer.WriteString(fmt.Sprint("\t"))
683-
if il, ok := itemLabels[cl]; ok {
684-
buffer.WriteString(fmt.Sprint(il))
685-
} else {
686-
buffer.WriteString("<none>")
687-
}
688-
}
689-
690-
return buffer.String()
691-
}
692-
693-
// Append all labels to a single column. We need this even when show-labels flag* is
694-
// false, since this adds newline delimiter to the end of each row.
695-
func AppendAllLabels(showLabels bool, itemLabels map[string]string) string {
696-
var buffer bytes.Buffer
697-
698-
if showLabels {
699-
buffer.WriteString(fmt.Sprint("\t"))
700-
buffer.WriteString(labels.FormatLabels(itemLabels))
701-
}
702-
buffer.WriteString("\n")
703-
704-
return buffer.String()
705-
}

pkg/printers/internalversion/printers.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1902,7 +1902,7 @@ func printControllerRevision(obj *apps.ControllerRevision, options printers.Prin
19021902
return nil, err
19031903
}
19041904
gvk := gv.WithKind(controllerRef.Kind)
1905-
controllerName = printers.FormatResourceName(gvk.GroupKind(), controllerRef.Name, withKind)
1905+
controllerName = formatResourceName(gvk.GroupKind(), controllerRef.Name, withKind)
19061906
}
19071907
revision := obj.Revision
19081908
age := translateTimestampSince(obj.CreationTimestamp)
@@ -1922,6 +1922,16 @@ func printControllerRevisionList(list *apps.ControllerRevisionList, options prin
19221922
return rows, nil
19231923
}
19241924

1925+
// formatResourceName receives a resource kind, name, and boolean specifying
1926+
// whether or not to update the current name to "kind/name"
1927+
func formatResourceName(kind schema.GroupKind, name string, withKind bool) string {
1928+
if !withKind || kind.Empty() {
1929+
return name
1930+
}
1931+
1932+
return strings.ToLower(kind.String()) + "/" + name
1933+
}
1934+
19251935
func printResourceQuota(resourceQuota *api.ResourceQuota, options printers.PrintOptions) ([]metav1beta1.TableRow, error) {
19261936
row := metav1beta1.TableRow{
19271937
Object: runtime.RawExtension{Object: resourceQuota},

pkg/printers/internalversion/printers_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func TestFormatResourceName(t *testing.T) {
278278
{schema.GroupKind{Group: "group", Kind: "Kind"}, "name", "kind.group/name"},
279279
}
280280
for _, tt := range tests {
281-
if got := printers.FormatResourceName(tt.kind, tt.name, true); got != tt.want {
281+
if got := formatResourceName(tt.kind, tt.name, true); got != tt.want {
282282
t.Errorf("formatResourceName(%q, %q) = %q, want %q", tt.kind, tt.name, got, tt.want)
283283
}
284284
}

0 commit comments

Comments
 (0)