Skip to content

Commit

Permalink
Add flag and PublishOption for destination repo (#351)
Browse files Browse the repository at this point in the history
* Add flag and PublishOption for destination repo

This enables programmatically setting the destination image repository
when embedding ko's `publish` functionality in other tools.

See #348

* Set DockerRepo PublishOption from KO_DOCKER_REPO

This enables programmatically setting the destination image repository
and avoids exposing a flag.

* Update comment on DockerRepo option

* Fix readme and copyright headers
  • Loading branch information
halvards authored Apr 30, 2021
1 parent 37aef60 commit 516cdee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
39 changes: 26 additions & 13 deletions pkg/commands/options/publish.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
// Copyright 2018 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.
/*
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

import (
"crypto/md5" //nolint: gosec // No strong cryptography needed.
"encoding/hex"
"os"
"path"

"github.com/google/ko/pkg/publish"
Expand All @@ -25,6 +28,10 @@ import (

// PublishOptions encapsulates options when publishing.
type PublishOptions struct {
// DockerRepo configures the destination image repository.
// In normal ko usage, this is populated with the value of $KO_DOCKER_REPO.
DockerRepo string

Tags []string

// Push publishes images to a registry.
Expand All @@ -46,6 +53,12 @@ type PublishOptions struct {
}

func AddPublishArg(cmd *cobra.Command, po *PublishOptions) {
// Set DockerRepo from the KO_DOCKER_REPO envionment variable.
// See https://github.com/google/ko/pull/351 for flag discussion.
if dockerRepo, exists := os.LookupEnv("KO_DOCKER_REPO"); exists {
po.DockerRepo = dockerRepo
}

cmd.Flags().StringSliceVarP(&po.Tags, "tags", "t", []string{"latest"},
"Which tags to use for the produced image instead of the default 'latest' tag "+
"(may not work properly with --base-import-paths or --bare).")
Expand Down
30 changes: 16 additions & 14 deletions pkg/commands/resolver.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
// Copyright 2018 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.
/*
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 commands

Expand Down Expand Up @@ -131,7 +133,7 @@ func makePublisher(po *options.PublishOptions) (publish.Interface, error) {
// Create the publish.Interface that we will use to publish image references
// to either a docker daemon or a container image registry.
innerPublisher, err := func() (publish.Interface, error) {
repoName := os.Getenv("KO_DOCKER_REPO")
repoName := po.DockerRepo
namer := options.MakeNamer(po)
if repoName == publish.LocalDomain || po.Local {
// TODO(jonjohnsonjr): I'm assuming that nobody will
Expand Down

0 comments on commit 516cdee

Please sign in to comment.