"io/ioutil"
"os"
"path"
+ "sort"
)
type Tree string
type XSAMeta struct {
XSA int
- SupportedVersions []XenVersion
+ SupportedVersions XenVersionSlice
Trees []Tree
Recipes map[XenVersion]*Recipe
// Not exported
func (m *XSAMeta) NewRecipe(v XenVersion) *Recipe {
var r Recipe
+ // Insert v to SupportedVersions in-order
+ // Lazy version: Add it to the end then sort
+ m.SupportedVersions = append(m.SupportedVersions, v)
+ sort.Sort(m.SupportedVersions)
+
r.xsa = m.XSA
r.XenVersion = v
r.Recipes = make(map[Tree]*TreeRecipe)
xm.Recipes[i].xsa = xm.XSA
}
+ sort.Sort(xm.SupportedVersions)
+
return
}
return err == nil
}
-// a.IsGreaterEqualThan(b) returns a >= b
-func (v XenVersion) IsGreaterEqualThan(v2 XenVersion) bool {
+func (v XenVersion) gt(v2 XenVersion, orEqual bool) bool {
majorA, minorA, err := v.parse()
if err != nil {
fmt.Printf("Parsing: %v\n", err)
return false
}
- if minorA >= minorB {
+ // Now we know majors are equal
+
+ if minorA > minorB {
+ return true
+ }
+
+ if orEqual && minorA == minorB {
return true
}
return false
}
+// a.IsGreaterEqualThan(b) returns a >= b
+func (v XenVersion) IsGreaterEqualThan(v2 XenVersion) bool {
+ return v.gt(v2, true)
+}
+
+// a.IsGreaterEqualThan(b) returns a > b
+func (v XenVersion) IsGreaterThan(v2 XenVersion) bool {
+ return v.gt(v2, false)
+}
+
+func (v XenVersion) PrevVersion() (next XenVersion, err error) {
+ major, minor, err := v.parse()
+ if err != nil {
+ err = fmt.Errorf("Parsing version %v: %v\n", v, err)
+ return
+ }
+
+ if minor > 0 {
+ minor--
+ next = XenVersion(fmt.Sprintf("%d.%d\n", major, minor))
+ return
+ }
+
+ // FIXME If we ever move to 5.x, or need to extend this to
+ // 3.x, we'll have to sort something out here; probably a
+ // table mapping major version -> highest release minor
+ err = fmt.Errorf("Don't know how to roll back major number")
+ return
+}
+
+type XenVersionSlice []XenVersion
+
+// Len is the number of elements in the collection.
+func (vl XenVersionSlice) Len() int {
+ return len(vl)
+}
+
+// Less reports whether the element with
+// index i should sort before the element with index j.
+func (vl XenVersionSlice) Less(i, j int) bool {
+ return vl[i].IsGreaterThan(vl[j])
+}
+
+// Swap swaps the elements with indexes i and j.
+func (vl XenVersionSlice) Swap(i, j int) {
+ tmp := vl[j]
+ vl[j] = vl[i]
+ vl[i] = tmp
+}
+
type XenVersionFull string
func (v XenVersionFull) String() string {
for v := range sm.Versions {
vm := sm.Versions[v]
- xsa.SupportedVersions = append(xsa.SupportedVersions, v)
-
+ // This will add v to xsa.SupportedVersions
r := xsa.NewRecipe(v)
for _, t := range xsa.Trees {
}
if err := r.ApplyBaselines("xsa"); err != nil {
- fmt.Printf("Making recipe baseline for version %v\n", v)
+ fmt.Printf("Making recipe baseline for version %v: %v\n", v, err)
return 1
}