From 8a17628da8aee3d901f7e2bd2445f39a379322c1 Mon Sep 17 00:00:00 2001 From: Patrick Zheng Date: Mon, 27 Nov 2023 10:35:44 +0800 Subject: [PATCH] update Signed-off-by: Patrick Zheng --- cmd/notation/plugin/uninstall.go | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/cmd/notation/plugin/uninstall.go b/cmd/notation/plugin/uninstall.go index 732c8f4ad..80822f784 100644 --- a/cmd/notation/plugin/uninstall.go +++ b/cmd/notation/plugin/uninstall.go @@ -20,9 +20,7 @@ import ( "os" "github.com/notaryproject/notation-go/dir" - "github.com/notaryproject/notation-go/log" "github.com/notaryproject/notation-go/plugin" - "github.com/notaryproject/notation-go/plugin/proto" "github.com/notaryproject/notation/cmd/notation/internal/cmdutil" "github.com/notaryproject/notation/internal/cmd" "github.com/spf13/cobra" @@ -70,40 +68,40 @@ Example - Uninstall plugin: func unInstallPlugin(command *cobra.Command, opts *pluginUninstallOpts) error { // set logger ctx := opts.LoggingFlagOpts.InitializeLogger(command.Context()) - logger := log.GetLogger(ctx) - pluginName := opts.pluginName - _, err := getPluginMetadataIfExist(ctx, pluginName) + exist, err := checkPluginExistence(ctx, pluginName) if err != nil { - if errors.Is(err, os.ErrNotExist) { // plugin does not exist - return fmt.Errorf("unable to find plugin %s.\nTo view a list of installed plugins, use `notation plugin list`", pluginName) - } - // plugin exists, but the binary is malfunctioning - logger.Infof("Uninstalling...Found plugin %s binary file is malfunctioning: %v", pluginName, err) + return fmt.Errorf("failed to check plugin existence: %w", err) + } + if !exist { + return fmt.Errorf("unable to find plugin %s.\nTo view a list of installed plugins, use `notation plugin list`", pluginName) } // core process prompt := fmt.Sprintf("Are you sure you want to uninstall plugin %q?", pluginName) confirmed, err := cmdutil.AskForConfirmation(os.Stdin, prompt, opts.confirmed) if err != nil { - return fmt.Errorf("failed when asking for confirmation: %v", err) + return fmt.Errorf("failed when asking for confirmation: %w", err) } if !confirmed { return nil } mgr := plugin.NewCLIManager(dir.PluginFS()) if err := mgr.Uninstall(ctx, pluginName); err != nil { - return fmt.Errorf("failed to uninstall %s: %v", pluginName, err) + return fmt.Errorf("failed to uninstall %s: %w", pluginName, err) } fmt.Printf("Successfully uninstalled plugin %s\n", pluginName) return nil } -// getPluginMetadataIfExist returns plugin's metadata if it exists in Notation -func getPluginMetadataIfExist(ctx context.Context, pluginName string) (*proto.GetMetadataResponse, error) { +// checkPluginExistence returns true if plugin exists in the system +func checkPluginExistence(ctx context.Context, pluginName string) (bool, error) { mgr := plugin.NewCLIManager(dir.PluginFS()) - plugin, err := mgr.Get(ctx, pluginName) + _, err := mgr.Get(ctx, pluginName) if err != nil { - return nil, err + if errors.Is(err, os.ErrNotExist) { // plugin does not exist + return false, nil + } + return false, err } - return plugin.GetMetadata(ctx, &proto.GetMetadataRequest{}) + return true, nil }