From f221a799d8dd4b09380a6106fccba6e550650797 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Thu, 26 Oct 2023 16:46:15 -0400 Subject: [PATCH] command: error on invalid plugins remove If a user attempts to remove a plugin through the `packer plugins remove' subcommand, and the specified plugin is not installed, the command succeeds, but does nothing, and exits silently. This is not clear what is happening, and arguably, calling a command that does nothing, not even explain what went wrong, is not intuitive. Because of that, this commit changes how the command behaves in this case, stating what went wrong, and exiting with a non-zero status. --- command/plugins_remove.go | 9 +++++++++ command/plugins_remove_test.go | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/command/plugins_remove.go b/command/plugins_remove.go index 624a632e5..5c13636f5 100644 --- a/command/plugins_remove.go +++ b/command/plugins_remove.go @@ -104,5 +104,14 @@ func (c *PluginsRemoveCommand) RunContext(buildCtx context.Context, args []strin c.Ui.Message(installation.BinaryPath) } + if len(installations) == 0 { + errMsg := fmt.Sprintf("No installed plugin found matching the plugin constraints %s", args[0]) + if len(args) == 2 { + errMsg = fmt.Sprintf("%s %s", errMsg, args[1]) + } + c.Ui.Error(errMsg) + return 1 + } + return 0 } diff --git a/command/plugins_remove_test.go b/command/plugins_remove_test.go index dcaafb98e..4149ceaeb 100644 --- a/command/plugins_remove_test.go +++ b/command/plugins_remove_test.go @@ -47,7 +47,7 @@ func TestPluginsRemoveCommand_Run(t *testing.T) { expectedPackerConfigDirHashBeforeRemove: "h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=", packerConfigDir: cfg.dir("1_pkr_plugins_config"), pluginSourceArgs: []string{"github.com/sylviamoss/comment", "v0.2.19"}, - want: 0, + want: 1, dirFiles: nil, expectedPackerConfigDirHashAfterRemove: "h1:Q5qyAOdD43hL3CquQdVfaHpOYGf0UsZ/+wVA9Ry6cbA=", }, @@ -158,7 +158,7 @@ func TestPluginsRemoveCommand_Run(t *testing.T) { expectedPackerConfigDirHashBeforeRemove: "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", packerConfigDir: cfg.dir("3_pkr_plugins_config"), pluginSourceArgs: []string{"example.com/sylviamoss/comment", "v0.2.19"}, - want: 0, + want: 1, dirFiles: nil, expectedPackerConfigDirHashAfterRemove: "h1:47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=", },