Skip to content

Commit

Permalink
resolved conflicts
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts committed Dec 6, 2023
2 parents 6d08fa1 + 49d48bc commit 4888c32
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 4 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(
listCommand(),
installCommand(nil),
uninstallCommand(nil),
)

return command
Expand Down
2 changes: 1 addition & 1 deletion cmd/notation/plugin/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ func installPluginExecutable(ctx context.Context, fileName string, fileReader io
mgr := plugin.NewCLIManager(dir.PluginFS())
existingPluginMetadata, newPluginMetadata, err := mgr.Install(ctx, tmpFilePath, force)
if err != nil {
if errors.Is(err, &plugin.ErrInstallLowerVersion{}) {
if errors.Is(err, &plugin.ErrPluginDowngrade{}) {
return fmt.Errorf("%s. %w.\nIt is not recommended to install an older version. To force the installation, use the \"--force\" option", newPluginMetadata.Name, err)
}
return err
Expand Down
107 changes: 107 additions & 0 deletions cmd/notation/plugin/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package plugin

import (
"context"
"errors"
"fmt"
"os"

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

type pluginUninstallOpts struct {
cmd.LoggingFlagOpts
pluginName string
confirmed bool
}

func uninstallCommand(opts *pluginUninstallOpts) *cobra.Command {
if opts == nil {
opts = &pluginUninstallOpts{}
}
command := &cobra.Command{
Use: "uninstall [flags] <plugin_name>",
Aliases: []string{"remove", "rm"},
Short: "Uninstall a plugin",
Long: `Uninstall a plugin
Example - Uninstall plugin:
notation plugin uninstall wabbit-plugin
`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("plugin name is required")
}
if len(args) > 1 {
return errors.New("only one plugin can be removed at a time")
}
opts.pluginName = args[0]
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
return uninstallPlugin(cmd, opts)
},
}

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

func uninstallPlugin(command *cobra.Command, opts *pluginUninstallOpts) error {
// set logger
ctx := opts.LoggingFlagOpts.InitializeLogger(command.Context())
pluginName := opts.pluginName
exist, err := checkPluginExistence(ctx, pluginName)
if err != nil {
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: %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: %w", pluginName, err)
}
fmt.Printf("Successfully uninstalled plugin %s\n", pluginName)
return nil
}

// checkPluginExistence returns true if plugin exists in the system
func checkPluginExistence(ctx context.Context, pluginName string) (bool, error) {
mgr := plugin.NewCLIManager(dir.PluginFS())
_, err := mgr.Get(ctx, pluginName)
if err != nil {
if errors.Is(err, os.ErrNotExist) { // plugin does not exist
return false, nil
}
return false, err
}
return true, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ require (
golang.org/x/sys v0.14.0 // indirect
)

replace github.com/notaryproject/notation-go => github.com/Two-Hearts/notation-go v0.0.0-20231128065613-75a090e11a34
replace github.com/notaryproject/notation-go => github.com/Two-Hearts/notation-go v0.0.0-20231206052238-c2f7ef881b94
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 h1:mFRzDkZVAjdal+s7s0MwaRv9igoPqLRdzOLzw/8Xvq8=
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358/go.mod h1:chxPXzSsl7ZWRAuOIE23GDNzjWuZquvFlgA8xmpunjU=
github.com/Two-Hearts/notation-go v0.0.0-20231128065613-75a090e11a34 h1:iiJ2n15JI32RnMpcMXINz4QlAtamstCfK8rXQKffWfM=
github.com/Two-Hearts/notation-go v0.0.0-20231128065613-75a090e11a34/go.mod h1:tSCFsAdKAtB7AfKS/BaUf8AXzASA+9TEokMDEDutqPM=
github.com/Two-Hearts/notation-go v0.0.0-20231206052238-c2f7ef881b94 h1:EBHFgDC9t31ODro+E835E5Z1Mz0fbr16udOLUFomGg0=
github.com/Two-Hearts/notation-go v0.0.0-20231206052238-c2f7ef881b94/go.mod h1:tSCFsAdKAtB7AfKS/BaUf8AXzASA+9TEokMDEDutqPM=
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74 h1:Kk6a4nehpJ3UuJRqlA3JxYxBZEqCeOmATOvrbT4p9RA=
github.com/alexbrainman/sspi v0.0.0-20210105120005-909beea2cc74/go.mod h1:cEWa1LVoE5KvSD9ONXsZrj0z6KqySlCCNKHlLzbqAt4=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
Expand Down
38 changes: 38 additions & 0 deletions test/e2e/suite/plugin/uninstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Copyright The Notary Project Authors.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package plugin

import (
. "github.com/notaryproject/notation/test/e2e/internal/notation"
"github.com/notaryproject/notation/test/e2e/internal/utils"
. "github.com/onsi/ginkgo/v2"
)

var _ = Describe("notation plugin uninstall", func() {
It("with valid plugin name", func() {
Host(nil, func(notation *utils.ExecOpts, _ *Artifact, vhost *utils.VirtualHost) {
vhost.SetOption(AddPlugin(NotationE2EPluginPath))
notation.Exec("plugin", "uninstall", "--yes", "e2e-plugin").
MatchContent("Successfully uninstalled plugin e2e-plugin\n")
})
})

It("with plugin does not exist", func() {
Host(nil, func(notation *utils.ExecOpts, _ *Artifact, vhost *utils.VirtualHost) {
notation.ExpectFailure().Exec("plugin", "uninstall", "--yes", "non-exist").
MatchErrContent("Error: unable to find plugin non-exist.\nTo view a list of installed plugins, use `notation plugin list`\n")
})
})

})

0 comments on commit 4888c32

Please sign in to comment.