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.
This commit is contained in:
morganamilo 2018-08-12 02:40:43 +01:00
parent 2fac9c036f
commit febccaef3a
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E
2 changed files with 49 additions and 38 deletions

86
main.go
View file

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

View file

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