unos/gitea-packages-update-check/main.go

73 lines
1.3 KiB
Go
Raw Normal View History

package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"time"
)
func main() {
var (
r = flag.String("registry", "git.usbharu.dev", "gitea registry")
s = flag.Bool("https", true, "use https")
o = flag.String("owner", "usbharu", "package owner")
n = flag.String("name", "", "package name")
t = flag.String("type", "generic", "package type")
)
flag.Parse()
var protocol string
if *s {
protocol = "https"
} else {
protocol = "http"
}
url := protocol + "://" + *r + "/api/v1/packages/" + *o + "?type=" + *t + "&q=" + *n + "&token=" + os.Getenv("GITEA_TOKEN")
get, err := http.Get(url)
if err != nil {
log.Fatal(err)
return
}
if get.StatusCode != 200 {
log.Fatalf("The Status is not 200. %d", get.StatusCode)
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(get.Body)
all, err := io.ReadAll(get.Body)
if err != nil {
log.Fatal(err)
return
}
var p []Packages
if err := json.Unmarshal(all, &p); err != nil {
log.Fatal("Error in json decode", err)
return
}
last := time.Time{}
var packageT Packages
for _, packages := range p {
if last.Before(packages.CreatedAt) {
last = packages.CreatedAt
packageT = packages
}
}
fmt.Printf("%s://%s/api/packages/%s/%s/%s/%s", protocol, *r, *o, *t, packageT.Name, packageT.Version)
}