Skip to content

Commit

Permalink
command: error on invalid plugins remove
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lbajolet-hashicorp committed Oct 26, 2023
1 parent 71aa2f3 commit 5baf1f5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions command/plugins_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions command/plugins_remove_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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=",
},
Expand Down Expand Up @@ -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=",
},
Expand Down

0 comments on commit 5baf1f5

Please sign in to comment.