plugin commands should exit with a non-zero code on error. Closes #980

This commit is contained in:
Puskar Basu
2021-10-08 18:41:44 +05:30
committed by GitHub
parent 0a7b63ae7e
commit a495d7a843

View File

@@ -170,12 +170,19 @@ Example:
return cmd
}
// exitCode=1 For panics
// exitCode=2 For insufficient/wrong arguments passed in the command
// exitCode=3 For errors related to loading state, loading version data or an issue contacting
// the update server.
// exitCode=4 For plugin listing failures
func runPluginInstallCmd(cmd *cobra.Command, args []string) {
utils.LogTime("runPluginInstallCmd install")
defer func() {
utils.LogTime("runPluginInstallCmd end")
if r := recover(); r != nil {
utils.ShowError(helpers.ToError(r))
exitCode = 1
}
}()
@@ -191,6 +198,7 @@ func runPluginInstallCmd(cmd *cobra.Command, args []string) {
fmt.Println()
cmd.Help()
fmt.Println()
exitCode = 2
return
}
@@ -261,6 +269,7 @@ func runPluginUpdateCmd(cmd *cobra.Command, args []string) {
utils.LogTime("runPluginUpdateCmd end")
if r := recover(); r != nil {
utils.ShowError(helpers.ToError(r))
exitCode = 1
}
}()
// args to 'plugin update' -- one or more plugins to install
@@ -274,22 +283,25 @@ func runPluginUpdateCmd(cmd *cobra.Command, args []string) {
fmt.Println()
cmd.Help()
fmt.Println()
exitCode = 2
return
}
if len(plugins) > 0 && cmdconfig.Viper().GetBool("all") {
// we can't allow update and install at the same time
// we can't allow update and install at the same time
fmt.Println()
utils.ShowError(fmt.Errorf("%s cannot be used when updating specific plugins", constants.Bold("`--all`")))
fmt.Println()
cmd.Help()
fmt.Println()
exitCode = 2
return
}
state, err := statefile.LoadState()
if err != nil {
utils.ShowError(fmt.Errorf("could not load state"))
exitCode = 3
return
}
@@ -297,6 +309,7 @@ func runPluginUpdateCmd(cmd *cobra.Command, args []string) {
versionData, err := versionfile.LoadPluginVersionFile()
if err != nil {
utils.ShowError(fmt.Errorf("error loading current plugin data"))
exitCode = 3
return
}
@@ -350,6 +363,7 @@ func runPluginUpdateCmd(cmd *cobra.Command, args []string) {
// this happens if for some reason the update server could not be contacted,
// in which case we get back an empty map
utils.ShowError(fmt.Errorf("there was an issue contacting the update server. Please try later"))
exitCode = 3
return
}
@@ -454,18 +468,21 @@ func runPluginListCmd(*cobra.Command, []string) {
utils.LogTime("runPluginListCmd end")
if r := recover(); r != nil {
utils.ShowError(helpers.ToError(r))
exitCode = 1
}
}()
ctx := context.Background()
pluginConnectionMap, err := getPluginConnectionMap(ctx)
if err != nil {
utils.ShowErrorWithMessage(err, "Plugin Listing failed")
exitCode = 4
return
}
list, err := plugin.List(pluginConnectionMap)
if err != nil {
utils.ShowErrorWithMessage(err, "Plugin Listing failed")
exitCode = 4
}
headers := []string{"Name", "Version", "Connections"}
rows := [][]string{}
@@ -482,6 +499,7 @@ func runPluginUninstallCmd(cmd *cobra.Command, args []string) {
utils.LogTime("runPluginUninstallCmd end")
if r := recover(); r != nil {
utils.ShowError(helpers.ToError(r))
exitCode = 1
}
}()
@@ -491,12 +509,14 @@ func runPluginUninstallCmd(cmd *cobra.Command, args []string) {
fmt.Println()
cmd.Help()
fmt.Println()
exitCode = 2
return
}
ctx := context.Background()
connectionMap, err := getPluginConnectionMap(ctx)
if err != nil {
utils.ShowError(err)
exitCode = 4
return
}