refactor: migrate JSON function to new package

We now use it to help read remote docker registries also.
This commit is contained in:
decentral1se 2021-08-09 16:16:33 +02:00
parent 260edad142
commit 7ad812ad98
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
3 changed files with 17 additions and 7 deletions

View File

@ -11,6 +11,7 @@ import (
"time" "time"
"coopcloud.tech/abra/config" "coopcloud.tech/abra/config"
"coopcloud.tech/abra/web"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing" "github.com/go-git/go-git/v5/plumbing"
) )
@ -204,7 +205,7 @@ func readAppsCatalogueFS(target interface{}) error {
} }
func readAppsCatalogueWeb(target interface{}) error { func readAppsCatalogueWeb(target interface{}) error {
if err := readJson(appsCatalogueURL, &target); err != nil { if err := web.ReadJSON(appsCatalogueURL, &target); err != nil {
return err return err
} }

View File

@ -1,7 +1,9 @@
package client package client
import ( import (
"github.com/docker/distribution/reference" "fmt"
"coopcloud.tech/abra/web"
) )
type Tag struct { type Tag struct {
@ -13,7 +15,13 @@ type Tags []Tag
var registryURL = "https://registry.hub.docker.com/v1/repositories/%s/tags" var registryURL = "https://registry.hub.docker.com/v1/repositories/%s/tags"
func ReadRegistryTags(image reference.Named, target interface{}) ([]string, error) { func GetRegistryTags(image string) (Tags, error) {
// tagsUrl := fmt.Sprintf(registryURL, image.Name()) var tags Tags
return nil, nil
tagsUrl := fmt.Sprintf(registryURL, image)
if err := web.ReadJSON(tagsUrl, &tags); err != nil {
return tags, err
}
return tags, nil
} }

View File

@ -1,4 +1,4 @@
package catalogue package web
import ( import (
"encoding/json" "encoding/json"
@ -6,7 +6,8 @@ import (
"time" "time"
) )
func readJson(url string, target interface{}) error { // ReadJSON reads JSON and parses it into your chosen interface pointer
func ReadJSON(url string, target interface{}) error {
httpClient := &http.Client{Timeout: 5 * time.Second} httpClient := &http.Client{Timeout: 5 * time.Second}
res, err := httpClient.Get(url) res, err := httpClient.Get(url)
if err != nil { if err != nil {