]> xenbits.xensource.com Git - people/gdunlap/xsatool.git/commitdiff
Fix config load
authorGeorge Dunlap <george.dunlap@citrix.com>
Fri, 5 May 2017 13:52:26 +0000 (14:52 +0100)
committerGeorge Dunlap <george.dunlap@citrix.com>
Fri, 5 May 2017 13:52:26 +0000 (14:52 +0100)
Have ToolConfig.Load return an error which returns true for
os.IsNotExist().

When the config isn't present, initialize it.

Signed-off-by: George Dunlap <george.dunlap@citrix.com>
meta.go
repo.go

diff --git a/meta.go b/meta.go
index 8fd7c4fe8f29ef2063c226de1ad1ede3fd536756..33e8091bf7e85a6d623b6efde29f67cc6324bc02 100644 (file)
--- a/meta.go
+++ b/meta.go
@@ -95,7 +95,8 @@ func (tc *ToolConfig) Load() (err error) {
 
                        rootDir = path.Clean(rootDir+"/..")
                        if rootDir == "/" {
-                               return fmt.Errorf("Couldn't find config file\n")
+                               // Should return 'true' for os.IsNotExist(err)
+                               return err
                        }
                }
        } else {
@@ -110,6 +111,16 @@ func (tc *ToolConfig) Load() (err error) {
        return
 }
 
+func (tc *ToolConfig) Init() (err error) {
+       var rootDir string
+       rootDir, err = os.Getwd()
+       if err != nil {
+               return fmt.Errorf("Trying to get wd: %v\n", err)
+       }
+       tc.rootDir = rootDir
+       return
+}
+
 func (tc *ToolConfig) Save() (err error) {
        if tc.rootDir == "" {
                return fmt.Errorf("Uninitialized root dir")
diff --git a/repo.go b/repo.go
index d9d55e5b6baa6fa659f7d4477a7518d94f5483db..9b4eb1e3ce5c9f15307265543b331d241a801bf6 100644 (file)
--- a/repo.go
+++ b/repo.go
@@ -28,9 +28,13 @@ func MainRepoInit(xsanum int, args []string) (ret int) {
        }
 
        fmt.Printf("Trying local config...\n")
-       if err := config.Tool.Load(); err != nil && !os.IsNotExist(err) {
-               fmt.Printf("Error opening config file: %v\n", err)
-               return 1
+       if err := config.Tool.Load(); err != nil {
+               if os.IsNotExist(err) {
+                       config.Tool.Init()
+               } else {
+                       fmt.Printf("Error opening config file: %v\n", err)
+                       return 1
+               }
        }
 
        setDef := func(s *string, d string) {
@@ -42,14 +46,20 @@ func MainRepoInit(xsanum int, args []string) (ret int) {
                }
        }
 
-       defaultPath := [TreeMax]string{
+       defaultPath := map[Tree]string{
                TreeXen: "xen.git",
                TreeQemuU: "qemu-xen.git",
                TreeQemuT: "qemu-xen-traditional.git",
                TreeXSA: "xsa.git",
        }
        setDefaultPath := func(tree Tree) (err error) {
-               setDef(&config.Tool.Paths[tree], defaultPath[tree])
+               _, prs := config.Tool.Paths[tree]
+               if prs {
+                       fmt.Printf("Already set to %s\n", config.Tool.Paths[tree])
+               } else {
+                       config.Tool.Paths[tree] = defaultPath[tree]
+                       fmt.Printf("Setting default: %s\n", defaultPath[tree])
+               }
                return
        }
 
@@ -74,14 +84,14 @@ func MainRepoInit(xsanum int, args []string) (ret int) {
                return true, nil
        }
 
-       defaultUrl := [TreeMax]string {
+       defaultUrl := map[Tree]string {
                TreeXen: "git://xenbits.xenproject.org/xen.git",
                TreeQemuU: "git://xenbits.xenproject.org/qemu-xen.git",
                TreeQemuT: "git://xenbits.xenproject.org/qemu-xen-traditional.git",
                TreeXSA: "xenbits.xen.org:/home/xensec/git/xsa.git",
        }
 
-       cloneXen := func(target string, origin string, t Tree) (xr XenRepo, err error) {
+       cloneXen := func(target string, origin string, t Tree) (xr *XenRepo, err error) {
                doClone, err := checkDir(target)
 
                if doClone {