yay/aur/vcs/github.go

155 lines
2.9 KiB
Go
Raw Normal View History

2017-05-01 01:23:03 +00:00
package github
2017-05-01 01:34:40 +00:00
import (
"encoding/json"
"fmt"
2017-05-01 01:34:40 +00:00
"net/http"
"os"
2017-05-01 01:34:40 +00:00
"strings"
)
2017-05-01 01:23:03 +00:00
// 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 {
2017-05-02 10:50:11 +00:00
Package string `json:"pkgname"`
URL string `json:"url"`
SHA string `json:"sha"`
}
type infos []Info
var savedInfo infos
2017-05-02 10:50:11 +00:00
var configfile string
func init() {
2017-05-02 10:50:11 +00:00
configfile = os.Getenv("HOME") + "/.config/yay/yay_vcs.json"
2017-05-02 10:50:11 +00:00
if _, err := os.Stat(configfile); os.IsNotExist(err) {
_ = os.MkdirAll(os.Getenv("HOME")+"/.config/yay", 0755)
}
2017-05-02 10:50:11 +00:00
file, err := os.Open(configfile)
if err != nil {
fmt.Println("error:", err)
return
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&savedInfo)
if err != nil {
fmt.Println("error:", err)
}
2017-05-01 01:23:03 +00:00
}
2017-05-02 10:50:11 +00:00
// ParseSource returns owner and repo from source
func ParseSource(source string) (owner string, repo string) {
2017-05-01 01:23:03 +00:00
split := strings.Split(source, "github.com/")
if len(split) > 1 {
secondSplit := strings.Split(split[1], "/")
if len(secondSplit) > 1 {
owner = secondSplit[0]
thirdSplit := strings.Split(secondSplit[1], ".git")
if len(thirdSplit) > 0 {
repo = thirdSplit[0]
}
}
}
return
}
2017-05-01 01:34:40 +00:00
2017-05-02 10:50:11 +00:00
func (info *Info) needsUpdate() bool {
var newRepo branches
r, err := http.Get(info.URL)
if err != nil {
fmt.Println(err)
return false
}
defer r.Body.Close()
2017-05-02 10:50:11 +00:00
err = json.NewDecoder(r.Body).Decode(&newRepo)
if err != nil {
fmt.Println(err)
return false
}
for _, e := range newRepo {
if e.Name == "master" {
if e.SHA != info.SHA {
return true
} else {
return false
}
}
}
return false
}
func CheckUpdates() (toUpdate []string) {
for _, e := range savedInfo {
if e.needsUpdate() {
toUpdate = append(toUpdate, e.Package)
}
}
return
}
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
2017-05-01 01:34:40 +00:00
url := "https://api.github.com/repos/" + owner + "/" + repo + "/branches"
r, err := http.Get(url)
if err != nil {
return
}
defer r.Body.Close()
2017-05-02 10:50:11 +00:00
_ = json.NewDecoder(r.Body).Decode(&newRepo)
2017-05-01 01:34:40 +00:00
packinfo := inStore(pkgname)
for _, e := range newRepo {
if e.Name == "master" {
if packinfo != nil {
packinfo.Package = pkgname
2017-05-02 10:50:11 +00:00
packinfo.URL = url
packinfo.SHA = e.SHA
} else {
2017-05-02 10:50:11 +00:00
savedInfo = append(savedInfo, Info{Package: pkgname, URL: url, SHA: e.SHA})
}
}
}
2017-05-01 01:34:40 +00:00
return
}
2017-05-02 10:50:11 +00:00
func SaveBranchInfo() error {
marshalledinfo, _ := json.Marshal(savedInfo)
in, err := os.OpenFile(configfile, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return err
}
defer in.Close()
_, err = in.Write(marshalledinfo)
if err != nil {
return err
}
err = in.Sync()
return err
}