Fix error when cacheHome does not exist

And refactor initYay out into smaller parts
This commit is contained in:
morganamilo 2018-03-07 05:20:10 +00:00
parent 251d31c970
commit 74ef0beaed
No known key found for this signature in database
GPG key ID: 6FE9E7996B0B082E
2 changed files with 43 additions and 28 deletions

65
cmd.go
View file

@ -62,14 +62,7 @@ Yay specific options:
If no operation is provided -Y will be assumed`) If no operation is provided -Y will be assumed`)
} }
func initYay() (err error) { func initPaths() {
var configHome string // configHome handles config directory home
var cacheHome string // cacheHome handles cache home
if 0 == os.Geteuid() {
fmt.Println("Please avoid running yay as root/sudo.")
}
if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" { if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
if info, err := os.Stat(configHome); err == nil && info.IsDir() { if info, err := os.Stat(configHome); err == nil && info.IsDir() {
configHome = configHome + "/yay" configHome = configHome + "/yay"
@ -93,12 +86,9 @@ func initYay() (err error) {
configFile = configHome + "/" + configFileName configFile = configHome + "/" + configFileName
vcsFile = cacheHome + "/" + vcsFileName vcsFile = cacheHome + "/" + vcsFileName
completionFile = cacheHome + "/" + completionFilePrefix completionFile = cacheHome + "/" + completionFilePrefix
}
//////////////// func initConfig() (err error) {
// yay 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) err = os.MkdirAll(filepath.Dir(configFile), 0755)
if err != nil { if err != nil {
@ -124,24 +114,31 @@ func initYay() (err error) {
} }
} }
///////////////// defaultSettings(&config)
// vcs config // return
//////////////// }
vfile, err := os.OpenFile(vcsFile, os.O_RDONLY|os.O_CREATE, 0644)
if err == nil { func initVCS() (err error) {
defer vfile.Close() if _, err = os.Stat(vcsFile); os.IsNotExist(err) {
decoder := json.NewDecoder(vfile) err = os.MkdirAll(filepath.Dir(vcsFile), 0755)
_ = decoder.Decode(&savedInfo) 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 return
} }
func initAlpm() (err error) { func initAlpm() (err error) {
/////////////////
// alpm config //
/////////////////
var value string var value string
var exists bool var exists bool
//var double bool //var double bool
@ -199,6 +196,10 @@ func main() {
var status int var status int
var err error var err error
if 0 == os.Geteuid() {
fmt.Println("Please avoid running yay as root/sudo.")
}
err = cmdArgs.parseCommandLine() err = cmdArgs.parseCommandLine()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -206,13 +207,23 @@ func main() {
goto cleanup goto cleanup
} }
err = initYay() initPaths()
err = initConfig()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
status = 1 status = 1
goto cleanup goto cleanup
} }
err = initVCS()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}
err = initAlpm() err = initAlpm()
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -227,8 +238,6 @@ func main() {
goto cleanup goto cleanup
} }
//ive used a goto here
//i think its the best way to do this sort of thing
cleanup: cleanup:
//cleanup //cleanup
//from here on out dont exit if an error occurs //from here on out dont exit if an error occurs

View file

@ -55,6 +55,12 @@ const completionFilePrefix string = "aur_"
// baseURL givers the AUR default address. // baseURL givers the AUR default address.
const baseURL string = "https://aur.archlinux.org" const baseURL string = "https://aur.archlinux.org"
// configHome handles config directory home
var configHome string
// cacheHome handles cache home
var cacheHome string
// savedInfo holds the current vcs info // savedInfo holds the current vcs info
var savedInfo vcsInfo var savedInfo vcsInfo