64 lines
1.1 KiB
Go
64 lines
1.1 KiB
Go
package forge
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
var _ Forger = (*GitHub)(nil)
|
|
|
|
type GitHub struct {
|
|
Owner string
|
|
Repo string
|
|
}
|
|
|
|
type GitHubRelease struct {
|
|
TagName string `json:"tag_name"`
|
|
Assets []struct {
|
|
Name string `json:"name"`
|
|
BrowserDownloadURL string `json:"browser_download_url"`
|
|
} `json:"assets"`
|
|
}
|
|
|
|
func NewGitHub(uri string) (GitHub, error) {
|
|
var g GitHub
|
|
_, paths, err := splitURI(uri)
|
|
if err != nil {
|
|
return g, err
|
|
}
|
|
g.Owner = paths[0]
|
|
g.Repo = paths[1]
|
|
return g, nil
|
|
}
|
|
|
|
func (g GitHub) Name() string {
|
|
return g.Repo
|
|
}
|
|
|
|
func (g GitHub) Latest() (Release, error) {
|
|
var release Release
|
|
|
|
var r GitHubRelease
|
|
u := fmt.Sprintf("https://api.github.com/repos/%s/%s/releases/latest", g.Owner, g.Repo)
|
|
res, err := http.Get(u)
|
|
if err != nil {
|
|
return release, err
|
|
}
|
|
defer res.Body.Close()
|
|
|
|
if err := json.NewDecoder(res.Body).Decode(&r); err != nil {
|
|
return release, err
|
|
}
|
|
|
|
release.Name = r.TagName
|
|
for _, a := range r.Assets {
|
|
release.Assets = append(release.Assets, Asset{
|
|
Name: a.Name,
|
|
DownloadURL: a.BrowserDownloadURL,
|
|
})
|
|
}
|
|
|
|
return release, nil
|
|
}
|