From 8011e9fbdb7b4ba2c682773eba7b73c5326edaf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Batuhan=20Apayd=C4=B1n?= Date: Thu, 23 Feb 2023 23:19:30 +0300 Subject: [PATCH] enable preparer interface to pre operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Batuhan Apaydın --- pkg/commands/build.go | 1 - pkg/commands/publisher.go | 6 ++++++ pkg/publish/default.go | 22 ++++++++++++++++++++++ pkg/publish/multi.go | 12 ++++++++++++ pkg/publish/publish.go | 4 ++++ 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/pkg/commands/build.go b/pkg/commands/build.go index 5284761364..1915d2b486 100644 --- a/pkg/commands/build.go +++ b/pkg/commands/build.go @@ -16,7 +16,6 @@ package commands import ( "fmt" - "github.com/google/ko/pkg/commands/options" "github.com/spf13/cobra" ) diff --git a/pkg/commands/publisher.go b/pkg/commands/publisher.go index 4f4fae657f..babec41cb5 100644 --- a/pkg/commands/publisher.go +++ b/pkg/commands/publisher.go @@ -41,6 +41,12 @@ func publishImages(ctx context.Context, importpaths []string, pub publish.Interf return nil, fmt.Errorf("importpath %q is not supported: %w", importpath, err) } + if ppub, ok := pub.(publish.Preparer); ok { + if err := ppub.Prepare(ctx, importpath); err != nil { + return nil, fmt.Errorf("error preparing publisher: %w", err) + } + } + img, err := b.Build(ctx, importpath) if err != nil { return nil, fmt.Errorf("error building %q: %w", importpath, err) diff --git a/pkg/publish/default.go b/pkg/publish/default.go index 343738d6b9..db6088d1e4 100644 --- a/pkg/publish/default.go +++ b/pkg/publish/default.go @@ -260,3 +260,25 @@ func (d *defalt) Publish(ctx context.Context, br build.Result, s string) (name.R func (d *defalt) Close() error { return nil } + +// Prepare implements publish.Preparer +func (d *defalt) Prepare(ctx context.Context, s string) error { + var no []name.Option + if d.insecure { + no = append(no, name.Insecure) + } + + for _, tagName := range d.tags { + tag, err := name.NewTag(fmt.Sprintf("%s:%s", d.namer(d.base, s), tagName), no...) + if err != nil { + return err + } + + if err := remote.CheckPushPermission(tag, d.keychain, d.t); err != nil { + return err + } + } + + return nil + +} diff --git a/pkg/publish/multi.go b/pkg/publish/multi.go index 38c47321e0..0cdafaf381 100644 --- a/pkg/publish/multi.go +++ b/pkg/publish/multi.go @@ -58,3 +58,15 @@ func (p *multiPublisher) Close() (err error) { } return } + +// Prepare implements publish.Preparer. +func (p *multiPublisher) Prepare(ctx context.Context, s string) (err error) { + for _, pub := range p.publishers { + if ppub, ok := pub.(Preparer); ok { + if perr := ppub.Prepare(ctx, s); perr != nil { + err = perr + } + } + } + return +} diff --git a/pkg/publish/publish.go b/pkg/publish/publish.go index 025669fdf7..0618500271 100644 --- a/pkg/publish/publish.go +++ b/pkg/publish/publish.go @@ -21,6 +21,10 @@ import ( "github.com/google/ko/pkg/build" ) +type Preparer interface { + Prepare(context.Context, string) error +} + // Interface abstracts different methods for publishing images. type Interface interface { // Publish uploads the given build.Result to a registry incorporating the