Skip to content

Commit

Permalink
feat: add uninstall to CLIManager (#363)
Browse files Browse the repository at this point in the history
Signed-off-by: Patrick Zheng <[email protected]>
  • Loading branch information
Two-Hearts authored Nov 23, 2023
1 parent ce0c457 commit 5de0d58
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
14 changes: 14 additions & 0 deletions plugin/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"errors"
"io/fs"
"os"
"path"

"github.com/notaryproject/notation-go/dir"
Expand Down Expand Up @@ -82,3 +83,16 @@ func (m *CLIManager) List(ctx context.Context) ([]string, error) {
})
return plugins, nil
}

// Uninstall uninstalls a plugin on the system by its name
// If the plugin dir does not exist, os.ErrNotExist is returned.
func (m *CLIManager) Uninstall(ctx context.Context, name string) error {
pluginDirPath, err := m.pluginFS.SysPath(name)
if err != nil {
return err
}
if _, err := os.Stat(pluginDirPath); err != nil {
return err
}
return os.RemoveAll(pluginDirPath)
}
26 changes: 26 additions & 0 deletions plugin/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"encoding/json"
"io/fs"
"os"
"reflect"
"testing"
"testing/fstest"
Expand Down Expand Up @@ -87,6 +88,31 @@ func TestManager_List(t *testing.T) {
})
}

func TestManager_Uninstall(t *testing.T) {
executor = testCommander{stdout: metadataJSON(validMetadata)}
mgr := NewCLIManager(mockfs.NewSysFSWithRootMock(fstest.MapFS{}, "./testdata/plugins"))
if err := os.MkdirAll("./testdata/plugins/toUninstall", 0777); err != nil {
t.Fatalf("failed to create toUninstall dir: %v", err)
}
defer os.RemoveAll("./testdata/plugins/toUninstall")
pluginFile, err := os.Create("./testdata/plugins/toUninstall/toUninstall")
if err != nil {
t.Fatalf("failed to create toUninstall file: %v", err)
}
if err := pluginFile.Close(); err != nil {
t.Fatalf("failed to close toUninstall file: %v", err)
}
// test uninstall valid plugin
if err := mgr.Uninstall(context.Background(), "toUninstall"); err != nil {
t.Fatalf("Manager.Uninstall() err %v, want nil", err)
}
// test uninstall non-exist plugin
expectedErrorMsg := "stat testdata/plugins/non-exist: no such file or directory"
if err := mgr.Uninstall(context.Background(), "non-exist"); err == nil || err.Error() != expectedErrorMsg {
t.Fatalf("Manager.Uninstall() err %v, want %s", err, expectedErrorMsg)
}
}

func metadataJSON(m proto.GetMetadataResponse) []byte {
d, err := json.Marshal(m)
if err != nil {
Expand Down

0 comments on commit 5de0d58

Please sign in to comment.