working commit
This commit is contained in:
+20
-10
@@ -24,20 +24,21 @@ import (
|
||||
"mstore/pkg/auxhttp"
|
||||
)
|
||||
|
||||
func (cli *Client) FileExists(ctx context.Context, fileuri string) (bool, error) {
|
||||
var res bool
|
||||
func (cli *Client) FileInfo(ctx context.Context, fileuri string) (bool, *descr.File, error) {
|
||||
var exists bool
|
||||
var err error
|
||||
file := &descr.File{}
|
||||
fileuri, username, password, err := repackServiceURI(fileuri)
|
||||
if err != nil {
|
||||
return res, err
|
||||
return exists, file, err
|
||||
}
|
||||
fileuri, err = convertFileURI(fileuri)
|
||||
if err != nil {
|
||||
return res, err
|
||||
return exists, file, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, fileuri, nil)
|
||||
if err != nil {
|
||||
return res, err
|
||||
return exists, file, err
|
||||
}
|
||||
if username != "" && password != "" {
|
||||
basic := auxhttp.EncodeBasicAuth(username, password)
|
||||
@@ -46,13 +47,23 @@ func (cli *Client) FileExists(ctx context.Context, fileuri string) (bool, error)
|
||||
client := makeHTTPClient()
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return res, err
|
||||
return exists, file, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
res = true
|
||||
file.Collection = resp.Header.Get("Content-Collection")
|
||||
file.Name = resp.Header.Get("Content-Name")
|
||||
contentSize := resp.Header.Get("Content-Size")
|
||||
size, err := strconv.ParseInt(contentSize, 10, 64)
|
||||
if err != nil {
|
||||
return exists, file, err
|
||||
}
|
||||
file.Size = size
|
||||
file.Type = resp.Header.Get("Content-Type")
|
||||
file.Checksum = resp.Header.Get("Content-Digest")
|
||||
exists = true
|
||||
}
|
||||
return res, err
|
||||
return exists, file, err
|
||||
}
|
||||
|
||||
func (cli *Client) PutFile(ctx context.Context, filename, fileuri string) error {
|
||||
@@ -164,7 +175,7 @@ func (cli *Client) GetFile(ctx context.Context, fileuri, filename string) (int64
|
||||
return size, err
|
||||
}
|
||||
|
||||
func (cli *Client) DeleteFile(ctx context.Context, fileuri, filename string) error {
|
||||
func (cli *Client) DeleteFile(ctx context.Context, fileuri string) error {
|
||||
var err error
|
||||
|
||||
fileuri, username, password, err := repackServiceURI(fileuri)
|
||||
@@ -247,7 +258,6 @@ func (cli *Client) ListFiles(ctx context.Context, catalogURI string) ([]descr.Fi
|
||||
err := fmt.Errorf("Mismatch Content-Length and recorded filesize")
|
||||
return res, err
|
||||
}
|
||||
fmt.Printf("list: %s\n",string(respBytes))
|
||||
err = json.Unmarshal(respBytes, &res)
|
||||
if err != nil {
|
||||
return res, err
|
||||
|
||||
@@ -101,9 +101,10 @@ func TestFileLife(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
|
||||
|
||||
exists, err := cli.FileExists(ctx, srvaddr+"/foo.bin")
|
||||
exists, file, err := cli.FileInfo(ctx, srvaddr+"/foo.bin")
|
||||
require.NoError(t, err)
|
||||
require.True(t, exists)
|
||||
require.NotNil(t, file)
|
||||
}
|
||||
{
|
||||
// GetFile
|
||||
@@ -151,9 +152,10 @@ func TestFileLife(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
ctx, _ = context.WithTimeout(ctx, 1*time.Second)
|
||||
|
||||
exists, err := cli.FileExists(ctx, srvaddr+"/foo.bin")
|
||||
exists, _, err := cli.FileInfo(ctx, srvaddr+"/foo.bin")
|
||||
require.NoError(t, err)
|
||||
require.False(t, exists)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,6 @@ package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/go-containerregistry/pkg/authn"
|
||||
"github.com/google/go-containerregistry/pkg/crane"
|
||||
@@ -27,7 +26,7 @@ type ImageDescr struct {
|
||||
Tags []string `json:"avilableTags,omitempty"`
|
||||
}
|
||||
|
||||
func (cli *Client) ImageInfo(ctx context.Context, imagepath string, timeout time.Duration) (*ImageDescr, error) {
|
||||
func (cli *Client) ImageInfo(ctx context.Context, imagepath string) (*ImageDescr, error) {
|
||||
var err error
|
||||
res := &ImageDescr{}
|
||||
|
||||
@@ -35,7 +34,6 @@ func (cli *Client) ImageInfo(ctx context.Context, imagepath string, timeout time
|
||||
if err != nil {
|
||||
return res, err
|
||||
}
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
options := make([]crane.Option, 0)
|
||||
options = append(options, crane.WithContext(ctx))
|
||||
|
||||
|
||||
@@ -19,9 +19,10 @@ import (
|
||||
"mstore/app/server"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
"sigs.k8s.io/yaml"
|
||||
)
|
||||
|
||||
func xxxTestImageLife(t *testing.T) {
|
||||
func TestImageLife(t *testing.T) {
|
||||
var srvport int64 = 10250
|
||||
srvdir := t.TempDir()
|
||||
srvaddr := fmt.Sprintf("127.0.0.1:%d", srvport)
|
||||
@@ -72,14 +73,24 @@ func xxxTestImageLife(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.True(t, helloRes)
|
||||
}
|
||||
|
||||
{
|
||||
// PishImage
|
||||
fmt.Printf("=== PushImage ===\n")
|
||||
cli := NewClient()
|
||||
ctx := context.Background()
|
||||
err := cli.PushImage(ctx, "test-oci.img", srvaddr+"/foo/test:123", 1*time.Second)
|
||||
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
err := cli.PushImage(ctx, "test-oci.img", srvaddr+"/foo/test:123")
|
||||
require.NoError(t, err)
|
||||
}
|
||||
{
|
||||
// ImageInfo
|
||||
fmt.Printf("=== ImageInfo ===\n")
|
||||
cli := NewClient()
|
||||
ctx, _ := context.WithTimeout(context.Background(), 1*time.Second)
|
||||
info, err := cli.ImageInfo(ctx, srvaddr+"/foo/test:123")
|
||||
require.NoError(t, err)
|
||||
infoYaml, err := yaml.Marshal(info)
|
||||
require.NoError(t, err)
|
||||
fmt.Printf("imageInfo:\n%s\n", string(infoYaml))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,7 +12,6 @@ package client
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"mstore/pkg/auxtool"
|
||||
"mstore/pkg/auxutar"
|
||||
@@ -23,9 +22,8 @@ import (
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
||||
func (cli *Client) PullImage(ctx context.Context, filepath, imagepath string, timeout time.Duration) error {
|
||||
func (cli *Client) PullImage(ctx context.Context, filepath, imagepath string) error {
|
||||
var err error
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
|
||||
imagepath, username, password, err := repackReference(imagepath)
|
||||
if err != nil {
|
||||
|
||||
@@ -13,7 +13,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"mstore/pkg/auxtool"
|
||||
"mstore/pkg/auxutar"
|
||||
@@ -26,15 +25,13 @@ import (
|
||||
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
|
||||
)
|
||||
|
||||
func (cli *Client) PushImage(ctx context.Context, filepath, imagepath string, timeout time.Duration) error {
|
||||
func (cli *Client) PushImage(ctx context.Context, filepath, imagepath string) error {
|
||||
var err error
|
||||
ctx, _ = context.WithTimeout(ctx, timeout)
|
||||
|
||||
imagepath, username, password, err := repackReference(imagepath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
options := make([]crane.Option, 0)
|
||||
options = append(options, crane.WithContext(ctx))
|
||||
if username != "" && password != "" {
|
||||
|
||||
Reference in New Issue
Block a user