Commit ea6b82a4 authored by Matt Butcher's avatar Matt Butcher

feat(tiller): sort list in tiller, not helm

parent 62cb6ce4
......@@ -53,12 +53,27 @@ service ReleaseService {
}
// ListReleasesRequest requests a list of releases.
//
// Releases can be retrieved in chunks by setting limit and offset.
//
// Releases can be sorted according to a few pre-determined sort stategies.
message ListReleasesRequest {
// The maximum number of releases to be returned
// Limit is the maximum number of releases to be returned.
int64 limit = 1;
// The zero-based offset at which the returned release list begins
// Offset is the zero-based offset at which the returned release list begins.
int64 offset = 2;
// SortBy is the sort field that the ListReleases server should sort data before returning.
ListSort.SortBy sort_by = 3;
}
message ListSort{
enum SortBy {
UNKNOWN = 0;
NAME = 1;
LAST_RELEASED = 2;
}
}
// ListReleasesResponse is a list of releases.
......
......@@ -2,11 +2,11 @@ package main
import (
"fmt"
"sort"
"github.com/gosuri/uitable"
"github.com/kubernetes/helm/pkg/helm"
"github.com/kubernetes/helm/pkg/proto/hapi/release"
"github.com/kubernetes/helm/pkg/proto/hapi/services"
"github.com/kubernetes/helm/pkg/timeconv"
"github.com/spf13/cobra"
)
......@@ -45,7 +45,12 @@ func listCmd(cmd *cobra.Command, args []string) error {
fmt.Println("TODO: Implement filter.")
}
res, err := helm.ListReleases(listMax, listOffset)
sortBy := services.ListSort_NAME
if listByDate {
sortBy = services.ListSort_LAST_RELEASED
}
res, err := helm.ListReleases(listMax, listOffset, sortBy)
if err != nil {
return prettyError(err)
}
......@@ -55,12 +60,6 @@ func listCmd(cmd *cobra.Command, args []string) error {
fmt.Println("Not all values were fetched.")
}
if listByDate {
sort.Sort(byDate(rels))
} else {
sort.Sort(byName(rels))
}
// Purty output, ya'll
if listLong {
return formatList(rels)
......@@ -85,26 +84,3 @@ func formatList(rels []*release.Release) error {
return nil
}
// byName implements the sort.Interface for []*release.Release.
type byName []*release.Release
func (r byName) Len() int {
return len(r)
}
func (r byName) Swap(p, q int) {
r[p], r[q] = r[q], r[p]
}
func (r byName) Less(i, j int) bool {
return r[i].Name < r[j].Name
}
type byDate []*release.Release
func (r byDate) Len() int { return len(r) }
func (r byDate) Swap(p, q int) {
r[p], r[q] = r[q], r[p]
}
func (r byDate) Less(p, q int) bool {
return r[p].Info.LastDeployed.Seconds < r[q].Info.LastDeployed.Seconds
}
......@@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"log"
"sort"
"github.com/kubernetes/helm/cmd/tiller/environment"
"github.com/kubernetes/helm/pkg/proto/hapi/release"
......@@ -49,6 +50,17 @@ func (s *releaseServer) ListReleases(req *services.ListReleasesRequest, stream s
total := int64(len(rels))
if req.SortBy != services.ListSort_UNKNOWN {
log.Printf("Sorting by %q", req.SortBy)
}
switch req.SortBy {
case services.ListSort_NAME:
sort.Sort(byName(rels))
case services.ListSort_LAST_RELEASED:
sort.Sort(byDate(rels))
}
l := int64(len(rels))
if req.Offset > 0 {
if req.Offset >= l {
......@@ -224,3 +236,26 @@ func (s *releaseServer) UninstallRelease(c ctx.Context, req *services.UninstallR
res := services.UninstallReleaseResponse{Release: rel}
return &res, nil
}
// byName implements the sort.Interface for []*release.Release.
type byName []*release.Release
func (r byName) Len() int {
return len(r)
}
func (r byName) Swap(p, q int) {
r[p], r[q] = r[q], r[p]
}
func (r byName) Less(i, j int) bool {
return r[i].Name < r[j].Name
}
type byDate []*release.Release
func (r byDate) Len() int { return len(r) }
func (r byDate) Swap(p, q int) {
r[p], r[q] = r[q], r[p]
}
func (r byDate) Less(p, q int) bool {
return r[p].Info.LastDeployed.Seconds < r[q].Info.LastDeployed.Seconds
}
......@@ -13,7 +13,7 @@ var Config = &config{
}
// ListReleases lists the current releases.
func ListReleases(limit, offset int) (*services.ListReleasesResponse, error) {
func ListReleases(limit, offset int, sort services.ListSort_SortBy) (*services.ListReleasesResponse, error) {
c := Config.client()
if err := c.dial(); err != nil {
return nil, err
......@@ -23,6 +23,7 @@ func ListReleases(limit, offset int) (*services.ListReleasesResponse, error) {
req := &services.ListReleasesRequest{
Limit: int64(limit),
Offset: int64(offset),
SortBy: sort,
}
cli, err := c.impl.ListReleases(context.TODO(), req, c.cfg.CallOpts()...)
if err != nil {
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment