Repository: kubernetes-sigs/krew
Pull request #668 (2020-11-24 02:32:08)

Default index function
Adding this based on the comment here https://github.com/kubernetes-sigs/krew/pull/666#discussion_r528472892. This reverts back `constants.go` so that it only has the constant values and adds a `pkg/index/util.go` that has the `DefaultIndex` function in it.

/assign @ahmetb 
/assign @corneliusweig 

Related issue: #637 
<!-- For proposed features, make sure there's an issue it's discussed first -->


k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/krew/pull/668#" title="Author self-approved">chriskim06</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **soltysh** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @soltysh` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fkrew).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes-sigs/krew/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["soltysh"]} -->
k8s-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/krew/pull/668#" title="Author self-approved">chriskim06</a>*, *<a href="https://github.com/kubernetes-sigs/krew/pull/668#pullrequestreview-537818881" title="Approved">corneliusweig</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fkrew).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubernetes-sigs/krew/blob/master/OWNERS)~~ [corneliusweig]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
k8s-ci-robot: New changes are detected. LGTM label has been removed.
ahmetb: /approve

I'm now thinking if this option is worth documenting somewhere. We have other options like $KREW_ROOT that also are not documented, maybe in the future.
k8s-ci-robot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubernetes-sigs/krew/pull/668#issuecomment-733191896" title="Approved">ahmetb</a>*, *<a href="https://github.com/kubernetes-sigs/krew/pull/668#" title="Author self-approved">chriskim06</a>*, *<a href="https://github.com/kubernetes-sigs/krew/pull/668#pullrequestreview-537818881" title="Approved">corneliusweig</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fkrew).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubernetes-sigs/krew/blob/master/OWNERS)~~ [ahmetb,corneliusweig]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->
chriskim06: I was planning on adding the docs in a separate PR but if you guys think we don't need it for now I can get to it later on
corneliusweig: /lgtm
Release the hold of you want to do the documentation in a separate PR. 
chriskim06: Ya I'd like to do them in a separate PR, I'll be able to submit that PR this weekend.

/hold cancel

Patch:
diff --git a/cmd/krew/cmd/update.go b/cmd/krew/cmd/update.go
index 99b6aa9..ff3662f 100644
--- a/cmd/krew/cmd/update.go
+++ b/cmd/krew/cmd/update.go
@@ -30,6 +30,7 @@ import (
 	"sigs.k8s.io/krew/internal/index/indexscanner"
 	"sigs.k8s.io/krew/internal/installation"
 	"sigs.k8s.io/krew/pkg/constants"
+	"sigs.k8s.io/krew/pkg/index"
 )
 
 // updateCmd represents the update command
@@ -141,8 +142,9 @@ func ensureDefaultIndexIfNoneExist() error {
 	}
 
 	klog.V(3).Infof("No index found, add default index.")
-	fmt.Fprintf(os.Stderr, "Adding \"default\" plugin index from %s.\n", constants.DefaultIndexURI)
-	return errors.Wrap(indexoperations.AddIndex(paths, constants.DefaultIndexName, constants.DefaultIndexURI),
+	defaultIndex := index.DefaultIndex()
+	fmt.Fprintf(os.Stderr, "Adding \"default\" plugin index from %s.\n", defaultIndex)
+	return errors.Wrap(indexoperations.AddIndex(paths, constants.DefaultIndexName, defaultIndex),
 		"failed to add default plugin index in absence of no indexes")
 }
 
diff --git a/cmd/krew/cmd/version.go b/cmd/krew/cmd/version.go
index dc345dc..08caa8a 100644
--- a/cmd/krew/cmd/version.go
+++ b/cmd/krew/cmd/version.go
@@ -22,6 +22,7 @@ import (
 	"sigs.k8s.io/krew/internal/installation"
 	"sigs.k8s.io/krew/internal/version"
 	"sigs.k8s.io/krew/pkg/constants"
+	"sigs.k8s.io/krew/pkg/index"
 )
 
 // versionCmd represents the version command
@@ -42,7 +43,7 @@ Remarks:
 		conf := [][]string{
 			{"GitTag", version.GitTag()},
 			{"GitCommit", version.GitCommit()},
-			{"IndexURI", constants.DefaultIndexURI},
+			{"IndexURI", index.DefaultIndex()},
 			{"BasePath", paths.BasePath()},
 			{"IndexPath", paths.IndexPath(constants.DefaultIndexName)},
 			{"InstallPath", paths.InstallPath()},
diff --git a/pkg/constants/constants.go b/pkg/constants/constants.go
index 3b2ab79..2f39381 100644
--- a/pkg/constants/constants.go
+++ b/pkg/constants/constants.go
@@ -14,26 +14,14 @@
 
 package constants
 
-import "os"
-
 const (
 	CurrentAPIVersion = "krew.googlecontainertools.github.com/v1alpha2"
 	PluginKind        = "Plugin"
 	ManifestExtension = ".yaml"
 	KrewPluginName    = "krew" // plugin name of krew itself
 
+	// DefaultIndexURI points to the upstream index.
+	DefaultIndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
 	// DefaultIndexName is a magic string that's used for a plugin name specified without an index.
 	DefaultIndexName = "default"
-
-	// defaultIndexURI is the default krew-index URI that gets used unless it's overridden
-	defaultIndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
 )
-
-// DefaultIndexURI points to the upstream index.
-var DefaultIndexURI = defaultIndexURI
-
-func init() {
-	if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" {
-		DefaultIndexURI = uri
-	}
-}
diff --git a/pkg/index/util.go b/pkg/index/util.go
new file mode 100644
index 0000000..7480dd5
--- /dev/null
+++ b/pkg/index/util.go
@@ -0,0 +1,28 @@
+// Copyright 2020 The Kubernetes Authors.
+//
+// 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 index
+
+import (
+	"os"
+
+	"sigs.k8s.io/krew/pkg/constants"
+)
+
+func DefaultIndex() string {
+	if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" {
+		return uri
+	}
+	return constants.DefaultIndexURI
+}


Test patch:
diff --git a/pkg/index/util_test.go b/pkg/index/util_test.go
new file mode 100644
index 0000000..4c621b0
--- /dev/null
+++ b/pkg/index/util_test.go
@@ -0,0 +1,36 @@
+// Copyright 2020 The Kubernetes Authors.
+//
+// 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 index
+
+import (
+	"os"
+	"testing"
+
+	"sigs.k8s.io/krew/pkg/constants"
+)
+
+func TestDefaultIndex(t *testing.T) {
+	if got := DefaultIndex(); got != constants.DefaultIndexURI {
+		t.Errorf("DefaultIndex() = %q, want %q", got, constants.DefaultIndexURI)
+	}
+
+	want := "foo"
+	os.Setenv("KREW_DEFAULT_INDEX_URI", want)
+	defer os.Unsetenv("KREW_DEFAULT_INDEX_URI")
+
+	if got := DefaultIndex(); got != want {
+		t.Errorf("DefaultIndex() = %q, want %q", got, want)
+	}
+}