2017-08-02 17:24:03 +00:00
|
|
|
package main
|
2017-05-01 01:23:03 +00:00
|
|
|
|
2017-05-01 01:34:40 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
2017-05-02 00:06:16 +00:00
|
|
|
"fmt"
|
2017-05-01 01:34:40 +00:00
|
|
|
"net/http"
|
2017-05-02 00:06:16 +00:00
|
|
|
"os"
|
2017-05-01 01:34:40 +00:00
|
|
|
"strings"
|
2017-05-16 15:53:15 +00:00
|
|
|
|
|
|
|
alpm "github.com/jguer/go-alpm"
|
2017-08-02 17:24:03 +00:00
|
|
|
"github.com/jguer/yay/pacman"
|
2017-05-01 01:34:40 +00:00
|
|
|
)
|
2017-05-01 01:23:03 +00:00
|
|
|
|
2017-05-02 00:06:16 +00:00
|
|
|
// branch contains the information of a repository branch
|
|
|
|
type branch struct {
|
2017-05-09 01:09:43 +00:00
|
|
|
Name string `json:"name"`
|
|
|
|
Commit struct {
|
|
|
|
SHA string `json:"sha"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
} `json:"commit"`
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"`
|
2017-05-02 00:06:16 +00:00
|
|
|
SHA string `json:"sha"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type infos []Info
|
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
// CreateDevelDB forces yay to create a DB of the existing development packages
|
|
|
|
func createDevelDB() error {
|
|
|
|
foreign, err := pacman.ForeignPackages()
|
2017-05-02 00:06:16 +00:00
|
|
|
if err != nil {
|
2017-08-02 17:24:03 +00:00
|
|
|
return err
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
2017-08-02 17:24:03 +00:00
|
|
|
|
|
|
|
keys := make([]string, len(foreign))
|
|
|
|
i := 0
|
|
|
|
for k := range foreign {
|
|
|
|
keys[i] = k
|
|
|
|
i++
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
2017-08-02 17:24:03 +00:00
|
|
|
|
|
|
|
config.NoConfirm = true
|
|
|
|
specialDBsauce = true
|
|
|
|
err = Install(keys, nil)
|
|
|
|
return err
|
2017-05-01 01:23:03 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 10:50:11 +00:00
|
|
|
// ParseSource returns owner and repo from source
|
2017-08-02 17:24:03 +00:00
|
|
|
func parseSource(source string) (owner string, repo string) {
|
2017-05-09 01:09:43 +00:00
|
|
|
if !(strings.Contains(source, "git://") ||
|
|
|
|
strings.Contains(source, ".git") ||
|
|
|
|
strings.Contains(source, "git+https://")) {
|
|
|
|
return
|
|
|
|
}
|
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 00:06:16 +00:00
|
|
|
|
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" {
|
2017-05-09 01:09:43 +00:00
|
|
|
if e.Commit.SHA != info.SHA {
|
2017-05-02 10:50:11 +00:00
|
|
|
return true
|
|
|
|
}
|
2017-08-02 17:24:03 +00:00
|
|
|
return false
|
2017-05-02 10:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-05-16 15:53:15 +00:00
|
|
|
// CheckUpdates returns list of outdated packages
|
2017-08-02 17:24:03 +00:00
|
|
|
func checkUpdates(foreign map[string]alpm.Package) (toUpdate []string) {
|
2017-05-02 10:50:11 +00:00
|
|
|
for _, e := range savedInfo {
|
|
|
|
if e.needsUpdate() {
|
2017-05-16 15:53:15 +00:00
|
|
|
if _, ok := foreign[e.Package]; ok {
|
|
|
|
toUpdate = append(toUpdate, e.Package)
|
|
|
|
} else {
|
|
|
|
RemovePackage([]string{e.Package})
|
|
|
|
}
|
2017-05-02 10:50:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 20:50:39 +00:00
|
|
|
func inStore(pkgName string) *Info {
|
2017-05-02 00:06:16 +00:00
|
|
|
for i, e := range savedInfo {
|
2017-06-11 20:50:39 +00:00
|
|
|
if pkgName == e.Package {
|
2017-05-02 00:06:16 +00:00
|
|
|
return &savedInfo[i]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BranchInfo updates saved information
|
2017-08-02 17:24:03 +00:00
|
|
|
func branchInfo(pkgName string, owner string, repo string) (err error) {
|
2017-05-09 01:09:43 +00:00
|
|
|
Updated = true
|
2017-05-02 00:06:16 +00:00
|
|
|
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
|
|
|
|
2017-06-11 20:50:39 +00:00
|
|
|
packinfo := inStore(pkgName)
|
2017-05-02 00:06:16 +00:00
|
|
|
|
|
|
|
for _, e := range newRepo {
|
|
|
|
if e.Name == "master" {
|
|
|
|
if packinfo != nil {
|
2017-05-16 15:53:15 +00:00
|
|
|
packinfo.Package = pkgName
|
2017-05-02 10:50:11 +00:00
|
|
|
packinfo.URL = url
|
2017-05-09 01:09:43 +00:00
|
|
|
packinfo.SHA = e.Commit.SHA
|
2017-05-02 00:06:16 +00:00
|
|
|
} else {
|
2017-05-16 15:53:15 +00:00
|
|
|
savedInfo = append(savedInfo, Info{Package: pkgName, URL: url, SHA: e.Commit.SHA})
|
2017-05-02 00:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 01:34:40 +00:00
|
|
|
return
|
|
|
|
}
|
2017-05-02 10:50:11 +00:00
|
|
|
|
2017-08-02 17:24:03 +00:00
|
|
|
func saveVCSInfo() error {
|
|
|
|
marshalledinfo, err := json.MarshalIndent(savedInfo)
|
2017-05-09 13:44:34 +00:00
|
|
|
if err != nil || string(marshalledinfo) == "null" {
|
|
|
|
return err
|
|
|
|
}
|
2017-05-02 10:50:11 +00:00
|
|
|
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
|
|
|
|
}
|