This repository has been archived on 2023-11-08. You can view files and clone it, but cannot push or open issues/pull-requests.
eget/forge/github.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
}