From 40f1743f4204ffa029a04e3a956340b33f11d5d8 Mon Sep 17 00:00:00 2001 From: Jguer Date: Sun, 9 Apr 2017 11:48:03 +0100 Subject: [PATCH] #15 plan implemented with 48h. To test --- actions.go | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 56 insertions(+), 3 deletions(-) diff --git a/actions.go b/actions.go index 5a2dd3a9..4834321f 100644 --- a/actions.go +++ b/actions.go @@ -10,6 +10,7 @@ import ( "os/exec" "strconv" "strings" + "time" aur "github.com/jguer/yay/aur" pac "github.com/jguer/yay/pacman" @@ -328,14 +329,66 @@ func GetPkgbuild(pkg string) (err error) { return } -func Complete() (err error) { - // Get the data +func createAURList(path string) (err error) { + os.MkdirAll(os.Getenv("HOME")+"/.cache/yay", 0755) + + out, err := os.Create(path) + if err != nil { + return err + } + defer out.Close() + resp, err := http.Get("https://aur.archlinux.org/packages.gz") if err != nil { return err } + defer resp.Body.Close() - _, err = io.Copy(os.Stdout, resp.Body) + _, err = io.Copy(out, resp.Body) + if err != nil { + return err + } + + return nil +} + +func updateAURList(out io.Writer) (err error) { + resp, err := http.Get("https://aur.archlinux.org/packages.gz") + if err != nil { + return err + } + + defer resp.Body.Close() + _, err = io.Copy(out, resp.Body) + if err != nil { + return err + } + + return nil + +} + +// Complete provides completion info for shells +func Complete() (err error) { + path := os.Getenv("HOME") + "/.cache/yay/aur.cache" + + if _, err := os.Stat(path); os.IsNotExist(err) { + err = createAURList(path) + } + + in, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755) + defer in.Close() + if err != nil { + return err + } + + info, err := in.Stat() + if time.Since(info.ModTime()).Hours() > 48 { + err = updateAURList(in) + } + + // Get the data + _, err = io.Copy(os.Stdout, in) if err != nil { return err }