-
Notifications
You must be signed in to change notification settings - Fork 369
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add index migration code behind an environment variable This only changes the directory structure that will be needed to support custom indexes. The other changes will be in subsequent PRs. * Check to see if the migration has already happened * Fix error and linting issue * Code review changes * Make migrated test case a little more explicit * Add migration check to root behind env var * Update wording around out of date index message * Add success message after migrate * Fix typo
- Loading branch information
1 parent
fb5343a
commit df44611
Showing
4 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright 2019 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 indexmigration | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/klog" | ||
|
||
"sigs.k8s.io/krew/internal/environment" | ||
"sigs.k8s.io/krew/internal/gitutil" | ||
"sigs.k8s.io/krew/pkg/constants" | ||
) | ||
|
||
// Done checks if the krew installation requires a migration to support multiple indexes. | ||
// A migration is necessary when the index directory contains a ".git" directory. | ||
func Done(paths environment.Paths) (bool, error) { | ||
_, err := os.Stat(filepath.Join(paths.IndexPath(), ".git")) | ||
if err == nil { | ||
return false, nil | ||
} else if os.IsNotExist(err) { | ||
return true, nil | ||
} | ||
return false, err | ||
} | ||
|
||
// Migrate removes the index directory and then clones krew-index to the new default index path. | ||
func Migrate(paths environment.Paths) error { | ||
isMigrated, err := Done(paths) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to check if index migration is complete") | ||
} | ||
if isMigrated { | ||
klog.V(2).Infoln("Already migrated") | ||
return nil | ||
} | ||
|
||
err = os.RemoveAll(paths.IndexPath()) | ||
if err != nil { | ||
return errors.Wrapf(err, "could not remove index directory %q", paths.IndexPath()) | ||
} | ||
err = gitutil.EnsureCloned(constants.IndexURI, filepath.Join(paths.IndexPath(), "default")) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to clone index") | ||
} | ||
klog.Infof("Migration completed successfully.") | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright 2019 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 indexmigration | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"sigs.k8s.io/krew/internal/environment" | ||
"sigs.k8s.io/krew/internal/testutil" | ||
) | ||
|
||
func TestIsMigrated(t *testing.T) { | ||
if _, ok := os.LookupEnv("X_KREW_ENABLE_MULTI_INDEX"); !ok { | ||
t.Skip("Set X_KREW_ENABLE_MULTI_INDEX variable to run this test") | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
dirPath string | ||
expected bool | ||
}{ | ||
{ | ||
name: "Already migrated", | ||
dirPath: "index/default/.git", | ||
expected: true, | ||
}, | ||
{ | ||
name: "Not migrated", | ||
dirPath: "index/.git", | ||
expected: false, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
tmpDir, cleanup := testutil.NewTempDir(t) | ||
defer cleanup() | ||
|
||
err := os.MkdirAll(tmpDir.Path(test.dirPath), os.ModePerm) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
newPaths := environment.NewPaths(tmpDir.Root()) | ||
actual, err := Done(newPaths) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if actual != test.expected { | ||
t.Errorf("Expected %v but found %v", test.expected, actual) | ||
} | ||
}) | ||
} | ||
} |