From 8c5a6c13c03a423428d7475004c4a2b7facb8eea Mon Sep 17 00:00:00 2001 From: Brian Bland Date: Thu, 14 Jan 2016 10:08:52 -0800 Subject: [PATCH] Splits up blob create options definitions to be package-specific Redefines privately in both storage and client packages Signed-off-by: Brian Bland --- registry/client/repository.go | 34 +++++++++++++++++++++++++++-- registry/client/repository_test.go | 3 +-- registry/storage/linkedblobstore.go | 8 +++---- 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/registry/client/repository.go b/registry/client/repository.go index c2aca03f8..d65212110 100644 --- a/registry/client/repository.go +++ b/registry/client/repository.go @@ -18,7 +18,6 @@ import ( "github.com/docker/distribution/reference" "github.com/docker/distribution/registry/api/v2" "github.com/docker/distribution/registry/client/transport" - "github.com/docker/distribution/registry/storage" "github.com/docker/distribution/registry/storage/cache" "github.com/docker/distribution/registry/storage/cache/memory" ) @@ -573,8 +572,39 @@ func (bs *blobs) Put(ctx context.Context, mediaType string, p []byte) (distribut return writer.Commit(ctx, desc) } +// createOptions is a collection of blob creation modifiers relevant to general +// blob storage intended to be configured by the BlobCreateOption.Apply method. +type createOptions struct { + Mount struct { + ShouldMount bool + From reference.Canonical + } +} + +type optionFunc func(interface{}) error + +func (f optionFunc) Apply(v interface{}) error { + return f(v) +} + +// WithMountFrom returns a BlobCreateOption which designates that the blob should be +// mounted from the given canonical reference. +func WithMountFrom(ref reference.Canonical) distribution.BlobCreateOption { + return optionFunc(func(v interface{}) error { + opts, ok := v.(*createOptions) + if !ok { + return fmt.Errorf("unexpected options type: %T", v) + } + + opts.Mount.ShouldMount = true + opts.Mount.From = ref + + return nil + }) +} + func (bs *blobs) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) { - var opts storage.CreateOptions + var opts createOptions for _, option := range options { err := option.Apply(&opts) diff --git a/registry/client/repository_test.go b/registry/client/repository_test.go index 811ab235f..bdd7ea20b 100644 --- a/registry/client/repository_test.go +++ b/registry/client/repository_test.go @@ -20,7 +20,6 @@ import ( "github.com/docker/distribution/manifest/schema1" "github.com/docker/distribution/reference" "github.com/docker/distribution/registry/api/errcode" - "github.com/docker/distribution/registry/storage" "github.com/docker/distribution/testutil" "github.com/docker/distribution/uuid" "github.com/docker/libtrust" @@ -523,7 +522,7 @@ func TestBlobMount(t *testing.T) { l := r.Blobs(ctx) - bw, err := l.Create(ctx, storage.WithMountFrom(canonicalRef)) + bw, err := l.Create(ctx, WithMountFrom(canonicalRef)) if bw != nil { t.Fatalf("Expected blob writer to be nil, was %v", bw) } diff --git a/registry/storage/linkedblobstore.go b/registry/storage/linkedblobstore.go index d7a9fd13c..a1f8724d7 100644 --- a/registry/storage/linkedblobstore.go +++ b/registry/storage/linkedblobstore.go @@ -97,9 +97,9 @@ func (lbs *linkedBlobStore) Put(ctx context.Context, mediaType string, p []byte) return desc, lbs.linkBlob(ctx, desc) } -// CreateOptions is a collection of blob creation modifiers relevant to general +// createOptions is a collection of blob creation modifiers relevant to general // blob storage intended to be configured by the BlobCreateOption.Apply method. -type CreateOptions struct { +type createOptions struct { Mount struct { ShouldMount bool From reference.Canonical @@ -116,7 +116,7 @@ func (f optionFunc) Apply(v interface{}) error { // mounted from the given canonical reference. func WithMountFrom(ref reference.Canonical) distribution.BlobCreateOption { return optionFunc(func(v interface{}) error { - opts, ok := v.(*CreateOptions) + opts, ok := v.(*createOptions) if !ok { return fmt.Errorf("unexpected options type: %T", v) } @@ -132,7 +132,7 @@ func WithMountFrom(ref reference.Canonical) distribution.BlobCreateOption { func (lbs *linkedBlobStore) Create(ctx context.Context, options ...distribution.BlobCreateOption) (distribution.BlobWriter, error) { context.GetLogger(ctx).Debug("(*linkedBlobStore).Writer") - var opts CreateOptions + var opts createOptions for _, option := range options { err := option.Apply(&opts)