working commit
This commit is contained in:
@@ -0,0 +1,9 @@
|
||||
package repocli
|
||||
|
||||
const (
|
||||
MediaTypeOIIv1 = "application/vnd.oci.image.index.v1+json"
|
||||
MediaTypeDDMLv2 = "application/vnd.docker.distribution.manifest.list.v2+json"
|
||||
|
||||
MediaTypeDDMv2 = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
MediaTypeOIMv1 = "application/vnd.oci.image.manifest.v1+json"
|
||||
)
|
||||
+38
-9
@@ -23,14 +23,23 @@ import (
|
||||
ocidigest "github.com/opencontainers/go-digest"
|
||||
)
|
||||
|
||||
func (cli *Client) GetRawManifest(ctx context.Context, rawrepo string) (bool, string, []byte, string, error) {
|
||||
const (
|
||||
ddmMediaType = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
oimMediaType = "application/vnd.oci.image.manifest.v1+json"
|
||||
ddmlMediaType = "application/vnd.docker.distribution.manifest.list.v2+json"
|
||||
oiiMeiaType = "application/vnd.oci.image.index.v1+json"
|
||||
|
||||
//application/vnd.docker.distribution.manifest.v1+json
|
||||
//application/vnd.docker.distribution.manifest.v1+prettyjws
|
||||
)
|
||||
|
||||
func (cli *Client) GetRawManifest(ctx context.Context, rawrepo string, accepts []string) (bool, string, []byte, string, error) {
|
||||
var err error
|
||||
var exist bool
|
||||
var mime string
|
||||
var man []byte
|
||||
var digstr string
|
||||
|
||||
fmt.Printf("=== %s\n", rawrepo)
|
||||
ref, err := NewReferer(rawrepo)
|
||||
if err != nil {
|
||||
return exist, mime, man, digstr, err
|
||||
@@ -42,7 +51,16 @@ func (cli *Client) GetRawManifest(ctx context.Context, rawrepo string) (bool, st
|
||||
return exist, mime, man, digstr, err
|
||||
}
|
||||
req.Header.Set("User-Agent", cli.userAgent)
|
||||
req.Header.Set("Accept", "*/*")
|
||||
if len(accepts) == 0 {
|
||||
accepts = []string{
|
||||
MediaTypeDDMLv2,
|
||||
MediaTypeDDMv2,
|
||||
MediaTypeOIMv1,
|
||||
MediaTypeOIIv1,
|
||||
}
|
||||
}
|
||||
req.Header.Set("Accept", strings.Join(accepts, ","))
|
||||
|
||||
resp, err := cli.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return exist, mime, man, digstr, err
|
||||
@@ -56,9 +74,25 @@ func (cli *Client) GetRawManifest(ctx context.Context, rawrepo string) (bool, st
|
||||
err := fmt.Errorf("Unxected response code %s", resp.Status)
|
||||
return exist, mime, man, digstr, err
|
||||
}
|
||||
mime = resp.Header.Get("Content-Type")
|
||||
if mime == "" {
|
||||
err := fmt.Errorf("Empty MIME type declaration")
|
||||
return exist, mime, man, digstr, err
|
||||
}
|
||||
accepted := false
|
||||
for _, accept := range accepts {
|
||||
if mime == accept {
|
||||
accepted = true
|
||||
}
|
||||
}
|
||||
if !accepted {
|
||||
err := fmt.Errorf("Unknown content type: %s", mime)
|
||||
return exist, mime, man, digstr, err
|
||||
|
||||
}
|
||||
digstr = resp.Header.Get("Docker-Content-Digest")
|
||||
if digstr == "" {
|
||||
err := fmt.Errorf("Empty digest declaration")
|
||||
err := fmt.Errorf("Empty manifest digest declaration")
|
||||
return exist, mime, man, digstr, err
|
||||
}
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
@@ -70,11 +104,6 @@ func (cli *Client) GetRawManifest(ctx context.Context, rawrepo string) (bool, st
|
||||
if err != nil {
|
||||
return exist, mime, man, digstr, err
|
||||
}
|
||||
mime = resp.Header.Get("Content-Type")
|
||||
if mime == "" {
|
||||
err := fmt.Errorf("Empty MIME type declaration")
|
||||
return exist, mime, man, digstr, err
|
||||
}
|
||||
digstr = strings.ToLower(digstr)
|
||||
digobj, err := ocidigest.Parse(digstr)
|
||||
if err != nil {
|
||||
|
||||
@@ -52,6 +52,7 @@ func (cli *Client) GetRawTags(ctx context.Context, rawrepo string) ([]byte, erro
|
||||
if err != nil {
|
||||
return list, err
|
||||
}
|
||||
fmt.Printf("uri: %s\n", uri)
|
||||
req.Header.Set("User-Agent", cli.userAgent)
|
||||
req.Header.Set("Accept", "*/*")
|
||||
resp, err := cli.httpClient.Do(req)
|
||||
|
||||
+24
-12
@@ -15,16 +15,17 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"runtime"
|
||||
|
||||
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
||||
)
|
||||
|
||||
const (
|
||||
MediaTypeOIIv1 = "application/vnd.oci.image.index.v1+json"
|
||||
MediaTypeDDMLv2 = "application/vnd.docker.distribution.manifest.list.v2+json"
|
||||
//MediaTypeOIIv1 = "application/vnd.oci.image.index.v1+json"
|
||||
//MediaTypeDDMLv2 = "application/vnd.docker.distribution.manifest.list.v2+json"
|
||||
|
||||
MediaTypeDDMv2 = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
MediaTypeOIMv1 = "application/vnd.oci.image.manifest.v1+json"
|
||||
// MediaTypeDDMv2 = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
// MediaTypeOIMv1 = "application/vnd.oci.image.manifest.v1+json"
|
||||
)
|
||||
|
||||
type Loader struct {
|
||||
@@ -39,13 +40,25 @@ func NewLoader(client *Client) *Loader {
|
||||
|
||||
func (down *Loader) Pull(ctx context.Context, rawref, dir, osname, arch string) error {
|
||||
var err error
|
||||
if osname == "" {
|
||||
osname = runtime.GOOS
|
||||
}
|
||||
if arch == "" {
|
||||
arch = runtime.GOARCH
|
||||
}
|
||||
|
||||
ref, err := NewReferer(rawref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rawrepo := ref.RawRepo()
|
||||
|
||||
exist, mime, man, digstr, err := down.cli.GetRawManifest(ctx, ref.Raw())
|
||||
accepts := []string{
|
||||
MediaTypeDDMLv2,
|
||||
MediaTypeDDMv2,
|
||||
MediaTypeOIMv1,
|
||||
MediaTypeOIIv1,
|
||||
}
|
||||
exist, mime, man, digstr, err := down.cli.GetRawManifest(ctx, ref.Raw(), accepts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -74,13 +87,12 @@ func (down *Loader) Pull(ctx context.Context, rawref, dir, osname, arch string)
|
||||
err = errors.New("Manifest for required arch and OS not found")
|
||||
return err
|
||||
}
|
||||
|
||||
exist, mime, man, digstr, err = down.cli.GetRawManifest(ctx, ref.RawWithTag(tag))
|
||||
if err != nil {
|
||||
return err
|
||||
accepts := []string{
|
||||
MediaTypeDDMv2,
|
||||
MediaTypeOIMv1,
|
||||
}
|
||||
if !exist {
|
||||
err = errors.New("Manifest not found")
|
||||
_, mime, man, digstr, err = down.cli.GetRawManifest(ctx, ref.RawWithTag(tag), accepts)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else if mime != MediaTypeOIMv1 && mime != MediaTypeDDMv2 {
|
||||
|
||||
Reference in New Issue
Block a user