func (s *CustomColumnsPrinter) PrintObj(obj runtime.Object, out io.Writer) error { w := tabwriter.NewWriter(out, columnwidth, tabwidth, padding, padding_character, flags) headers := make([]string, len(s.Columns)) for ix := range s.Columns { headers[ix] = s.Columns[ix].Header } fmt.Fprintln(w, strings.Join(headers, "\t")) parsers := make([]*jsonpath.JSONPath, len(s.Columns)) for ix := range s.Columns { parsers[ix] = jsonpath.New(fmt.Sprintf("column%d", ix)) if err := parsers[ix].Parse(s.Columns[ix].FieldSpec); err != nil { return err } } if runtime.IsListType(obj) { objs, err := runtime.ExtractList(obj) if err != nil { return err } for ix := range objs { if err := s.printOneObject(objs[ix], parsers, w); err != nil { return err } } } else { if err := s.printOneObject(obj, parsers, w); err != nil { return err } } return w.Flush() }
func (r *RuntimeSort) Less(i, j int) bool { iObj := r.objs[i] jObj := r.objs[j] parser := jsonpath.New("sorting") parser.Parse(r.field) iValues, err := parser.FindResults(reflect.ValueOf(iObj).Elem().Interface()) if err != nil { glog.Fatalf("Failed to get i values for %#v using %s (%#v)", iObj, r.field, err) } jValues, err := parser.FindResults(reflect.ValueOf(jObj).Elem().Interface()) if err != nil { glog.Fatalf("Failed to get j values for %#v using %s (%v)", jObj, r.field, err) } iField := iValues[0][0] jField := jValues[0][0] less, err := isLess(iField, jField) if err != nil { glog.Fatalf("Field %s in %v is an unsortable type: %s", r.field, iObj, iField.Kind().String()) } return less }
func (s *SortingPrinter) sortObj(obj runtime.Object) error { objs, err := runtime.ExtractList(obj) if err != nil { return err } if len(objs) == 0 { return nil } parser := jsonpath.New("sorting") parser.Parse(s.SortField) values, err := parser.FindResults(reflect.ValueOf(objs[0]).Elem().Interface()) if err != nil { return err } if len(values) == 0 { return fmt.Errorf("couldn't find any field with path: %s", s.SortField) } sorter := &RuntimeSort{ field: s.SortField, objs: objs, } sort.Sort(sorter) runtime.SetList(obj, sorter.objs) return nil }
func (s *SortingPrinter) sortObj(obj runtime.Object) error { objs, err := runtime.ExtractList(obj) if err != nil { return err } if len(objs) == 0 { return nil } parser := jsonpath.New("sorting") parser.Parse(s.SortField) for ix := range objs { item := objs[ix] switch u := item.(type) { case *runtime.Unknown: var err error if objs[ix], err = api.Codec.Decode(u.RawJSON); err != nil { return err } } } values, err := parser.FindResults(reflect.ValueOf(objs[0]).Elem().Interface()) if err != nil { return err } if len(values) == 0 { return fmt.Errorf("couldn't find any field with path: %s", s.SortField) } sorter := &RuntimeSort{ field: s.SortField, objs: objs, } sort.Sort(sorter) runtime.SetList(obj, sorter.objs) return nil }