client rebuilding in progress
This commit is contained in:
@@ -9,6 +9,10 @@
|
||||
*/
|
||||
package descr
|
||||
|
||||
import (
|
||||
"path"
|
||||
)
|
||||
|
||||
type File struct {
|
||||
ID string `db:"id" json:"id,omitempty" yaml:"id,omitempty"`
|
||||
Collection string `db:"collection" json:"collection,omitempty" yaml:"collection,omitempty"`
|
||||
@@ -21,3 +25,44 @@ type File struct {
|
||||
CreatedBy string `db:"created_by" json:"createdBy,omitempty" yaml:"createdBy,omitempty"`
|
||||
UpdatedBy string `db:"updated_by" json:"updatedBy,omitempty" yaml:"updatedBy,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
type Files struct {
|
||||
files []File
|
||||
}
|
||||
|
||||
func NewFiles() *Files {
|
||||
return &Files{
|
||||
files: make([]File, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func xxxNewFiles(files []File) *Files {
|
||||
return &Files{
|
||||
files: files,
|
||||
}
|
||||
}
|
||||
|
||||
func (fi *Files) Set(files []File) {
|
||||
fi.files = files
|
||||
}
|
||||
|
||||
func (fi Files) ArrayPtr() *[]File {
|
||||
return &fi.files
|
||||
}
|
||||
|
||||
func (fi Files) Array() []File {
|
||||
return fi.files
|
||||
}
|
||||
|
||||
|
||||
|
||||
func (fi Files) List() []string {
|
||||
list := make([]string, 0)
|
||||
for _, file := range fi.files {
|
||||
list = append(list, path.Join(file.Collection, file.Name))
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
|
||||
+31
-23
@@ -11,13 +11,17 @@ type Client struct {
|
||||
userAgent string
|
||||
}
|
||||
|
||||
func NewClient(transport http.RoundTripper, mwFunc ...MiddlewareFunc) *Client {
|
||||
func NewClient(transport http.RoundTripper, mwFuncs ...MiddlewareFunc) *Client {
|
||||
if transport == nil {
|
||||
transport = NewDefaultTransport()
|
||||
}
|
||||
for _, mwFunc := range mwFuncs {
|
||||
transport = mwFunc(transport)
|
||||
}
|
||||
httpClient := &http.Client{
|
||||
Transport: transport,
|
||||
}
|
||||
|
||||
return &Client{
|
||||
httpClient: httpClient,
|
||||
userAgent: "ociClient/1.0",
|
||||
@@ -34,26 +38,6 @@ func (cli *Client) UseMiddleware(mwFunc MiddlewareFunc) {
|
||||
cli.httpClient.Transport = mwFunc(cli.httpClient.Transport)
|
||||
}
|
||||
|
||||
// ExampleMiddleware
|
||||
func NewExampleMiddleware() MiddlewareFunc {
|
||||
return func(next http.RoundTripper) http.RoundTripper {
|
||||
return newExampleTransport(next)
|
||||
}
|
||||
}
|
||||
|
||||
type ExampleTransport struct {
|
||||
next http.RoundTripper
|
||||
}
|
||||
|
||||
func newExampleTransport(next http.RoundTripper) *ExampleTransport {
|
||||
return &ExampleTransport{
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
|
||||
func (tran ExampleTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
|
||||
// BasicAuthMiddleware
|
||||
func NewBasicAuthMiddleware(user, pass string) MiddlewareFunc {
|
||||
@@ -76,8 +60,10 @@ func newBasicAuthMW(next http.RoundTripper, user, pass string) *BasicAuthMW {
|
||||
}
|
||||
|
||||
func (tran BasicAuthMW) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
pair := base64.StdEncoding.EncodeToString([]byte(tran.user + ":" + tran.pass))
|
||||
req.Header.Set("Authorization", "Basic "+pair)
|
||||
if tran.user != "" && tran.pass != "" {
|
||||
pair := base64.StdEncoding.EncodeToString([]byte(tran.user + ":" + tran.pass))
|
||||
req.Header.Set("Authorization", "Basic "+pair)
|
||||
}
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
|
||||
@@ -123,3 +109,25 @@ func NewDefaultTransport() *DefaultTransport {
|
||||
func (wrap *DefaultTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return wrap.transport.RoundTrip(req)
|
||||
}
|
||||
|
||||
|
||||
// ExampleMiddleware
|
||||
func NewExampleMiddleware() MiddlewareFunc {
|
||||
return func(next http.RoundTripper) http.RoundTripper {
|
||||
return newExampleTransport(next)
|
||||
}
|
||||
}
|
||||
|
||||
type ExampleTransport struct {
|
||||
next http.RoundTripper
|
||||
}
|
||||
|
||||
func newExampleTransport(next http.RoundTripper) *ExampleTransport {
|
||||
return &ExampleTransport{
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
|
||||
func (tran ExampleTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
package filecli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (cli *Client) DeleteCollection(ctx context.Context, rawpath string) ([]byte, error) {
|
||||
var err error
|
||||
var list []byte
|
||||
|
||||
ref, err := ParsePath(rawpath)
|
||||
if err != nil {
|
||||
return list, err
|
||||
}
|
||||
uri := ref.Collection()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodDelete, 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")
|
||||
blobSize, err := strconv.ParseInt(contentLength, 10, 64)
|
||||
if err != nil {
|
||||
return list, err
|
||||
}
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
recSize, err := Copy(ctx, buffer, resp.Body)
|
||||
if blobSize != recSize {
|
||||
err := fmt.Errorf("Mismatch declared and actual body size, %d and %d", blobSize, recSize)
|
||||
return list, err
|
||||
}
|
||||
list = buffer.Bytes()
|
||||
return list, err
|
||||
}
|
||||
@@ -7,44 +7,45 @@ import (
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (cli *Client) FileExists(ctx context.Context, rawpath string) (bool, int64, error) {
|
||||
func (cli *Client) FileInfo(ctx context.Context, rawpath string) (bool, int64, string, error) {
|
||||
var err error
|
||||
var exist bool
|
||||
var size int64
|
||||
var digest string
|
||||
|
||||
ref, err := ParsePath(rawpath)
|
||||
if err != nil {
|
||||
return exist, size, err
|
||||
return exist, size, digest, err
|
||||
}
|
||||
uri := ref.File()
|
||||
|
||||
fmt.Println(uri)
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, uri, nil)
|
||||
if err != nil {
|
||||
return exist, size, err
|
||||
return exist, size, digest, err
|
||||
}
|
||||
req.Header.Set("User-Agent", cli.userAgent)
|
||||
req.Header.Set("Accept", "*/*")
|
||||
|
||||
resp, err := cli.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return exist, size, err
|
||||
return exist, size, digest, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return exist, size, err
|
||||
return exist, size, digest, err
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err := fmt.Errorf("Unxected response code %s", resp.Status)
|
||||
return exist, size, err
|
||||
return exist, size, digest, err
|
||||
}
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
contentLength := resp.Header.Get("Content-Size")
|
||||
size, err = strconv.ParseInt(contentLength, 10, 64)
|
||||
if err != nil {
|
||||
return exist, size, err
|
||||
return exist, size, digest, err
|
||||
}
|
||||
|
||||
digest = resp.Header.Get("Content-Digest")
|
||||
exist = true
|
||||
return exist, size, err
|
||||
return exist, size, digest, err
|
||||
}
|
||||
@@ -12,7 +12,7 @@ func (cli *Client) GetFile(ctx context.Context, rawpath string, writer io.Writer
|
||||
var err error
|
||||
var exist bool
|
||||
|
||||
ref, err :=ParsePath(rawpath)
|
||||
ref, err := ParsePath(rawpath)
|
||||
if err != nil {
|
||||
return exist, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
package filecli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (cli *Client) ListCollections(ctx context.Context, rawpath string) ([]byte, error) {
|
||||
var err error
|
||||
var list []byte
|
||||
|
||||
ref, err := ParsePath(rawpath)
|
||||
if err != nil {
|
||||
return list, err
|
||||
}
|
||||
uri := ref.Collections()
|
||||
|
||||
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("Unexpected response code %s", resp.Status)
|
||||
return list, err
|
||||
}
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
blobSize, err := strconv.ParseInt(contentLength, 10, 64)
|
||||
if err != nil {
|
||||
return list, err
|
||||
}
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
recSize, err := Copy(ctx, buffer, resp.Body)
|
||||
if blobSize != recSize {
|
||||
err := fmt.Errorf("Mismatch declared and actual body size, %d and %d", blobSize, recSize)
|
||||
return list, err
|
||||
}
|
||||
list = buffer.Bytes()
|
||||
return list, err
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package filecli
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func (cli *Client) ListFiles(ctx context.Context, rawpath string) ([]byte, error) {
|
||||
var err error
|
||||
var list []byte
|
||||
|
||||
ref, err := ParsePath(rawpath)
|
||||
if err != nil {
|
||||
return list, err
|
||||
}
|
||||
uri := ref.Files()
|
||||
|
||||
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("Unexpected response code %s", resp.Status)
|
||||
return list, err
|
||||
}
|
||||
contentLength := resp.Header.Get("Content-Length")
|
||||
blobSize, err := strconv.ParseInt(contentLength, 10, 64)
|
||||
if err != nil {
|
||||
return list, err
|
||||
}
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
recSize, err := Copy(ctx, buffer, resp.Body)
|
||||
if blobSize != recSize {
|
||||
err := fmt.Errorf("Mismatch declared and actual body size, %d and %d", blobSize, recSize)
|
||||
return list, err
|
||||
}
|
||||
list = buffer.Bytes()
|
||||
return list, err
|
||||
}
|
||||
@@ -21,14 +21,14 @@ func (cli *Client) PutFile(ctx context.Context, rawpath string, src io.Reader, s
|
||||
}
|
||||
req.Header.Set("User-Agent", cli.userAgent)
|
||||
req.Header.Set("Content-Type", "application/octet-stream")
|
||||
req.Header.Set("Content-Length", strconv.FormatInt(size, 10))
|
||||
req.Header.Set("Content-Size", strconv.FormatInt(size, 10))
|
||||
resp, err := cli.httpClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
err = fmt.Errorf("File not accepted, code %d", resp.StatusCode)
|
||||
err = fmt.Errorf("File not accepted, code %s", resp.Status)
|
||||
return err
|
||||
}
|
||||
return err
|
||||
|
||||
+47
-6
@@ -2,17 +2,27 @@ package filecli
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
const (
|
||||
PathTypeIdentic = "identic"
|
||||
PathTypePrefix = "prefix"
|
||||
PathTypeRegexp = "regexp"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
urlobj *url.URL
|
||||
user, pass string
|
||||
resource string
|
||||
resource string
|
||||
values url.Values
|
||||
}
|
||||
|
||||
func ParsePath(rawpath string) (*Repository, error) {
|
||||
repo := &Repository{}
|
||||
repo := &Repository{
|
||||
values: url.Values{},
|
||||
}
|
||||
if !strings.Contains(rawpath, "://") {
|
||||
rawpath = "https://" + rawpath
|
||||
}
|
||||
@@ -25,14 +35,30 @@ func ParsePath(rawpath string) (*Repository, error) {
|
||||
repo.pass, _ = urlobj.User.Password()
|
||||
urlobj.User = nil
|
||||
}
|
||||
repo.resource = repo.urlobj.Path
|
||||
repo.resource = urlobj.Path
|
||||
urlobj.Path = "/"
|
||||
repo.urlobj = urlobj
|
||||
repo.urlobj.Path = "/"
|
||||
repo.urlobj = urlobj
|
||||
|
||||
repo.values = urlobj.Query()
|
||||
return repo, err
|
||||
}
|
||||
|
||||
func (repo *Repository) Raw() string {
|
||||
res := path.Join(repo.urlobj.Host, repo.resource)
|
||||
query := repo.values.Encode()
|
||||
if query != "" {
|
||||
return res + "?" + query
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func (repo *Repository) SetResource(resource string) {
|
||||
repo.resource = resource
|
||||
}
|
||||
|
||||
func (repo *Repository) PathType(typ string) {
|
||||
repo.values.Set("pathType", typ)
|
||||
}
|
||||
|
||||
func (repo *Repository) File() string {
|
||||
curl := repo.urlobj.JoinPath("/v3/api/file", repo.resource)
|
||||
return curl.String()
|
||||
@@ -43,7 +69,22 @@ func (repo *Repository) Files() string {
|
||||
return curl.String()
|
||||
}
|
||||
|
||||
func (repo *Repository) Collection() string {
|
||||
curl := repo.urlobj.JoinPath("/v3/api/collection", repo.resource)
|
||||
return curl.String()
|
||||
}
|
||||
|
||||
func (repo *Repository) Collections() string {
|
||||
curl := repo.urlobj.JoinPath("/v3/api/collections", repo.resource)
|
||||
return curl.String()
|
||||
}
|
||||
|
||||
func (repo *Repository) Userinfo() (string, string) {
|
||||
return repo.user, repo.pass
|
||||
}
|
||||
|
||||
func (repo *Repository) SetUserinfo(user, pass string) {
|
||||
if user != "" && pass != "" {
|
||||
repo.user, repo.pass = user, pass
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,8 +87,10 @@ func newBasicAuthMW(next http.RoundTripper, user, pass string) *BasicAuthMW {
|
||||
}
|
||||
|
||||
func (tran BasicAuthMW) RoundTrip(req *http.Request) (*http.Response, error) {
|
||||
pair := base64.StdEncoding.EncodeToString([]byte(tran.user + ":" + tran.pass))
|
||||
req.Header.Set("Authorization", "Basic "+pair)
|
||||
if tran.user != "" && tran.pass != "" {
|
||||
pair := base64.StdEncoding.EncodeToString([]byte(tran.user + ":" + tran.pass))
|
||||
req.Header.Set("Authorization", "Basic "+pair)
|
||||
}
|
||||
return tran.next.RoundTrip(req)
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
func xxxTestClientGetManifest(t *testing.T) {
|
||||
func TestClientGetManifest(t *testing.T) {
|
||||
rawrepo := "mirror.gcr.io/alpine"
|
||||
tags := []string{
|
||||
"3.20.0",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package repocli
|
||||
|
||||
const (
|
||||
MediaTypeDDMLv2 = "application/vnd.docker.distribution.manifest.list.v2+json"
|
||||
MediaTypeDDMv2 = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
//MediaTypeDDMLv2 = "application/vnd.docker.distribution.manifest.list.v2+json"
|
||||
//MediaTypeDDMv2 = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
)
|
||||
|
||||
@@ -12,9 +12,9 @@ import (
|
||||
|
||||
const (
|
||||
MediaTypeOIIv1 = "application/vnd.oci.image.index.v1+json"
|
||||
MediatypeDDMLv2 = "application/vnd.docker.distribution.manifest.list.v2+json"
|
||||
MediaTypeDDMLv2 = "application/vnd.docker.distribution.manifest.list.v2+json"
|
||||
|
||||
MediatypeDDMv2 = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
MediaTypeDDMv2 = "application/vnd.docker.distribution.manifest.v2+json"
|
||||
MediaTypeOIMv1 = "application/vnd.oci.image.manifest.v1+json"
|
||||
)
|
||||
|
||||
@@ -30,7 +30,7 @@ func NewDownloader(client *Client) *Downloader {
|
||||
|
||||
func (down *Downloader) Pull(ctx context.Context, rawref, dir, os, arch string) error {
|
||||
var err error
|
||||
ref, err := NewReference(rawref)
|
||||
ref, err := ParseReference(rawref)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -46,7 +46,7 @@ func (down *Downloader) Pull(ctx context.Context, rawref, dir, os, arch string)
|
||||
return err
|
||||
}
|
||||
|
||||
if mime == MediaTypeOIIv1 || mime == MediatypeDDMLv2 {
|
||||
if mime == MediaTypeOIIv1 || mime == MediaTypeDDMLv2 {
|
||||
var index ocispec.Index
|
||||
err = json.Unmarshal(man, &index)
|
||||
if err != nil {
|
||||
@@ -72,7 +72,7 @@ func (down *Downloader) Pull(ctx context.Context, rawref, dir, os, arch string)
|
||||
err = errors.New("Manifest not found")
|
||||
return err
|
||||
}
|
||||
if mime != MediaTypeOIMv1 && mime != MediatypeDDMv2 {
|
||||
if mime != MediaTypeOIMv1 && mime != MediaTypeDDMv2 {
|
||||
err = errors.New("Unknown manifest media type")
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ type Reference struct {
|
||||
base, tag string
|
||||
}
|
||||
|
||||
func NewReference(rawref string) (*Reference, error) {
|
||||
func ParseReference(rawref string) (*Reference, error) {
|
||||
ref := &Reference{}
|
||||
if !strings.Contains(rawref, "://") {
|
||||
rawref = "https://" + rawref
|
||||
@@ -54,3 +54,7 @@ func (ref *Reference) Repo() string {
|
||||
func (ref *Reference) Tag() string {
|
||||
return ref.tag
|
||||
}
|
||||
|
||||
func (ref *Reference) Userinfo() (string, string) {
|
||||
return ref.user, ref.pass
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user