Skip to content

Commit 9749c9e

Browse files
Default index function (#668)
* Add function for getting default index URI * Add variable in update command * Update pkg/index/util.go Co-authored-by: Cornelius Weig <[email protected]> * Add simple unit test for DefaultIndex function * Code review change Co-authored-by: Cornelius Weig <[email protected]>
1 parent bb0fe8b commit 9749c9e

File tree

5 files changed

+72
-17
lines changed

5 files changed

+72
-17
lines changed

cmd/krew/cmd/update.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"sigs.k8s.io/krew/internal/index/indexscanner"
3131
"sigs.k8s.io/krew/internal/installation"
3232
"sigs.k8s.io/krew/pkg/constants"
33+
"sigs.k8s.io/krew/pkg/index"
3334
)
3435

3536
// updateCmd represents the update command
@@ -141,8 +142,9 @@ func ensureDefaultIndexIfNoneExist() error {
141142
}
142143

143144
klog.V(3).Infof("No index found, add default index.")
144-
fmt.Fprintf(os.Stderr, "Adding \"default\" plugin index from %s.\n", constants.DefaultIndexURI)
145-
return errors.Wrap(indexoperations.AddIndex(paths, constants.DefaultIndexName, constants.DefaultIndexURI),
145+
defaultIndex := index.DefaultIndex()
146+
fmt.Fprintf(os.Stderr, "Adding \"default\" plugin index from %s.\n", defaultIndex)
147+
return errors.Wrap(indexoperations.AddIndex(paths, constants.DefaultIndexName, defaultIndex),
146148
"failed to add default plugin index in absence of no indexes")
147149
}
148150

cmd/krew/cmd/version.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sigs.k8s.io/krew/internal/installation"
2323
"sigs.k8s.io/krew/internal/version"
2424
"sigs.k8s.io/krew/pkg/constants"
25+
"sigs.k8s.io/krew/pkg/index"
2526
)
2627

2728
// versionCmd represents the version command
@@ -42,7 +43,7 @@ Remarks:
4243
conf := [][]string{
4344
{"GitTag", version.GitTag()},
4445
{"GitCommit", version.GitCommit()},
45-
{"IndexURI", constants.DefaultIndexURI},
46+
{"IndexURI", index.DefaultIndex()},
4647
{"BasePath", paths.BasePath()},
4748
{"IndexPath", paths.IndexPath(constants.DefaultIndexName)},
4849
{"InstallPath", paths.InstallPath()},

pkg/constants/constants.go

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,14 @@
1414

1515
package constants
1616

17-
import "os"
18-
1917
const (
2018
CurrentAPIVersion = "krew.googlecontainertools.github.com/v1alpha2"
2119
PluginKind = "Plugin"
2220
ManifestExtension = ".yaml"
2321
KrewPluginName = "krew" // plugin name of krew itself
2422

23+
// DefaultIndexURI points to the upstream index.
24+
DefaultIndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
2525
// DefaultIndexName is a magic string that's used for a plugin name specified without an index.
2626
DefaultIndexName = "default"
27-
28-
// defaultIndexURI is the default krew-index URI that gets used unless it's overridden
29-
defaultIndexURI = "https://github.com/kubernetes-sigs/krew-index.git"
3027
)
31-
32-
// DefaultIndexURI points to the upstream index.
33-
var DefaultIndexURI = defaultIndexURI
34-
35-
func init() {
36-
if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" {
37-
DefaultIndexURI = uri
38-
}
39-
}

pkg/index/util.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2020 The Kubernetes Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package index
16+
17+
import (
18+
"os"
19+
20+
"sigs.k8s.io/krew/pkg/constants"
21+
)
22+
23+
func DefaultIndex() string {
24+
if uri := os.Getenv("KREW_DEFAULT_INDEX_URI"); uri != "" {
25+
return uri
26+
}
27+
return constants.DefaultIndexURI
28+
}

pkg/index/util_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2020 The Kubernetes Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package index
16+
17+
import (
18+
"os"
19+
"testing"
20+
21+
"sigs.k8s.io/krew/pkg/constants"
22+
)
23+
24+
func TestDefaultIndex(t *testing.T) {
25+
if got := DefaultIndex(); got != constants.DefaultIndexURI {
26+
t.Errorf("DefaultIndex() = %q, want %q", got, constants.DefaultIndexURI)
27+
}
28+
29+
want := "foo"
30+
os.Setenv("KREW_DEFAULT_INDEX_URI", want)
31+
defer os.Unsetenv("KREW_DEFAULT_INDEX_URI")
32+
33+
if got := DefaultIndex(); got != want {
34+
t.Errorf("DefaultIndex() = %q, want %q", got, want)
35+
}
36+
}

0 commit comments

Comments
 (0)