Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add uninstall to CLIManager #363

Merged
merged 6 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading