]> xenbits.xensource.com Git - people/gdunlap/xsatool/commitdiff
Add XSA.Load() and Save(), and use it rather than hardcoding
authorGeorge Dunlap <george.dunlap@citrix.com>
Fri, 28 Apr 2017 10:30:03 +0000 (11:30 +0100)
committerGeorge Dunlap <george.dunlap@citrix.com>
Fri, 28 Apr 2017 10:30:03 +0000 (11:30 +0100)
Signed-off-by: George Dunlap <george.dunlap@citrix.com>
main.go
meta.go

diff --git a/main.go b/main.go
index e93c46df1d0b16646e5af67d0f831325e4085582..f455d157c784f4d3f58dc433c524dda28e778269 100644 (file)
--- a/main.go
+++ b/main.go
@@ -128,76 +128,12 @@ func main() {
        }
 
        if loadXSA {
-               switch xsanum {
-               case 206:
-                       xsa = XSAMeta{
-                               XSA: 206,
-                               SupportedVersions: []XenVersion{"master", "4.8", "4.7", "4.6", "4.5", "4.4"},
-                               Recipes: []Recipe{
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("master"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa206-unstable/*.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.8.0"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa206-4.8/*.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.7.2"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa206-4.7/*.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.6.5"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa206-4.6/*.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.5.5"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa206-4.5/*.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.4.4"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa206-4.4/*.patch"} },
-                                       },
-                               },
-                       }
-               case 212:
-                       xsa = XSAMeta{
-                               XSA: 212,
-                               SupportedVersions: []XenVersion{"master", "4.8", "4.7", "4.6", "4.5", "4.4"},
-                               Recipes: []Recipe{
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("master"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa212-unstable.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.8.0"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa212-unstable.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.7.2"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa212-unstable.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.6.5"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa212-unstable.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.5.5"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa212-unstable.patch"} },
-                                       },
-                                       Recipe{
-                                               XenVersionFull: XenVersionFull("4.4.4"),
-                                               Xen: TreeRecipe{ Patches: []string{"xsa212-unstable.patch"} },
-                                       },
-                               },
-                       }
-
-               default:
-                       fmt.Printf("No recipe for xsa %d\n", xsanum)
-                       os.Exit(-1)
+               err = xsa.Load(xsanum)
+               if err != nil {
+                       fmt.Printf("Loading xsa%d metadata: %v\n",
+                               err)
+                       os.Exit(1)
                }
-
-               xsa.Normalize()
        }
        
        os.Exit(main(xsanum, args))
diff --git a/meta.go b/meta.go
index 2de292053349e70ee406b09fd0b3f9a7ff5ae7a2..a13157099d10ddbdfea083627d26e3bf19062d46 100644 (file)
--- a/meta.go
+++ b/meta.go
@@ -161,6 +161,8 @@ type XSAMeta struct {
        XSA               int
        SupportedVersions []XenVersion
        Recipes           []Recipe
+       // Not exported
+       dirty             bool
 }
 
 func (m *XSAMeta) GetRecipe(v XenVersion) (*Recipe) {
@@ -177,3 +179,55 @@ func (m *XSAMeta) Normalize() {
                m.Recipes[i].xsa = m.XSA
        }
 }
+
+func (xm *XSAMeta) Load(xsanum int) (err error) {
+       if config.Tool.GetXSAPath() == "" {
+               return fmt.Errorf("Tool config not loaded")
+       }
+
+       filename := fmt.Sprintf("%s/xsa%d.meta",
+               config.Tool.GetXSAPath(), xsanum)
+       
+       b, err := ioutil.ReadFile(filename)
+       if err != nil {
+               if os.IsNotExist(err) {
+                       // No problem if the file doesn't exist; just use the null config
+                       xm.XSA = xsanum
+                       return nil
+               } else {
+                       return fmt.Errorf("Opening global metadata file %s: %v\n", filename, err)
+               }
+       }
+
+       err = json.Unmarshal(b, xm)
+       if err != nil {
+               return fmt.Errorf("Unmarshalling metadata: %v\n", err)
+       }
+
+       if xm.XSA != xsanum {
+               return fmt.Errorf("Invalid input: Expecting xsa %d, got %d\n",
+                       xsanum, xm.XSA)
+       }
+       return
+}
+
+func (xm *XSAMeta) Save() (err error) {
+       if config.Tool.GetXSAPath() == "" {
+               return fmt.Errorf("Tool config not loaded")
+       }
+
+       b, err := json.MarshalIndent(*xm, "", "  ")
+       if err != nil {
+               return fmt.Errorf("Marshalling config: %v", err)
+       }
+
+       filename := config.Tool.GetXSAPath() + "/" + GlobalMetaFilename
+       err = ioutil.WriteFile(filename, b, 0666)
+       if err != nil {
+               return fmt.Errorf("Writing config: %v", err)
+       }
+
+       xm.dirty = false
+       
+       return
+}