Skip to content

Commit

Permalink
Allow to customize publish.Namer (#477)
Browse files Browse the repository at this point in the history
* Allow to customize publish.Namer

Fixes #476

* Removing a CLI flag for path separator

* Removing ImageNameSeparator option after review
  • Loading branch information
cardil committed Dec 14, 2021
1 parent a56047a commit 84e8ab6
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 3 deletions.
72 changes: 72 additions & 0 deletions pkg/commands/options/namer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2021 Google LLC All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package options_test

import (
"path"
"testing"

"github.com/google/ko/pkg/commands/options"
)

func TestMakeNamer(t *testing.T) {
foreachTestCaseMakeNamer(func(tc testMakeNamerCase) {
t.Run(tc.name, func(t *testing.T) {
namer := options.MakeNamer(&tc.opts)
got := namer("registry.example.org/foo/bar", "example.org/sample/cmd/example")

if got != tc.want {
t.Errorf("got image name %s, wanted %s", got, tc.want)
}
})
})
}

func foreachTestCaseMakeNamer(fn func(tc testMakeNamerCase)) {
for _, namerCase := range testMakeNamerCases() {
fn(namerCase)
}
}

func testMakeNamerCases() []testMakeNamerCase {
return []testMakeNamerCase{{
name: "defaults",
want: "registry.example.org/foo/bar/example-51d74b7127c5f7495a338df33ecdeb19",
}, {
name: "with preserve import paths",
want: "registry.example.org/foo/bar/example.org/sample/cmd/example",
opts: options.PublishOptions{PreserveImportPaths: true},
}, {
name: "with base import paths",
want: "registry.example.org/foo/bar/example",
opts: options.PublishOptions{BaseImportPaths: true},
}, {
name: "with bare",
want: "registry.example.org/foo/bar",
opts: options.PublishOptions{Bare: true},
}, {
name: "with custom namer",
want: "registry.example.org/foo/bar-example",
opts: options.PublishOptions{ImageNamer: func(base string, importpath string) string {
return base + "-" + path.Base(importpath)
}},
}}
}

type testMakeNamerCase struct {
name string
opts options.PublishOptions
want string
}
11 changes: 8 additions & 3 deletions pkg/commands/options/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ limitations under the License.
package options

import (
"crypto/md5" //nolint: gosec // No strong cryptography needed.
"crypto/md5" // nolint: gosec // No strong cryptography needed.
"encoding/hex"
"os"
"path"
Expand Down Expand Up @@ -65,6 +65,9 @@ type PublishOptions struct {
BaseImportPaths bool
// Bare uses a tag on the KO_DOCKER_REPO without anything additional.
Bare bool
// ImageNamer can be used to pass a custom image name function. When given
// PreserveImportPaths, BaseImportPaths, Bare has no effect.
ImageNamer publish.Namer
}

func AddPublishArg(cmd *cobra.Command, po *PublishOptions) {
Expand Down Expand Up @@ -99,7 +102,7 @@ func AddPublishArg(cmd *cobra.Command, po *PublishOptions) {
}

func packageWithMD5(base, importpath string) string {
hasher := md5.New() //nolint: gosec // No strong cryptography needed.
hasher := md5.New() // nolint: gosec // No strong cryptography needed.
hasher.Write([]byte(importpath))
return path.Join(base, path.Base(importpath)+"-"+hex.EncodeToString(hasher.Sum(nil)))
}
Expand All @@ -117,7 +120,9 @@ func bareDockerRepo(base, _ string) string {
}

func MakeNamer(po *PublishOptions) publish.Namer {
if po.PreserveImportPaths {
if po.ImageNamer != nil {
return po.ImageNamer
} else if po.PreserveImportPaths {
return preserveImportPath
} else if po.BaseImportPaths {
return baseImportPaths
Expand Down

0 comments on commit 84e8ab6

Please sign in to comment.