Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts committed Nov 27, 2023
1 parent 51caed0 commit 8a17628
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions cmd/notation/plugin/uninstall.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 8a17628

Please sign in to comment.