Browse Source

Minor bug fixed in completions. Github updates progressing. Info fetching done

Jguer 8 years ago
parent
commit
5c0650fe5a
2 changed files with 70 additions and 9 deletions
  1. 69 8
      aur/github/github.go
  2. 1 1
      utils.go

+ 69 - 8
aur/github/github.go

@@ -2,17 +2,49 @@ package github
 
 import (
 	"encoding/json"
+	"fmt"
 	"net/http"
+	"os"
 	"strings"
 )
 
-// Branch contains the information of a repository branch
-type Branch struct {
-	Name   string `json:"name"`
-	Commit struct {
-		Sha string `json:"sha"`
-		URL string `json:"url"`
-	} `json:"commit"`
+// branch contains the information of a repository branch
+type branch struct {
+	Name string `json:"name"`
+	SHA  string `json:"sha"`
+	URL  string `json:"url"`
+}
+
+type branches []branch
+
+// Info contains the last commit sha of a repo
+type Info struct {
+	Package string `json:"owner"`
+	Repo    string `json:"Repo"`
+	SHA     string `json:"sha"`
+}
+
+type infos []Info
+
+var savedInfo infos
+
+func init() {
+	path := os.Getenv("HOME") + "/.config/yay/yay_github.json"
+
+	if _, err := os.Stat(path); os.IsNotExist(err) {
+		os.MkdirAll(os.Getenv("HOME")+"/.config/yay/yay_github.json", 0755)
+	}
+
+	file, err := os.Open(path)
+	if err != nil {
+		fmt.Println("error:", err)
+		return
+	}
+	decoder := json.NewDecoder(file)
+	err = decoder.Decode(&savedInfo)
+	if err != nil {
+		fmt.Println("error:", err)
+	}
 }
 
 func parseSource(source string) (owner string, repo string) {
@@ -31,7 +63,22 @@ func parseSource(source string) (owner string, repo string) {
 	return
 }
 
-func branchInfo(owner string, repo string) (newRepo []Branch, err error) {
+func checkUpdates() {
+
+}
+
+func inStore(pkgname string) *Info {
+	for i, e := range savedInfo {
+		if pkgname == e.Package {
+			return &savedInfo[i]
+		}
+	}
+	return nil
+}
+
+// BranchInfo updates saved information
+func BranchInfo(pkgname string, owner string, repo string) (err error) {
+	var newRepo branches
 	url := "https://api.github.com/repos/" + owner + "/" + repo + "/branches"
 	r, err := http.Get(url)
 	if err != nil {
@@ -41,5 +88,19 @@ func branchInfo(owner string, repo string) (newRepo []Branch, err error) {
 
 	json.NewDecoder(r.Body).Decode(newRepo)
 
+	packinfo := inStore(pkgname)
+
+	for _, e := range newRepo {
+		if e.Name == "master" {
+			if packinfo != nil {
+				packinfo.Package = pkgname
+				packinfo.Repo = owner + "/" + repo
+				packinfo.SHA = e.SHA
+			} else {
+				savedInfo = append(savedInfo, Info{Package: pkgname, Repo: owner + "/" + repo, SHA: e.SHA})
+			}
+		}
+	}
+
 	return
 }

+ 1 - 1
utils.go

@@ -47,7 +47,7 @@ func Complete() (err error) {
 	path := os.Getenv("HOME") + "/.cache/yay/aur_" + util.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(os.Getenv("HOME")+"/.cache/yay/aur_"+util.Shell+".cache", 0755)
 
 		out, err := os.Create(path)
 		if err != nil {