Merge pull request #614 from Morganamilo/refactormain

Refactor main
This commit is contained in:
Anna 2018-08-15 20:11:00 +01:00 committed by GitHub
commit 0e40d9a3f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 118 additions and 171 deletions

288
main.go
View File

@ -11,30 +11,18 @@ import (
)
func setPaths() error {
if _configHome, set := os.LookupEnv("XDG_CONFIG_HOME"); set {
if _configHome == "" {
return fmt.Errorf("XDG_CONFIG_HOME set but empty")
}
configHome = filepath.Join(_configHome, "yay")
} else if _configHome, set := os.LookupEnv("HOME"); set {
if _configHome == "" {
return fmt.Errorf("HOME set but empty")
}
configHome = filepath.Join(_configHome, ".config/yay")
if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
configHome = filepath.Join(configHome, "yay")
} else if configHome = os.Getenv("HOME"); configHome != "" {
configHome = filepath.Join(configHome, ".config/yay")
} else {
return fmt.Errorf("XDG_CONFIG_HOME and HOME unset")
}
if _cacheHome, set := os.LookupEnv("XDG_CACHE_HOME"); set {
if _cacheHome == "" {
return fmt.Errorf("XDG_CACHE_HOME set but empty")
}
cacheHome = filepath.Join(_cacheHome, "yay")
} else if _cacheHome, set := os.LookupEnv("HOME"); set {
if _cacheHome == "" {
return fmt.Errorf("XDG_CACHE_HOME set but empty")
}
cacheHome = filepath.Join(_cacheHome, ".cache/yay")
if cacheHome = os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
cacheHome = filepath.Join(cacheHome, "yay")
} else if cacheHome = os.Getenv("HOME"); cacheHome != "" {
cacheHome = filepath.Join(cacheHome, ".cache/yay")
} else {
return fmt.Errorf("XDG_CACHE_HOME and HOME unset")
}
@ -45,98 +33,96 @@ func setPaths() error {
return nil
}
func initConfig() (err error) {
defaultSettings(&config)
func initConfig() error {
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) {
err = os.MkdirAll(filepath.Dir(configFile), 0755)
if err != nil {
err = fmt.Errorf("Unable to create config directory:\n%s\n"+
"The error was:\n%s", filepath.Dir(configFile), err)
return
}
// Save the default config if nothing is found
config.saveConfig()
} else {
cfile, errf := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
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
}
}
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)
}
}
return
return nil
}
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)
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)
}
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
return nil
}
func initAlpm() (err error) {
var value string
var exists bool
//var double bool
alpmConf, err = readAlpmConfig(config.PacmanConf)
if err != nil {
err = fmt.Errorf("Unable to read Pacman conf: %s", err)
return
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
}
value, _, exists = cmdArgs.getArg("dbpath", "b")
if exists {
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
}
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
}
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
}
value, _, exists = cmdArgs.getArg("root", "r")
if exists {
if value, _, exists := cmdArgs.getArg("root", "r"); exists {
alpmConf.RootDir = value
}
value, _, exists = cmdArgs.getArg("arch")
if exists {
if value, _, exists := cmdArgs.getArg("arch"); exists {
alpmConf.Architecture = value
}
value, _, exists = cmdArgs.getArg("ignore")
if exists {
if value, _, exists := cmdArgs.getArg("ignore"); exists {
alpmConf.IgnorePkg = append(alpmConf.IgnorePkg, strings.Split(value, ",")...)
}
value, _, exists = cmdArgs.getArg("ignoregroup")
if exists {
if value, _, exists := cmdArgs.getArg("ignoregroup"); exists {
alpmConf.IgnoreGroup = append(alpmConf.IgnoreGroup, strings.Split(value, ",")...)
}
@ -144,23 +130,19 @@ func initAlpm() (err error) {
//current system does not allow duplicate arguments
//but pacman allows multiple cachdirs to be passed
//for now only handle one cache dir
value, _, exists = cmdArgs.getArg("cachdir")
if exists {
if value, _, exists := cmdArgs.getArg("cachdir"); exists {
alpmConf.CacheDir = []string{value}
}
value, _, exists = cmdArgs.getArg("gpgdir")
if exists {
if value, _, exists := cmdArgs.getArg("gpgdir"); exists {
alpmConf.GPGDir = value
}
err = initAlpmHandle()
if err != nil {
return
if err = initAlpmHandle(); err != nil {
return err
}
value, _, _ = cmdArgs.getArg("color")
if value == "always" || value == "auto" {
if value, _, _ := cmdArgs.getArg("color"); value == "always" || value == "auto" {
useColor = true
} else if value == "never" {
useColor = false
@ -168,97 +150,61 @@ func initAlpm() (err error) {
useColor = alpmConf.Options&alpm.ConfColor > 0
}
return
return nil
}
func initAlpmHandle() (err error) {
func initAlpmHandle() error {
var err error
if alpmHandle != nil {
err = alpmHandle.Release()
if err != nil {
if err := alpmHandle.Release(); err != nil {
return err
}
}
alpmHandle, err = alpmConf.CreateHandle()
if err != nil {
err = fmt.Errorf("Unable to CreateHandle: %s", err)
return
if alpmHandle, err = alpmConf.CreateHandle(); err != nil {
return fmt.Errorf("Unable to CreateHandle: %s", err)
}
alpmHandle.SetQuestionCallback(questionCallback)
alpmHandle.SetLogCallback(logCallback)
return
return nil
}
func exitOnError(err error) {
if err != nil {
if str := err.Error(); str != "" {
fmt.Println(str)
}
cleanup()
os.Exit(1)
}
}
func cleanup() int {
if alpmHandle != nil {
if err := alpmHandle.Release(); err != nil {
fmt.Println(err)
return 1
}
}
return 0
}
func main() {
var status int
var err error
if 0 == os.Geteuid() {
fmt.Println("Please avoid running yay as root/sudo.")
}
err = cmdArgs.parseCommandLine()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}
err = setPaths()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}
err = initConfig()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}
cmdArgs.extractYayOptions()
err = initVCS()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}
err = initAlpm()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}
err = handleCmd()
if err != nil {
if err.Error() != "" {
fmt.Println(err)
}
status = 1
goto cleanup
}
cleanup:
//cleanup
//from here on out don't exit if an error occurs
//if we fail to save the configuration
//at least continue on and try clean up other parts
if alpmHandle != nil {
err = alpmHandle.Release()
if err != nil {
fmt.Println(err)
status = 1
}
}
os.Exit(status)
exitOnError(setPaths())
defaultSettings(&config)
exitOnError(initHomeDirs())
exitOnError(initConfig())
exitOnError(cmdArgs.parseCommandLine())
exitOnError(initBuildDir())
exitOnError(initVCS())
exitOnError(initAlpm())
exitOnError(handleCmd())
os.Exit(cleanup())
}

View File

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