-
Notifications
You must be signed in to change notification settings - Fork 370
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
cmd: Update each index instead of just default #588
Changes from 6 commits
d9f30f8
de38a18
6717fe8
a4c94da
0e31f9a
c674426
993758c
8a8195d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -19,12 +19,14 @@ import ( | |||||||||||||
"fmt" | ||||||||||||||
"io" | ||||||||||||||
"os" | ||||||||||||||
"strings" | ||||||||||||||
|
||||||||||||||
"github.com/pkg/errors" | ||||||||||||||
"github.com/spf13/cobra" | ||||||||||||||
"k8s.io/klog" | ||||||||||||||
|
||||||||||||||
"sigs.k8s.io/krew/internal/gitutil" | ||||||||||||||
"sigs.k8s.io/krew/internal/index/indexoperations" | ||||||||||||||
"sigs.k8s.io/krew/internal/index/indexscanner" | ||||||||||||||
"sigs.k8s.io/krew/internal/installation" | ||||||||||||||
"sigs.k8s.io/krew/pkg/constants" | ||||||||||||||
|
@@ -43,7 +45,7 @@ plugin index from the internet. | |||||||||||||
Remarks: | ||||||||||||||
You don't need to run this command: Running "krew update" or "krew upgrade" | ||||||||||||||
will silently run this command.`, | ||||||||||||||
RunE: ensureIndexUpdated, | ||||||||||||||
RunE: ensureIndexesUpdated, | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func showFormattedPluginsInfo(out io.Writer, header string, plugins []string) { | ||||||||||||||
|
@@ -102,13 +104,24 @@ func showUpdatedPlugins(out io.Writer, preUpdate, posUpdate []index.Plugin, inst | |||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
func ensureIndexUpdated(_ *cobra.Command, _ []string) error { | ||||||||||||||
func ensureIndexesUpdated(_ *cobra.Command, _ []string) error { | ||||||||||||||
preUpdateIndex, _ := indexscanner.LoadPluginListFromFS(paths.IndexPluginsPath(constants.DefaultIndexName)) | ||||||||||||||
|
||||||||||||||
klog.V(1).Infof("Updating the local copy of plugin index (%s)", paths.IndexPath(constants.DefaultIndexName)) | ||||||||||||||
if err := gitutil.EnsureUpdated(constants.DefaultIndexURI, paths.IndexPath(constants.DefaultIndexName)); err != nil { | ||||||||||||||
return errors.Wrap(err, "failed to update the local index") | ||||||||||||||
indexes, err := indexoperations.ListIndexes(paths) | ||||||||||||||
if err != nil { | ||||||||||||||
return errors.Wrap(err, "failed to list indexes") | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
var failed []string | ||||||||||||||
for _, idx := range indexes { | ||||||||||||||
indexPath := paths.IndexPath(idx.Name) | ||||||||||||||
klog.V(1).Infof("Updating the local copy of plugin index (%s)", indexPath) | ||||||||||||||
if err := gitutil.EnsureUpdated(idx.URL, indexPath); err != nil { | ||||||||||||||
klog.V(1).Infof("error updating index %s: %s", idx.Name, err) | ||||||||||||||
failed = append(failed, idx.Name) | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
fmt.Fprintln(os.Stderr, "Updated the local copy of plugin index.") | ||||||||||||||
|
||||||||||||||
if len(preUpdateIndex) == 0 { | ||||||||||||||
|
@@ -132,6 +145,9 @@ func ensureIndexUpdated(_ *cobra.Command, _ []string) error { | |||||||||||||
// TODO(chriskim06) consider commenting this out when refactoring for custom indexes | ||||||||||||||
showUpdatedPlugins(os.Stderr, preUpdateIndex, posUpdateIndex, installedPlugins) | ||||||||||||||
|
||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’ve realized this if (+counting) isnt necessary since errors.Wrap already handles nil err. Maybe remove? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure I can do that. Do you think it will look weird if this ends with the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it’s fine. I just realized the check is redundant. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok I'll remove the check then. |
||||||||||||||
if len(failed) != 0 { | ||||||||||||||
return errors.Errorf("failed to update the following indexes: %s\n", strings.Join(failed, ", ")) | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something else we do in install.go is that we collect the first error as Lines 161 to 163 in b2b8a40
Lines 178 to 180 in b2b8a40
The reason for this is that when you wrap+return errors, when executed with |
||||||||||||||
} | ||||||||||||||
return nil | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,50 @@ func TestKrewUpdate(t *testing.T) { | |
} | ||
} | ||
|
||
func TestKrewUpdateMultipleIndexes(t *testing.T) { | ||
skipShort(t) | ||
test, cleanup := NewTest(t) | ||
defer cleanup() | ||
|
||
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex() | ||
os.Setenv(constants.EnableMultiIndexSwitch, "1") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a comment like // to enable new paths in environment.NewPaths() |
||
defer os.Unsetenv(constants.EnableMultiIndexSwitch) | ||
|
||
paths := environment.NewPaths(test.Root()) | ||
test.Krew("index", "add", "foo", paths.IndexPath(constants.DefaultIndexName)).RunOrFail() | ||
if err := os.RemoveAll(paths.IndexPluginsPath("foo")); err != nil { | ||
t.Errorf("error removing plugins directory from index: %s", err) | ||
} | ||
test.Krew("update").RunOrFailOutput() | ||
out, err := ioutil.ReadDir(paths.IndexPluginsPath("foo")) | ||
if err != nil { | ||
t.Errorf("error reading plugins directory: %s", err) | ||
} | ||
if len(out) == 0 { | ||
t.Error("no plugins in index foo after update") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't you be also checking what's in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ya good call 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it should be enough to look for two plugin names in the output. For example, |
||
} | ||
|
||
func TestKrewUpdateFailedIndex(t *testing.T) { | ||
skipShort(t) | ||
test, cleanup := NewTest(t) | ||
defer cleanup() | ||
|
||
test = test.WithEnv(constants.EnableMultiIndexSwitch, 1).WithIndex() | ||
os.Setenv(constants.EnableMultiIndexSwitch, "1") | ||
defer os.Unsetenv(constants.EnableMultiIndexSwitch) | ||
|
||
paths := environment.NewPaths(test.Root()) | ||
test.TempDir().InitEmptyGitRepo(paths.IndexPath("foo"), "invalid-git") | ||
out, err := test.Krew("update").Run() | ||
if err == nil { | ||
t.Error("expected update to fail") | ||
} | ||
if !strings.Contains(string(out), "failed to update the following indexes: foo") { | ||
t.Error("expected index update to fail for foo") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try doing this for debuggability: %q doesn’t contain msg=%q There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What are the two strings in the example you gave? I'm guessing the output of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Out & “failed to ...foo” You can export latter into a variable. |
||
} | ||
} | ||
|
||
func TestKrewUpdateListsNewPlugins(t *testing.T) { | ||
skipShort(t) | ||
test, cleanup := NewTest(t) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should probably do this as
klog.Warningf
otherwise the user will never know (unless they use -v) what the error is.See how we handle multiple plugin install failures in
install.go
.krew/cmd/krew/cmd/install.go
Line 160 in b2b8a40