Skip to content

Commit

Permalink
Default index function (#668)
Browse files Browse the repository at this point in the history
* Add function for getting default index URI

* Add variable in update command

* Update pkg/index/util.go

Co-authored-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>

* Add simple unit test for DefaultIndex function

* Code review change

Co-authored-by: Cornelius Weig <22861411+corneliusweig@users.noreply.github.com>
  • Loading branch information
chriskim06 and corneliusweig authored Nov 25, 2020
1 parent bb0fe8b commit 9749c9e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
6 changes: 4 additions & 2 deletions cmd/krew/cmd/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/krew/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()},
Expand Down
16 changes: 2 additions & 14 deletions pkg/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
28 changes: 28 additions & 0 deletions pkg/index/util.go
Original file line number Diff line number Diff line change
@@ -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
}
36 changes: 36 additions & 0 deletions pkg/index/util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}

0 comments on commit 9749c9e

Please sign in to comment.