Refactor main.go to use one liner if err checks

This commit is contained in:
morganamilo 2018-08-02 21:36:42 +01:00
parent 1739a88be2
commit 2fac9c036f
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E

161
main.go
View file

@ -45,98 +45,88 @@ func setPaths() error {
return nil return nil
} }
func initConfig() (err error) { func initConfig() error {
defaultSettings(&config) defaultSettings(&config)
if _, err = os.Stat(configFile); os.IsNotExist(err) { if _, err := os.Stat(configFile); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(configFile), 0755) if err = os.MkdirAll(filepath.Dir(configFile), 0755); err != nil {
if err != nil { return fmt.Errorf("Unable to create config directory:\n%s\n"+
err = fmt.Errorf("Unable to create config directory:\n%s\n"+
"The error was:\n%s", filepath.Dir(configFile), err) "The error was:\n%s", filepath.Dir(configFile), err)
return
} }
// Save the default config if nothing is found // Save the default config if nothing is found
config.saveConfig() return config.saveConfig()
} else { } else if err != nil {
cfile, errf := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644) return err
if errf != nil {
fmt.Printf("Error reading config: %s\n", err)
} else {
defer cfile.Close()
decoder := json.NewDecoder(cfile)
err = decoder.Decode(&config)
if err != nil {
fmt.Println("Loading default Settings.\nError reading config:",
err)
defaultSettings(&config)
}
if _, err = os.Stat(config.BuildDir); os.IsNotExist(err) {
err = os.MkdirAll(config.BuildDir, 0755)
if err != nil {
err = fmt.Errorf("Unable to create BuildDir directory:\n%s\n"+
"The error was:\n%s", config.BuildDir, err)
return
}
}
}
} }
return cfile, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
}
func initVCS() (err error) {
if _, err = os.Stat(vcsFile); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(vcsFile), 0755)
if err != nil {
err = fmt.Errorf("Unable to create vcs directory:\n%s\n"+
"The error was:\n%s", filepath.Dir(configFile), err)
return
}
} else {
vfile, err := os.OpenFile(vcsFile, os.O_RDONLY|os.O_CREATE, 0644)
if err == nil {
defer vfile.Close()
decoder := json.NewDecoder(vfile)
_ = decoder.Decode(&savedInfo)
}
}
return
}
func initAlpm() (err error) {
var value string
var exists bool
//var double bool
alpmConf, err = readAlpmConfig(config.PacmanConf)
if err != nil { if err != nil {
err = fmt.Errorf("Unable to read Pacman conf: %s", err) return fmt.Errorf("Error reading config: %s\n", err)
return
} }
value, _, exists = cmdArgs.getArg("dbpath", "b") defer cfile.Close()
if exists { decoder := json.NewDecoder(cfile)
if err = decoder.Decode(&config); err != nil {
return fmt.Errorf("Error reading config: %s",
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
}
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)
}
} else if err != nil {
return err
}
vfile, err := os.OpenFile(vcsFile, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
defer vfile.Close()
decoder := json.NewDecoder(vfile)
_ = decoder.Decode(&savedInfo)
return nil
}
func initAlpm() error {
var err error
if alpmConf, err = readAlpmConfig(config.PacmanConf); err != nil {
return fmt.Errorf("Unable to read Pacman conf: %s", err)
}
if value, _, exists := cmdArgs.getArg("dbpath", "b"); exists {
alpmConf.DBPath = value alpmConf.DBPath = value
} }
value, _, exists = cmdArgs.getArg("root", "r") if value, _, exists := cmdArgs.getArg("root", "r"); exists {
if exists {
alpmConf.RootDir = value alpmConf.RootDir = value
} }
value, _, exists = cmdArgs.getArg("arch") if value, _, exists := cmdArgs.getArg("arch"); exists {
if exists {
alpmConf.Architecture = value alpmConf.Architecture = value
} }
value, _, exists = cmdArgs.getArg("ignore") if value, _, exists := cmdArgs.getArg("ignore"); exists {
if exists {
alpmConf.IgnorePkg = append(alpmConf.IgnorePkg, strings.Split(value, ",")...) alpmConf.IgnorePkg = append(alpmConf.IgnorePkg, strings.Split(value, ",")...)
} }
value, _, exists = cmdArgs.getArg("ignoregroup") if value, _, exists := cmdArgs.getArg("ignoregroup"); exists {
if exists {
alpmConf.IgnoreGroup = append(alpmConf.IgnoreGroup, strings.Split(value, ",")...) alpmConf.IgnoreGroup = append(alpmConf.IgnoreGroup, strings.Split(value, ",")...)
} }
@ -144,23 +134,19 @@ func initAlpm() (err error) {
//current system does not allow duplicate arguments //current system does not allow duplicate arguments
//but pacman allows multiple cachdirs to be passed //but pacman allows multiple cachdirs to be passed
//for now only handle one cache dir //for now only handle one cache dir
value, _, exists = cmdArgs.getArg("cachdir") if value, _, exists := cmdArgs.getArg("cachdir"); exists {
if exists {
alpmConf.CacheDir = []string{value} alpmConf.CacheDir = []string{value}
} }
value, _, exists = cmdArgs.getArg("gpgdir") if value, _, exists := cmdArgs.getArg("gpgdir"); exists {
if exists {
alpmConf.GPGDir = value alpmConf.GPGDir = value
} }
err = initAlpmHandle() if err = initAlpmHandle(); err != nil {
if err != nil { return err
return
} }
value, _, _ = cmdArgs.getArg("color") if value, _, _ := cmdArgs.getArg("color"); value == "always" || value == "auto" {
if value == "always" || value == "auto" {
useColor = true useColor = true
} else if value == "never" { } else if value == "never" {
useColor = false useColor = false
@ -168,26 +154,25 @@ func initAlpm() (err error) {
useColor = alpmConf.Options&alpm.ConfColor > 0 useColor = alpmConf.Options&alpm.ConfColor > 0
} }
return return nil
} }
func initAlpmHandle() (err error) { func initAlpmHandle() error {
var err error
if alpmHandle != nil { if alpmHandle != nil {
err = alpmHandle.Release() if err := alpmHandle.Release(); err != nil {
if err != nil {
return err return err
} }
} }
alpmHandle, err = alpmConf.CreateHandle() if alpmHandle, err = alpmConf.CreateHandle(); err != nil {
if err != nil { return fmt.Errorf("Unable to CreateHandle: %s", err)
err = fmt.Errorf("Unable to CreateHandle: %s", err)
return
} }
alpmHandle.SetQuestionCallback(questionCallback) alpmHandle.SetQuestionCallback(questionCallback)
alpmHandle.SetLogCallback(logCallback) alpmHandle.SetLogCallback(logCallback)
return return nil
} }
func exitOnError(err error) { func exitOnError(err error) {