mirror of
https://github.com/aptly-dev/aptly.git
synced 2026-05-08 22:30:41 +00:00
Merge branch 'sort_snapshots_time' of https://github.com/simonaquino/aptly into simonaquino-sort_snapshots_time
This commit is contained in:
+56
-12
@@ -7,6 +7,49 @@ import (
|
|||||||
"sort"
|
"sort"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Snapshot sorting methods
|
||||||
|
const (
|
||||||
|
SortName = iota
|
||||||
|
SortTime
|
||||||
|
)
|
||||||
|
|
||||||
|
type snapshotListToSort struct {
|
||||||
|
list []*deb.Snapshot
|
||||||
|
sortMethod int
|
||||||
|
}
|
||||||
|
|
||||||
|
func ParseSortMethod(sortMethod_string string) int {
|
||||||
|
|
||||||
|
switch sortMethod_string {
|
||||||
|
case "time", "Time":
|
||||||
|
return SortTime
|
||||||
|
case "name", "Name":
|
||||||
|
return SortName
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Errorf("Sorting method \"%s\" unknown. Defaulting to '-sort=name'", sortMethod_string)
|
||||||
|
return SortName
|
||||||
|
}
|
||||||
|
|
||||||
|
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:
|
||||||
|
sL := []string{s.list[i].Name, s.list[j].Name}
|
||||||
|
return sort.StringsAreSorted(sL)
|
||||||
|
case SortTime:
|
||||||
|
return s.list[i].CreatedAt.Before(s.list[j].CreatedAt)
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
@@ -15,32 +58,32 @@ func aptlySnapshotList(cmd *commander.Command, args []string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
|
raw := cmd.Flag.Lookup("raw").Value.Get().(bool)
|
||||||
|
sortMethod_string := cmd.Flag.Lookup("sort").Value.Get().(string)
|
||||||
|
|
||||||
snapshots := make([]string, context.CollectionFactory().SnapshotCollection().Len())
|
snapshotsToSort := &snapshotListToSort{}
|
||||||
|
snapshotsToSort.list = make([]*deb.Snapshot, context.CollectionFactory().SnapshotCollection().Len())
|
||||||
|
snapshotsToSort.sortMethod = ParseSortMethod(sortMethod_string)
|
||||||
|
|
||||||
i := 0
|
i := 0
|
||||||
context.CollectionFactory().SnapshotCollection().ForEach(func(snapshot *deb.Snapshot) error {
|
context.CollectionFactory().SnapshotCollection().ForEach(func(snapshot *deb.Snapshot) error {
|
||||||
if raw {
|
snapshotsToSort.list[i] = snapshot
|
||||||
snapshots[i] = snapshot.Name
|
|
||||||
} else {
|
|
||||||
snapshots[i] = snapshot.String()
|
|
||||||
}
|
|
||||||
i++
|
i++
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
sort.Strings(snapshots)
|
sort.Sort(snapshotsToSort)
|
||||||
|
|
||||||
if raw {
|
if raw {
|
||||||
for _, snapshot := range snapshots {
|
for _, snapshot := range snapshotsToSort.list {
|
||||||
fmt.Printf("%s\n", snapshot)
|
fmt.Printf("%s\n", snapshot.Name)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if len(snapshots) > 0 {
|
if len(snapshotsToSort.list) > 0 {
|
||||||
fmt.Printf("List of snapshots:\n")
|
fmt.Printf("List of snapshots:\n")
|
||||||
|
|
||||||
for _, snapshot := range snapshots {
|
for _, snapshot := range snapshotsToSort.list {
|
||||||
fmt.Printf(" * %s\n", snapshot)
|
fmt.Printf(" * %s\n", snapshot.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
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")
|
||||||
@@ -67,6 +110,7 @@ Example:
|
|||||||
}
|
}
|
||||||
|
|
||||||
cmd.Flag.Bool("raw", false, "display list in machine-readable format")
|
cmd.Flag.Bool("raw", false, "display list in machine-readable format")
|
||||||
|
cmd.Flag.String("sort", "name", "display list in 'name' or creation 'time' order")
|
||||||
|
|
||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,5 @@
|
|||||||
|
snap2
|
||||||
|
snap1
|
||||||
|
snap3
|
||||||
|
snap4
|
||||||
|
snap5
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
List of snapshots:
|
||||||
|
* [snap2]: Snapshot from mirror [wheezy-contrib]: http://mirror.yandex.ru/debian/ wheezy
|
||||||
|
* [snap1]: Snapshot from mirror [wheezy-main]: http://mirror.yandex.ru/debian/ wheezy
|
||||||
|
* [snap3]: Merged from sources: 'snap1', 'snap2'
|
||||||
|
* [snap4]: Pulled into 'snap1' with 'snap2' as source, pull request was: 'mame unrar'
|
||||||
|
* [snap5]: Snapshot from local repo [local-repo]
|
||||||
|
|
||||||
|
To get more information about snapshot, run `aptly snapshot show <name>`.
|
||||||
@@ -47,3 +47,36 @@ class ListSnapshot4Test(BaseTest):
|
|||||||
list snapshots: raw empty list
|
list snapshots: raw empty list
|
||||||
"""
|
"""
|
||||||
runCmd = "aptly snapshot -raw list"
|
runCmd = "aptly snapshot -raw list"
|
||||||
|
|
||||||
|
|
||||||
|
class ListSnapshot5Test(BaseTest):
|
||||||
|
"""
|
||||||
|
list snapshots: raw regular list sorted by time
|
||||||
|
"""
|
||||||
|
fixtureDB = True
|
||||||
|
fixtureCmds = [
|
||||||
|
"aptly snapshot create snap2 from mirror wheezy-main",
|
||||||
|
"aptly snapshot create snap1 from mirror wheezy-contrib",
|
||||||
|
"aptly snapshot merge snap3 snap1 snap2",
|
||||||
|
"aptly snapshot pull snap1 snap2 snap4 mame unrar",
|
||||||
|
"aptly repo create local-repo",
|
||||||
|
"aptly repo add local-repo ${files}",
|
||||||
|
"aptly snapshot create snap5 from repo local-repo",
|
||||||
|
]
|
||||||
|
runCmd = "aptly -raw -sort=time snapshot list"
|
||||||
|
|
||||||
|
class ListSnapshot6Test(BaseTest):
|
||||||
|
"""
|
||||||
|
list snapshots: regular list sorted by time
|
||||||
|
"""
|
||||||
|
fixtureDB = True
|
||||||
|
fixtureCmds = [
|
||||||
|
"aptly snapshot create snap2 from mirror wheezy-contrib",
|
||||||
|
"aptly snapshot create snap1 from mirror wheezy-main",
|
||||||
|
"aptly snapshot merge snap3 snap1 snap2",
|
||||||
|
"aptly snapshot pull snap1 snap2 snap4 mame unrar",
|
||||||
|
"aptly repo create local-repo",
|
||||||
|
"aptly repo add local-repo ${files}",
|
||||||
|
"aptly snapshot create snap5 from repo local-repo",
|
||||||
|
]
|
||||||
|
runCmd = "aptly -sort=time snapshot list"
|
||||||
|
|||||||
Reference in New Issue
Block a user