mirror of https://github.com/k3s-io/k3s
Sort snapshots by time and key in tabwriter output
Fixes snapshot list coming out in non-deterministic order
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
(cherry picked from commit a15b804e00
)
Signed-off-by: Brad Davidson <brad.davidson@rancher.com>
pull/8644/head
parent
61bbad7d9e
commit
8633571a5b
|
@ -7,6 +7,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
"text/tabwriter"
|
||||
"time"
|
||||
|
@ -198,9 +199,23 @@ func list(app *cli.Context, cfg *cmds.Server) error {
|
|||
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
|
||||
defer w.Flush()
|
||||
|
||||
// Sort snapshots by creation time and key
|
||||
sfKeys := make([]string, 0, len(sf))
|
||||
for k := range sf {
|
||||
sfKeys = append(sfKeys, k)
|
||||
}
|
||||
sort.Slice(sfKeys, func(i, j int) bool {
|
||||
iKey := sfKeys[i]
|
||||
jKey := sfKeys[j]
|
||||
if sf[iKey].CreatedAt.Equal(sf[jKey].CreatedAt) {
|
||||
return iKey < jKey
|
||||
}
|
||||
return sf[iKey].CreatedAt.Before(sf[jKey].CreatedAt)
|
||||
})
|
||||
|
||||
fmt.Fprint(w, "Name\tLocation\tSize\tCreated\n")
|
||||
for _, s := range sf {
|
||||
fmt.Fprintf(w, "%s\t%s\t%d\t%s\n", s.Name, s.Location, s.Size, s.CreatedAt.Format(time.RFC3339))
|
||||
for _, k := range sfKeys {
|
||||
fmt.Fprintf(w, "%s\t%s\t%d\t%s\n", sf[k].Name, sf[k].Location, sf[k].Size, sf[k].CreatedAt.Format(time.RFC3339))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue