code reformat, init fixes

This commit is contained in:
2021-11-28 01:04:20 +01:00
parent da41b5820b
commit 1071695e87
37 changed files with 559 additions and 162 deletions

View File

@ -0,0 +1,57 @@
package docker_hub
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
type DockerSearchResponse struct {
NumPages int `json:"num_pages"`
NumResults int `json:"num_results"`
PageSize int `json:"page_size"`
Page int `json:"page"`
Query string `json:"query"`
Results []DockerImage `json:"results"`
}
type DockerImage struct {
Name string `json:"name"`
Description string `json:"description"`
StarCount int `json:"star_count"`
IsTrusted bool `json:"is_trusted"`
IsAutomated bool `json:"is_automated"`
IsOfficial bool `json:"is_official"`
}
var urlSearch = "https://registry.hub.docker.com/v1/search"
func GetImage(image string) []DockerImage {
var resp DockerSearchResponse
searchString := url.QueryEscape(image)
urlSearch = urlSearch + "/?q=" + searchString
fmt.Printf("%v", urlSearch)
res, _ := http.Get(urlSearch)
defer res.Body.Close()
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
json.Unmarshal(bytes, &resp)
return resp.Results
}
func DockerImageToArray(images []DockerImage) []string {
n := len(images)
arrTags := make([]string, n)
for idx, image := range images {
arrTags[idx] = image.Name
}
return arrTags
}

View File

@ -8,23 +8,23 @@ import (
type DockerImageTag struct {
Layer string `json:"layer"`
Name string `json:"name"`
Name string `json:"name"`
}
var url = "https://registry.hub.docker.com/v1/repositories"
var urlTags = "https://registry.hub.docker.com/v1/repositories"
func GetImageTags(image string) []DockerImageTag {
url = url + "/" + image + "/tags"
res, _ := http.Get(url)
urlTags = urlTags + "/" + image + "/tags"
res, _ := http.Get(urlTags)
defer res.Body.Close()
bytes, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
if err != nil {
panic(err.Error())
}
var tags []DockerImageTag
json.Unmarshal(bytes, &tags)
var tags []DockerImageTag
json.Unmarshal(bytes, &tags)
return tags
}
@ -37,4 +37,4 @@ func ImageTagsToArray(tags []DockerImageTag) []string {
}
return arrTags
}
}