Merge branch 'lebauce-snapshot-api'

This commit is contained in:
Andrey Smirnov
2015-02-06 20:18:52 +03:00
16 changed files with 831 additions and 111 deletions
+16
View File
@@ -1,6 +1,7 @@
package deb
import (
"encoding/json"
"fmt"
"github.com/smira/aptly/aptly"
"github.com/smira/aptly/utils"
@@ -38,6 +39,11 @@ type Package struct {
collection *PackageCollection
}
// Check interface
var (
_ json.Marshaler = &Package{}
)
// NewPackageFromControlFile creates Package from parsed Debian control file
func NewPackageFromControlFile(input Stanza) *Package {
result := &Package{
@@ -205,6 +211,16 @@ func (p *Package) String() string {
return fmt.Sprintf("%s_%s_%s", p.Name, p.Version, p.Architecture)
}
// MarshalJSON implements json.Marshaller interface
func (p *Package) MarshalJSON() ([]byte, error) {
stanza := p.Stanza()
stanza["FilesHash"] = fmt.Sprintf("%08x", p.FilesHash)
stanza["Key"] = string(p.Key(""))
stanza["ShortKey"] = string(p.ShortKey(""))
return json.Marshal(stanza)
}
// GetField returns fields from package
func (p *Package) GetField(name string) string {
switch name {
+23
View File
@@ -2,6 +2,8 @@ package deb
import (
"bytes"
"encoding/json"
"github.com/AlekSi/pointer"
"github.com/ugorji/go/codec"
"sort"
)
@@ -154,6 +156,27 @@ type PackageDiff struct {
Left, Right *Package
}
// Check interface
var (
_ json.Marshaler = PackageDiff{}
)
// MarshalJSON implements json.Marshaler interface
func (d PackageDiff) MarshalJSON() ([]byte, error) {
serialized := struct {
Left, Right *string
}{}
if d.Left != nil {
serialized.Left = pointer.ToString(string(d.Left.Key("")))
}
if d.Right != nil {
serialized.Right = pointer.ToString(string(d.Right.Key("")))
}
return json.Marshal(serialized)
}
// PackageDiffs is a list of PackageDiff records
type PackageDiffs []PackageDiff
+69
View File
@@ -9,6 +9,7 @@ import (
"github.com/smira/aptly/utils"
"github.com/ugorji/go/codec"
"log"
"sort"
"strings"
"sync"
"time"
@@ -296,6 +297,22 @@ func (collection *SnapshotCollection) ForEach(handler func(*Snapshot) error) err
return err
}
func (collection *SnapshotCollection) ForEachSorted(sortMethod string, handler func(*Snapshot) error) error {
sorter, err := newSnapshotSorter(sortMethod, collection)
if err != nil {
return err
}
for _, i := range sorter.list {
err = handler(collection.list[i])
if err != nil {
return err
}
}
return nil
}
// Len returns number of snapshots in collection
// ForEach runs method for each snapshot
func (collection *SnapshotCollection) Len() int {
@@ -327,3 +344,55 @@ func (collection *SnapshotCollection) Drop(snapshot *Snapshot) error {
return collection.db.Delete(snapshot.RefKey())
}
// Snapshot sorting methods
const (
SortName = iota
SortTime
)
type snapshotSorter struct {
list []int
collection *SnapshotCollection
sortMethod int
}
func newSnapshotSorter(sortMethod string, collection *SnapshotCollection) (*snapshotSorter, error) {
s := &snapshotSorter{collection: collection}
switch sortMethod {
case "time", "Time":
s.sortMethod = SortTime
case "name", "Name":
s.sortMethod = SortName
default:
return nil, fmt.Errorf("sorting method \"%s\" unknown", sortMethod)
}
s.list = make([]int, len(collection.list))
for i := range s.list {
s.list[i] = i
}
sort.Sort(s)
return s, nil
}
func (s *snapshotSorter) Swap(i, j int) {
s.list[i], s.list[j] = s.list[j], s.list[i]
}
func (s *snapshotSorter) Less(i, j int) bool {
switch s.sortMethod {
case SortName:
return s.collection.list[s.list[i]].Name < s.collection.list[s.list[j]].Name
case SortTime:
return s.collection.list[s.list[i]].CreatedAt.Before(s.collection.list[s.list[j]].CreatedAt)
}
panic("unknown sort method")
}
func (s *snapshotSorter) Len() int {
return len(s.list)
}