working commit
This commit is contained in:
@@ -0,0 +1,73 @@
|
|||||||
|
package repocli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (cli *Client) GetCatalog(ctx context.Context, rawrepo string) ([]string, error) {
|
||||||
|
var err error
|
||||||
|
list := make([]string, 0)
|
||||||
|
catdata, err := cli.getRawCatalog(ctx, rawrepo)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(catdata, &list)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cli *Client) getRawCatalog(ctx context.Context, rawrepo string) ([]byte, error) {
|
||||||
|
var err error
|
||||||
|
var list []byte
|
||||||
|
|
||||||
|
ref, err := NewReferer(rawrepo)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
uri := ref.Catalog()
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", cli.userAgent)
|
||||||
|
req.Header.Set("Accept", "*/*")
|
||||||
|
resp, err := cli.httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode == http.StatusNotFound {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
err := fmt.Errorf("Unxected response code %s", resp.Status)
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
contentLength := resp.Header.Get("Content-Length")
|
||||||
|
if contentLength == "" {
|
||||||
|
err = errors.New("Empty Content-Length header")
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
listSize, err := strconv.ParseInt(contentLength, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
buffer := bytes.NewBuffer(nil)
|
||||||
|
recSize, err := Copy(ctx, buffer, resp.Body)
|
||||||
|
if listSize != recSize {
|
||||||
|
err := fmt.Errorf("Mismatch declared and actual size")
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
list = buffer.Bytes()
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package repocli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (cli *Client) GetReferers(ctx context.Context, rawrepo, digest string) ([]string, error) {
|
||||||
|
var err error
|
||||||
|
list := make([]string, 0)
|
||||||
|
tagdata, err := cli.getRawReferers(ctx, rawrepo, digest)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(tagdata, &list)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cli *Client) getRawReferers(ctx context.Context, rawrepo, digest string) ([]byte, error) {
|
||||||
|
var err error
|
||||||
|
var list []byte
|
||||||
|
|
||||||
|
ref, err := NewReferer(rawrepo)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
uri := ref.Referers(digest)
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", cli.userAgent)
|
||||||
|
req.Header.Set("Accept", "*/*")
|
||||||
|
resp, err := cli.httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
err := fmt.Errorf("Unxected response code %s", resp.Status)
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
contentLength := resp.Header.Get("Content-Length")
|
||||||
|
if contentLength == "" {
|
||||||
|
err = errors.New("Empty Content-Length header")
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
listSize, err := strconv.ParseInt(contentLength, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
buffer := bytes.NewBuffer(nil)
|
||||||
|
recSize, err := Copy(ctx, buffer, resp.Body)
|
||||||
|
if listSize != recSize {
|
||||||
|
err := fmt.Errorf("Mismatch declared and actual size")
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
list = buffer.Bytes()
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
@@ -0,0 +1,70 @@
|
|||||||
|
package repocli
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (cli *Client) GetTags(ctx context.Context, rawrepo string) ([]string, error) {
|
||||||
|
var err error
|
||||||
|
list := make([]string, 0)
|
||||||
|
tagdata, err := cli.getRawTags(ctx, rawrepo)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
err = json.Unmarshal(tagdata, &list)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cli *Client) getRawTags(ctx context.Context, rawrepo string) ([]byte, error) {
|
||||||
|
var err error
|
||||||
|
var list []byte
|
||||||
|
|
||||||
|
ref, err := NewReferer(rawrepo)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
uri := ref.Tags()
|
||||||
|
|
||||||
|
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
req.Header.Set("User-Agent", cli.userAgent)
|
||||||
|
req.Header.Set("Accept", "*/*")
|
||||||
|
resp, err := cli.httpClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
err := fmt.Errorf("Unxected response code %s", resp.Status)
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
contentLength := resp.Header.Get("Content-Length")
|
||||||
|
if contentLength == "" {
|
||||||
|
err = errors.New("Empty Content-Length header")
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
listSize, err := strconv.ParseInt(contentLength, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
buffer := bytes.NewBuffer(nil)
|
||||||
|
recSize, err := Copy(ctx, buffer, resp.Body)
|
||||||
|
if listSize != recSize {
|
||||||
|
err := fmt.Errorf("Mismatch declared and actual size")
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
|
list = buffer.Bytes()
|
||||||
|
return list, err
|
||||||
|
}
|
||||||
@@ -95,6 +95,21 @@ func (repo *Referer) Put(loc, digest string) (string, error) {
|
|||||||
return out, err
|
return out, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (repo *Referer) Tags() string {
|
||||||
|
curl := repo.urlobj.JoinPath("/v2", repo.base, "/tags/list")
|
||||||
|
return curl.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (repo *Referer) Referers(digest string) string {
|
||||||
|
curl := repo.urlobj.JoinPath("/v2", repo.base, "/referrers/", digest)
|
||||||
|
return curl.String()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (repo *Referer) Catalog() string {
|
||||||
|
curl := repo.urlobj.JoinPath("/v2/_catalog")
|
||||||
|
return curl.String()
|
||||||
|
}
|
||||||
|
|
||||||
func (repo *Referer) Userinfo() (string, string) {
|
func (repo *Referer) Userinfo() (string, string) {
|
||||||
return repo.user, repo.pass
|
return repo.user, repo.pass
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user