]> xenbits.xensource.com Git - people/gdunlap/xsatool.git/commitdiff
main: Always attempt load the config
authorGeorge Dunlap <george.dunlap@citrix.com>
Mon, 30 Oct 2017 17:34:23 +0000 (17:34 +0000)
committerGeorge Dunlap <george.dunlap@citrix.com>
Mon, 30 Oct 2017 17:34:23 +0000 (17:34 +0000)
Rather than loading most of the time in main() (and failing if it fails),
and then also loading the config in "repo init" (and tolerating its absence),
always attempt to load the config, and only fail if we end up missing bits of
information that we need.

This will set the stage for unifying the configuration and the
command-line options.

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

diff --git a/main.go b/main.go
index 92885b5e496a61a88bec0f728ce3968bb5f6c045..a7056be8be1398becfc8db77e70342b956c15582 100644 (file)
--- a/main.go
+++ b/main.go
@@ -105,7 +105,6 @@ func XsaMain(args []string) int {
        var err error
        var xsanum int
 
-       loadConfig := true
        loadGlobal := false
        loadXSA := false
        checkXSARepos := true
@@ -149,7 +148,6 @@ func XsaMain(args []string) int {
                        switch cmd {
                        case "init":
                                main = MainRepoInit
-                               loadConfig = false
                        case "update":
                                main = MainRepoUpdate
                        case "x":
@@ -176,11 +174,10 @@ func XsaMain(args []string) int {
                }
        }
 
-       if loadConfig {
-               if err := G.config.Tool.Load(); err != nil {
-                       fmt.Printf("Error finding config: %v\n", err)
-                       return 1
-               }
+       // Always load the config
+       if err := G.config.Tool.Load(); err != nil && !os.IsNotExist(err) {
+               fmt.Printf("Error loading config: %v\n", err)
+               return 1
        }
 
        if loadGlobal {
diff --git a/meta.go b/meta.go
index d1d87f9bffd80719f734ac3321ec4b851b3fc495..b87a50c6c7066edf7f07230a3aa6039a721164cb 100644 (file)
--- a/meta.go
+++ b/meta.go
@@ -82,6 +82,7 @@ type ToolConfig struct {
        }
        // Not saved
        rootDir string
+       loaded bool
 }
 
 const ConfigFileName = ".xsatool"
@@ -126,6 +127,11 @@ func (tc *ToolConfig) Load() (err error) {
        }
 
        err = json.Unmarshal(b, tc)
+
+       if err == nil {
+               tc.loaded = true
+       }
+       
        return
 }
 
diff --git a/repo.go b/repo.go
index 084ca9b45a6f8a9e06df33da946fc98d31ca8f15..4c0f9995e8a88fe669413e91638688acb5bd55ed 100644 (file)
--- a/repo.go
+++ b/repo.go
@@ -32,14 +32,9 @@ func MainRepoInit(unused *XSAMeta, args []string) (ret int) {
                return 1
        }
 
-       if err := G.config.Tool.Load(); err != nil {
-               if os.IsNotExist(err) {
-                       fmt.Printf("Initializing new tool config")
-                       G.config.Tool.Init()
-               } else {
-                       fmt.Printf("Error opening config file: %v\n", err)
-                       return 1
-               }
+       if !G.config.Tool.loaded {
+               fmt.Printf("Initializing new tool config\n")
+               G.config.Tool.Init()
        } else {
                fmt.Printf("Using existing tool config in %s\n", G.config.Tool.rootDir)
        }