Skip to content

Commit

Permalink
added uninstall
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <patrickzheng@microsoft.com>
  • Loading branch information
Two-Hearts committed Oct 23, 2023
1 parent d19ec82 commit bf3d6e9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/notation/plugin/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func Cmd() *cobra.Command {
command.AddCommand(
pluginListCommand(),
pluginInstallCommand(nil),
pluginUninstallCommand(nil),
)

return command
Expand Down
4 changes: 2 additions & 2 deletions cmd/notation/plugin/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func pluginInstallCommand(opts *pluginInstallOpts) *cobra.Command {
Long: `Install a Notation plugin
Example - Install plugin from file system:
notation plugin install --file myPlugin.tar.gz --checksum 123abcd
notation plugin install --file myPlugin.tar.gz --checksum abcdef
`,
RunE: func(cmd *cobra.Command, args []string) error {
return installPlugin(cmd, opts)
Expand Down Expand Up @@ -139,7 +139,7 @@ func installPluginFromZip(ctx context.Context, zipPath string, force bool) error
defer archive.Close()
for _, f := range archive.File {
fmode := f.Mode()
// only consider regular executable files in the zip
// only consider regular executable files
if fmode.IsRegular() && osutil.IsOwnerExecutalbeFile(fmode) {
fileInArchive, err := f.Open()
if err != nil {
Expand Down
72 changes: 72 additions & 0 deletions cmd/notation/plugin/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package plugin

import (
"errors"
"fmt"
"os"

"github.com/notaryproject/notation-go/dir"
"github.com/notaryproject/notation/cmd/notation/internal/cmdutil"
"github.com/spf13/cobra"
)

type pluginUninstallOpts struct {
pluginName string
confirmed bool
}

func pluginUninstallCommand(opts *pluginUninstallOpts) *cobra.Command {
if opts == nil {
opts = &pluginUninstallOpts{}
}
command := &cobra.Command{
Use: "uninstall [flags] <plugin_name>",
Short: "Uninstall plugin",
Long: `Uninstall a Notation plugin
Example - Uninstall plugin:
notation plugin uninstall my-plugin
`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("plugin name is required")
}
opts.pluginName = args[0]
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return unInstallPlugin(cmd, opts)
},
}

command.Flags().BoolVarP(&opts.confirmed, "yes", "y", false, "do not prompt for confirmation")
return command
}

func unInstallPlugin(command *cobra.Command, opts *pluginUninstallOpts) error {
pluginName := opts.pluginName
existed, err := checkPluginExistence(command.Context(), pluginName)
if err != nil {
return fmt.Errorf("failed to check plugin existence, %w", err)
}
if !existed {
return fmt.Errorf("plugin %s does not exist", pluginName)
}
pluginPath, err := dir.PluginFS().SysPath(pluginName)
if err != nil {
return err
}
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 err
}
if !confirmed {
return nil
}
err = os.RemoveAll(pluginPath)
if err == nil {
fmt.Printf("Successfully uninstalled plugin %s\n", pluginName)
}
return err
}

0 comments on commit bf3d6e9

Please sign in to comment.