mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-06 22:18:28 +00:00
Move command line snapshot sorting to common snapshot code
This commit is contained in:
+10
-65
@@ -4,49 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"github.com/smira/aptly/deb"
|
"github.com/smira/aptly/deb"
|
||||||
"github.com/smira/commander"
|
"github.com/smira/commander"
|
||||||
"sort"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Snapshot sorting methods
|
|
||||||
const (
|
|
||||||
SortName = iota
|
|
||||||
SortTime
|
|
||||||
)
|
|
||||||
|
|
||||||
type snapshotListToSort struct {
|
|
||||||
list []*deb.Snapshot
|
|
||||||
sortMethod int
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseSortMethod(sortMethod string) (int, error) {
|
|
||||||
switch sortMethod {
|
|
||||||
case "time", "Time":
|
|
||||||
return SortTime, nil
|
|
||||||
case "name", "Name":
|
|
||||||
return SortName, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return -1, fmt.Errorf("sorting method \"%s\" unknown", sortMethod)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s snapshotListToSort) Swap(i, j int) {
|
|
||||||
s.list[i], s.list[j] = s.list[j], s.list[i]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s snapshotListToSort) Less(i, j int) bool {
|
|
||||||
switch s.sortMethod {
|
|
||||||
case SortName:
|
|
||||||
return s.list[i].Name < s.list[j].Name
|
|
||||||
case SortTime:
|
|
||||||
return s.list[i].CreatedAt.Before(s.list[j].CreatedAt)
|
|
||||||
}
|
|
||||||
panic("unknown sort method")
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s snapshotListToSort) Len() int {
|
|
||||||
return len(s.list)
|
|
||||||
}
|
|
||||||
|
|
||||||
func aptlySnapshotList(cmd *commander.Command, args []string) error {
|
func aptlySnapshotList(cmd *commander.Command, args []string) error {
|
||||||
var err error
|
var err error
|
||||||
if len(args) != 0 {
|
if len(args) != 0 {
|
||||||
@@ -57,44 +16,30 @@ func aptlySnapshotList(cmd *commander.Command, args []string) error {
|
|||||||
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
|
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
|
||||||
sortMethodString := cmd.Flag.Lookup("sort").Value.Get().(string)
|
sortMethodString := cmd.Flag.Lookup("sort").Value.Get().(string)
|
||||||
|
|
||||||
snapshotsToSort := &snapshotListToSort{}
|
collection := context.CollectionFactory().SnapshotCollection()
|
||||||
snapshotsToSort.list = make([]*deb.Snapshot, context.CollectionFactory().SnapshotCollection().Len())
|
collection.Sort(sortMethodString)
|
||||||
snapshotsToSort.sortMethod, err = parseSortMethod(sortMethodString)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
i := 0
|
|
||||||
context.CollectionFactory().SnapshotCollection().ForEach(func(snapshot *deb.Snapshot) error {
|
|
||||||
snapshotsToSort.list[i] = snapshot
|
|
||||||
i++
|
|
||||||
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
context.CloseDatabase()
|
|
||||||
|
|
||||||
sort.Sort(snapshotsToSort)
|
|
||||||
|
|
||||||
if raw {
|
if raw {
|
||||||
for _, snapshot := range snapshotsToSort.list {
|
collection.ForEach(func(snapshot *deb.Snapshot) error {
|
||||||
fmt.Printf("%s\n", snapshot.Name)
|
fmt.Printf("%s\n", snapshot.Name)
|
||||||
}
|
return nil
|
||||||
|
})
|
||||||
} else {
|
} else {
|
||||||
if len(snapshotsToSort.list) > 0 {
|
if collection.Len() > 0 {
|
||||||
fmt.Printf("List of snapshots:\n")
|
fmt.Printf("List of snapshots:\n")
|
||||||
|
|
||||||
for _, snapshot := range snapshotsToSort.list {
|
collection.ForEach(func(snapshot *deb.Snapshot) error {
|
||||||
fmt.Printf(" * %s\n", snapshot.String())
|
fmt.Printf(" * %s\n", snapshot.String())
|
||||||
}
|
return nil
|
||||||
|
})
|
||||||
|
|
||||||
fmt.Printf("\nTo get more information about snapshot, run `aptly snapshot show <name>`.\n")
|
fmt.Printf("\nTo get more information about snapshot, run `aptly snapshot show <name>`.\n")
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("\nNo snapshots found, create one with `aptly snapshot create...`.\n")
|
fmt.Printf("\nNo snapshots found, create one with `aptly snapshot create...`.\n")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return err
|
|
||||||
|
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeCmdSnapshotList() *commander.Command {
|
func makeCmdSnapshotList() *commander.Command {
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
"github.com/smira/aptly/utils"
|
"github.com/smira/aptly/utils"
|
||||||
"github.com/ugorji/go/codec"
|
"github.com/ugorji/go/codec"
|
||||||
"log"
|
"log"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -327,3 +328,58 @@ func (collection *SnapshotCollection) Drop(snapshot *Snapshot) error {
|
|||||||
|
|
||||||
return collection.db.Delete(snapshot.RefKey())
|
return collection.db.Delete(snapshot.RefKey())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Snapshot sorting methods
|
||||||
|
const (
|
||||||
|
SortName = iota
|
||||||
|
SortTime
|
||||||
|
)
|
||||||
|
|
||||||
|
type snapshotListToSort struct {
|
||||||
|
list []*Snapshot
|
||||||
|
sortMethod int
|
||||||
|
}
|
||||||
|
|
||||||
|
func parseSortMethod(sortMethod string) (int, error) {
|
||||||
|
switch sortMethod {
|
||||||
|
case "time", "Time":
|
||||||
|
return SortTime, nil
|
||||||
|
case "name", "Name":
|
||||||
|
return SortName, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return -1, fmt.Errorf("sorting method \"%s\" unknown", sortMethod)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s snapshotListToSort) Swap(i, j int) {
|
||||||
|
s.list[i], s.list[j] = s.list[j], s.list[i]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s snapshotListToSort) Less(i, j int) bool {
|
||||||
|
switch s.sortMethod {
|
||||||
|
case SortName:
|
||||||
|
return s.list[i].Name < s.list[j].Name
|
||||||
|
case SortTime:
|
||||||
|
return s.list[i].CreatedAt.Before(s.list[j].CreatedAt)
|
||||||
|
}
|
||||||
|
panic("unknown sort method")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s snapshotListToSort) Len() int {
|
||||||
|
return len(s.list)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (collection *SnapshotCollection) Sort(sortMethodString string) error {
|
||||||
|
var err error
|
||||||
|
snapshotsToSort := &snapshotListToSort{}
|
||||||
|
snapshotsToSort.list = collection.list
|
||||||
|
snapshotsToSort.sortMethod, err = parseSortMethod(sortMethodString)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Sort(snapshotsToSort)
|
||||||
|
collection.list = snapshotsToSort.list
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user