Skip to content

Commit

Permalink
Added support for XDG_CACHE_HOME, fixes relating to file location not…
Browse files Browse the repository at this point in the history
… covered in the commit relating to Jguer#50.
  • Loading branch information
Jguer committed Oct 18, 2017
1 parent 893196b commit b016bf6
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 53 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@ Yay was created with a few objectives in mind and based on the design of [yaourt

### Changelog

#### 2.196
- XDG_CONFIG_HOME support
- XDG_CACHE_HOME support

#### 2.165
- Upgrade list now allows skipping upgrade install.
- Upgrade list now allows skipping upgrade install

#### 2.159
- Qstats now warns about packages not available in AUR.
- Qstats now warns about packages not available in AUR

#### 2.152
- Fetching backend changed to Mikkel Oscar's [Aur](https://github.com/mikkeloscar/aur)
Expand Down
95 changes: 60 additions & 35 deletions cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,71 +40,97 @@ func usage() {
}

func init() {
defaultSettings(&config)

var configHome string // configHome handles config directory home
var cacheHome string // cacheHome handles cache home
var err error
if dir := os.Getenv("XDG_CONFIG_HOME"); dir != "" {
if info, err := os.Stat(dir); err == nil && info.IsDir() == true {
configfile = os.Getenv("XDG_CONFIG_HOME") + "/yay/config.json"

if 0 == os.Geteuid() {
fmt.Println("Please avoid running yay as root/sudo.")
}

if configHome = os.Getenv("XDG_CONFIG_HOME"); configHome != "" {
if info, err := os.Stat(configHome); err == nil && info.IsDir() == true {
configHome = configHome + "/yay"
} else {
configfile = os.Getenv("HOME") + "/.config/yay/config.json"
configHome = os.Getenv("HOME") + "/.config/yay"
}
} else {
configfile = os.Getenv("HOME") + "/.config/yay/config.json"
configHome = os.Getenv("HOME") + "/.config/yay"
}

if _, err = os.Stat(configfile); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(configfile), 0755)
if cacheHome = os.Getenv("XDG_CACHE_HOME"); cacheHome != "" {
if info, err := os.Stat(cacheHome); err == nil && info.IsDir() == true {
cacheHome = cacheHome + "/yay"
} else {
cacheHome = os.Getenv("HOME") + "/.cache/yay"
}
} else {
cacheHome = os.Getenv("HOME") + "/.config/yay"
}

configFile = configHome + "/config.json"
vcsFile = configHome + "/yay_vcs.json"
completionFile = cacheHome + "/aur_"

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

if _, err = os.Stat(configFile); os.IsNotExist(err) {
err = os.MkdirAll(filepath.Dir(configFile), 0700)
if err != nil {
fmt.Println("Unable to create config directory:", filepath.Dir(configfile), err)
fmt.Println("Unable to create config directory:", filepath.Dir(configFile), err)
os.Exit(2)
}
// Save the default config if nothing is found
config.saveConfig()
} else {
file, err := os.Open(configfile)
file, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
fmt.Println("Error reading config:", err)
} else {
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
fmt.Println("Loading default Settings\nError reading config:", err)
fmt.Println("Loading default Settings.\nError reading config:", err)
defaultSettings(&config)
}
}
}

AlpmConf, err = readAlpmConfig(config.PacmanConf)
if err != nil {
fmt.Println("Unable to read Pacman conf", err)
os.Exit(1)
}

AlpmHandle, err = AlpmConf.CreateHandle()
if err != nil {
fmt.Println("Unable to CreateHandle", err)
os.Exit(1)
}

/////////////////
// vcs config //
////////////////
updated = false
configfile = os.Getenv("HOME") + "/.config/yay/yay_vcs.json"

if _, err := os.Stat(configfile); os.IsNotExist(err) {
_ = os.MkdirAll(os.Getenv("HOME")+"/.config/yay", 0755)
return
}

file, err := os.Open(configfile)
file, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
fmt.Println("error:", err)
return
}
defer file.Close()
decoder := json.NewDecoder(file)
err = decoder.Decode(&savedInfo)
if err != nil {
fmt.Println("error:", err)
}

/////////////////
// alpm config //
/////////////////
AlpmConf, err = readAlpmConfig(config.PacmanConf)
if err != nil {
fmt.Println("Unable to read Pacman conf", err)
os.Exit(1)
}

AlpmHandle, err = AlpmConf.CreateHandle()
if err != nil {
fmt.Println("Unable to CreateHandle", err)
os.Exit(1)
}
}

func parser() (op string, options []string, packages []string, changedConfig bool, err error) {
Expand Down Expand Up @@ -339,11 +365,10 @@ func numberMenu(pkgS []string, flags []string) (err error) {

// Complete provides completion info for shells
func complete() (err error) {
path := os.Getenv("HOME") + "/.cache/yay/aur_" + config.Shell + ".cache"
path := completionFile + config.Shell + ".cache"

if info, err := os.Stat(path); os.IsNotExist(err) || time.Since(info.ModTime()).Hours() > 48 {
os.MkdirAll(os.Getenv("HOME")+"/.cache/yay/", 0755)

os.MkdirAll(filepath.Dir(completionFile), 0700)
out, err := os.Create(path)
if err != nil {
return err
Expand All @@ -358,7 +383,7 @@ func complete() (err error) {
return err
}

in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0600)
if err != nil {
return err
}
Expand Down
11 changes: 9 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,14 @@ var specialDBsauce = false

var savedInfo infos

var configfile string
// configfile holds yay config file path.
var configFile string

// vcsfile holds yay vcs info file path.
var vcsFile string

//completion file
var completionFile string

// Updated returns if database has been updated
var updated bool
Expand Down Expand Up @@ -80,7 +87,7 @@ func readAlpmConfig(pacmanconf string) (conf alpm.PacmanConfig, err error) {
func (config *Configuration) saveConfig() error {
config.NoConfirm = false
marshalledinfo, _ := json.MarshalIndent(config, "", "\t")
in, err := os.OpenFile(configfile, os.O_RDWR|os.O_CREATE, 0644)
in, err := os.OpenFile(configFile, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
return err
}
Expand Down
23 changes: 23 additions & 0 deletions upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@ loop:
return
}

// checkUpdates returns list of outdated packages
func checkUpdates(remoteNames []string) (toUpdate []string) {
for _, e := range savedInfo {
if e.needsUpdate() {
if _, ok := foreign[e.Package]; ok {
toUpdate = append(toUpdate, e.Package)
} else {
removeVCSPackage([]string{e.Package})
}
}
}
return
}

func checkIgnored(name string) {
for

}

// upAUR gathers foreign packages and checks if they have new versions.
// Output: Upgrade type package list.
func upAUR(remote []alpm.Package, remoteNames []string) (toUpgrade upSlice, err error) {
Expand All @@ -151,6 +170,10 @@ func upAUR(remote []alpm.Package, remoteNames []string) (toUpgrade upSlice, err
packageC := make(chan upgrade)
done := make(chan bool)

if config.Devel {

}

for i := len(remote); i != 0; i = j {
//Split requests so AUR RPC doesn't get mad at us.
j = i - config.RequestSplitN
Expand Down
15 changes: 1 addition & 14 deletions vcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,6 @@ func (info *Info) needsUpdate() bool {
return false
}

// checkUpdates returns list of outdated packages
func checkUpdates(foreign map[string]alpm.Package) (toUpdate []string) {
for _, e := range savedInfo {
if e.needsUpdate() {
if _, ok := foreign[e.Package]; ok {
toUpdate = append(toUpdate, e.Package)
} else {
removeVCSPackage([]string{e.Package})
}
}
}
return
}

func inStore(pkgName string) *Info {
for i, e := range savedInfo {
Expand Down Expand Up @@ -148,7 +135,7 @@ func saveVCSInfo() error {
if err != nil || string(marshalledinfo) == "null" {
return err
}
in, err := os.OpenFile(configfile, os.O_RDWR|os.O_CREATE, 0755)
in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
Expand Down

0 comments on commit b016bf6

Please sign in to comment.