Skip to content

Commit

Permalink
Support sort-by timestamp in kubectl get
Browse files Browse the repository at this point in the history
  • Loading branch information
janetkuo committed May 19, 2016
1 parent 28333a0 commit a178b5d
Showing 1 changed file with 4 additions and 26 deletions.
30 changes: 4 additions & 26 deletions pkg/kubectl/sorting_printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,10 @@ func isLess(i, j reflect.Value) (bool, error) {
case reflect.Ptr:
return isLess(i.Elem(), j.Elem())
case reflect.Struct:
// special case handling
lessFuncList := []structLessFunc{timeLess}
if ok, less := structLess(i, j, lessFuncList); ok {
return less, nil
// sort unversioned.Time
in := i.Interface()
if t, ok := in.(unversioned.Time); ok {
return t.Before(j.Interface().(unversioned.Time)), nil
}
// fallback to the fields comparision
for idx := 0; idx < i.NumField(); idx++ {
Expand All @@ -183,28 +183,6 @@ func isLess(i, j reflect.Value) (bool, error) {
}
}

// structLessFunc checks whether i and j could be compared(the first return value),
// and if it could, return whether i is less than j(the second return value)
type structLessFunc func(i, j reflect.Value) (bool, bool)

// structLess returns whether i and j could be compared with the given function list
func structLess(i, j reflect.Value, lessFuncList []structLessFunc) (bool, bool) {
for _, lessFunc := range lessFuncList {
if ok, less := lessFunc(i, j); ok {
return ok, less
}
}
return false, false
}

// compare two unversioned.Time values.
func timeLess(i, j reflect.Value) (bool, bool) {
if i.Type() != reflect.TypeOf(unversioned.Unix(0, 0)) {
return false, false
}
return true, i.MethodByName("Before").Call([]reflect.Value{j})[0].Bool()
}

func (r *RuntimeSort) Less(i, j int) bool {
iObj := r.objs[i]
jObj := r.objs[j]
Expand Down

0 comments on commit a178b5d

Please sign in to comment.