Skip to content

Commit

Permalink
enable preparer interface to pre operations
Browse files Browse the repository at this point in the history
Signed-off-by: Batuhan Apaydın <[email protected]>
  • Loading branch information
developer-guy committed Feb 23, 2023
1 parent f23b6a0 commit 8011e9f
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 1 deletion.
1 change: 0 additions & 1 deletion pkg/commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package commands

import (
"fmt"

"github.com/google/ko/pkg/commands/options"
"github.com/spf13/cobra"
)
Expand Down
6 changes: 6 additions & 0 deletions pkg/commands/publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions pkg/publish/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

}
12 changes: 12 additions & 0 deletions pkg/publish/multi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 4 additions & 0 deletions pkg/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8011e9f

Please sign in to comment.