Ver código fonte

Reorder main

Split functions out into smaller, more simple chunks. Ensure the correct
order of things.

Before, initConfig() would also create config.BuildDirbefore
the command line flags have been applied, so yay would
try to mkdir what was in the config, ignoring the flag.
morganamilo 6 anos atrás
pai
commit
febccaef3a
2 arquivos alterados com 46 adições e 35 exclusões
  1. 45 35
      main.go
  2. 1 0
      parser.go

+ 45 - 35
main.go

@@ -46,59 +46,67 @@ func setPaths() error {
 }
 
 func initConfig() error {
-	defaultSettings(&config)
+	cfile, err := os.Open(configFile)
+	if !os.IsNotExist(err) && err != nil {
+		return fmt.Errorf("Failed to open config file '%s': %s", configFile, err)
+	}
 
-	if _, err := os.Stat(configFile); os.IsNotExist(err) {
-		if err = os.MkdirAll(filepath.Dir(configFile), 0755); err != nil {
-			return fmt.Errorf("Unable to create config directory:\n%s\n"+
-				"The error was:\n%s", filepath.Dir(configFile), err)
+	defer cfile.Close()
+	if !os.IsNotExist(err) {
+		decoder := json.NewDecoder(cfile)
+		if err = decoder.Decode(&config); err != nil {
+			return fmt.Errorf("Failed to read config '%s': %s", configFile, err)
 		}
-		// Save the default config if nothing is found
-		return config.saveConfig()
-	} else if err != nil {
-		return err
 	}
 
-	cfile, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
-	if err != nil {
-		return fmt.Errorf("Error reading config: %s\n", err)
-	}
+	return nil
+}
 
-	defer cfile.Close()
-	decoder := json.NewDecoder(cfile)
-	if err = decoder.Decode(&config); err != nil {
-		return fmt.Errorf("Error reading config: %s",
-			err)
+func initVCS() error {
+	vfile, err := os.Open(vcsFile)
+	if !os.IsNotExist(err) && err != nil {
+		return fmt.Errorf("Failed to open vcs file '%s': %s", vcsFile, err)
 	}
 
-	if _, err = os.Stat(config.BuildDir); os.IsNotExist(err) {
-		if err = os.MkdirAll(config.BuildDir, 0755); err != nil {
-			return fmt.Errorf("Unable to create BuildDir directory:\n%s\n"+
-				"The error was:\n%s", config.BuildDir, err)
+	defer vfile.Close()
+	if !os.IsNotExist(err) {
+		decoder := json.NewDecoder(vfile)
+		if err = decoder.Decode(&savedInfo); err != nil {
+			return fmt.Errorf("Failed to read vcs '%s': %s", vcsFile, err)
 		}
 	}
 
-	return err
+	return nil
 }
 
-func initVCS() error {
-	if _, err := os.Stat(vcsFile); os.IsNotExist(err) {
-		if err = os.MkdirAll(filepath.Dir(vcsFile), 0755); err != nil {
-			return fmt.Errorf("Unable to create vcs directory:\n%s\n"+
-				"The error was:\n%s", filepath.Dir(configFile), err)
+func initHomeDirs() error {
+	if _, err := os.Stat(configHome); os.IsNotExist(err) {
+		if err = os.MkdirAll(configHome, 0755); err != nil {
+			return fmt.Errorf("Failed to create config directory '%s': %s", configHome, err)
 		}
 	} else if err != nil {
 		return err
 	}
 
-	vfile, err := os.OpenFile(vcsFile, os.O_RDONLY|os.O_CREATE, 0644)
-	if err != nil {
+	if _, err := os.Stat(cacheHome); os.IsNotExist(err) {
+		if err = os.MkdirAll(cacheHome, 0755); err != nil {
+			return fmt.Errorf("Failed to create cache directory '%s': %s", cacheHome, err)
+		}
+	} else if err != nil {
 		return err
 	}
 
-	defer vfile.Close()
-	decoder := json.NewDecoder(vfile)
-	_ = decoder.Decode(&savedInfo)
+	return nil
+}
+
+func initBuildDir() error {
+	if _, err := os.Stat(config.BuildDir); os.IsNotExist(err) {
+		if err = os.MkdirAll(config.BuildDir, 0755); err != nil {
+			return fmt.Errorf("Failed to create BuildDir directory '%s': %s", config.BuildDir, err)
+		}
+	} else if err != nil {
+		return err
+	}
 
 	return nil
 }
@@ -201,10 +209,12 @@ func main() {
 		fmt.Println("Please avoid running yay as root/sudo.")
 	}
 
-	exitOnError(cmdArgs.parseCommandLine())
 	exitOnError(setPaths())
+	defaultSettings(&config)
+	exitOnError(initHomeDirs())
 	exitOnError(initConfig())
-	cmdArgs.extractYayOptions()
+	exitOnError(cmdArgs.parseCommandLine())
+	exitOnError(initBuildDir())
 	exitOnError(initVCS())
 	exitOnError(initAlpm())
 	exitOnError(handleCmd())

+ 1 - 0
parser.go

@@ -853,6 +853,7 @@ func (parser *arguments) parseCommandLine() (err error) {
 		os.Stdin = file
 	}
 
+	cmdArgs.extractYayOptions()
 	return
 }