Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default index function #668

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a mini unit-test would be great for this.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for sure, I'll add that in

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("expected DefaultIndex() to be %q; got=%q", constants.DefaultIndexURI, got)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.Errorf("expected DefaultIndex() to be %q; got=%q", constants.DefaultIndexURI, got)
t.Errorf("DefaultIndex() = %q, want %q", got, constants.DefaultIndexURI)

Same below.

}

want := "foo"
os.Setenv("KREW_DEFAULT_INDEX_URI", want)
defer os.Unsetenv("KREW_DEFAULT_INDEX_URI")

if got := DefaultIndex(); got != want {
t.Errorf("expected DefaultIndex() to be %q; got=%q", want, got)
}
}