yay/vcs.go

147 lines
3.0 KiB
Go
Raw Normal View History

package main
2017-05-01 03:23:03 +02:00
2017-05-01 03:34:40 +02:00
import (
"encoding/json"
"fmt"
2017-11-21 01:27:01 +01:00
"io/ioutil"
2017-05-01 03:34:40 +02:00
"net/http"
"os"
2017-05-01 03:34:40 +02:00
"strings"
)
2017-05-01 03:23:03 +02:00
// 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"`
}
type branches []branch
// Info contains the last commit sha of a repo
type Info struct {
2017-05-02 12:50:11 +02:00
Package string `json:"pkgname"`
URL string `json:"url"`
SHA string `json:"sha"`
}
type infos []Info
2017-10-14 18:11:47 +02:00
// createDevelDB forces yay to create a DB of the existing development packages
func createDevelDB() error {
_, _, _, remoteNames, err := filterPackages()
if err != nil {
return err
}
config.NoConfirm = true
specialDBsauce = true
err = aurInstall(remoteNames, nil)
return err
2017-05-01 03:23:03 +02:00
}
2017-10-14 18:11:47 +02:00
// parseSource returns owner and repo from source
func parseSource(source string) (owner string, repo string) {
if !(strings.Contains(source, "git://") ||
strings.Contains(source, ".git") ||
strings.Contains(source, "git+https://")) {
return
}
2017-05-01 03:23:03 +02: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 03:34:40 +02:00
2017-05-02 12:50:11 +02: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-11-21 01:27:01 +01:00
body, _ := ioutil.ReadAll(r.Body)
err = json.Unmarshal(body, &newRepo)
2017-05-02 12:50:11 +02:00
if err != nil {
2017-11-21 01:27:01 +01:00
fmt.Printf("Cannot update '%v'\nError: %v\nStatus code: %v\nBody: %v\n",
info.Package, err, r.StatusCode, string(body))
2017-05-02 12:50:11 +02:00
return false
}
for _, e := range newRepo {
if e.Name == "master" {
return e.Commit.SHA != info.SHA
2017-05-02 12:50:11 +02:00
}
}
return false
}
func inStore(pkgName string) *Info {
for i, e := range savedInfo {
if pkgName == e.Package {
return &savedInfo[i]
}
}
return nil
}
2017-10-14 18:11:47 +02:00
// branchInfo updates saved information
func branchInfo(pkgName string, owner string, repo string) (err error) {
2017-08-04 11:26:53 +02:00
updated = true
var newRepo branches
2017-05-01 03:34:40 +02: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 12:50:11 +02:00
_ = json.NewDecoder(r.Body).Decode(&newRepo)
2017-05-01 03:34:40 +02:00
packinfo := inStore(pkgName)
for _, e := range newRepo {
if e.Name == "master" {
if packinfo != nil {
packinfo.Package = pkgName
2017-05-02 12:50:11 +02:00
packinfo.URL = url
packinfo.SHA = e.Commit.SHA
} else {
savedInfo = append(savedInfo, Info{Package: pkgName, URL: url, SHA: e.Commit.SHA})
}
}
}
2017-05-01 03:34:40 +02:00
return
}
2017-05-02 12:50:11 +02:00
func saveVCSInfo() error {
2017-08-04 11:26:53 +02:00
marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
2017-05-09 15:44:34 +02:00
if err != nil || string(marshalledinfo) == "null" {
return err
}
in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
2017-05-02 12:50:11 +02:00
if err != nil {
return err
}
defer in.Close()
_, err = in.Write(marshalledinfo)
if err != nil {
return err
}
err = in.Sync()
return err
}