Skip to content

Commit

Permalink
Merge pull request Jguer#210 from Morganamilo/fix#209
Browse files Browse the repository at this point in the history
Fix error when cacheHome does not exist
  • Loading branch information
Morganamilo authored Mar 7, 2018
2 parents 251d31c + 74ef0be commit e436017
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
65 changes: 37 additions & 28 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,7 @@ Yay specific options:
If no operation is provided -Y will be assumed`)
}

func initYay() (err error) {
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.")
}

func initPaths() {
if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
if info, err := os.Stat(configHome); err == nil && info.IsDir() {
configHome = configHome + "/yay"
Expand All @@ -93,12 +86,9 @@ func initYay() (err error) {
configFile = configHome + "/" + configFileName
vcsFile = cacheHome + "/" + vcsFileName
completionFile = cacheHome + "/" + completionFilePrefix
}

////////////////
// yay config //
////////////////
defaultSettings(&config)

func initConfig() (err error) {
if _, err = os.Stat(configFile); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(configFile), 0755)
if err != nil {
Expand All @@ -124,24 +114,31 @@ func initYay() (err error) {
}
}

/////////////////
// vcs config //
////////////////
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)
defaultSettings(&config)
return
}

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) {
/////////////////
// alpm config //
/////////////////

var value string
var exists bool
//var double bool
Expand Down Expand Up @@ -199,18 +196,32 @@ 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 = initYay()
initPaths()

err = initConfig()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup
}

err = initVCS()
if err != nil {
fmt.Println(err)
status = 1
goto cleanup

}

err = initAlpm()
Expand All @@ -227,8 +238,6 @@ func main() {
goto cleanup
}

//ive used a goto here
//i think its the best way to do this sort of thing
cleanup:
//cleanup
//from here on out dont exit if an error occurs
Expand Down
6 changes: 6 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ const completionFilePrefix string = "aur_"
// baseURL givers the AUR default address.
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
var savedInfo vcsInfo

Expand Down

0 comments on commit e436017

Please sign in to comment.