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

Fix about adding plugin with alias #219

Merged
merged 3 commits into from
Apr 21, 2024
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
52 changes: 23 additions & 29 deletions internal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
"github.com/version-fox/vfox/internal/config"
"github.com/version-fox/vfox/internal/env"
"github.com/version-fox/vfox/internal/logger"
"github.com/version-fox/vfox/internal/toolset"
"github.com/version-fox/vfox/internal/util"
"io"
"net"
"net/http"
Expand All @@ -35,6 +28,14 @@ import (
"path/filepath"
"strconv"
"strings"

"github.com/pterm/pterm"
"github.com/urfave/cli/v2"
"github.com/version-fox/vfox/internal/config"
"github.com/version-fox/vfox/internal/env"
"github.com/version-fox/vfox/internal/logger"
"github.com/version-fox/vfox/internal/toolset"
"github.com/version-fox/vfox/internal/util"
)

const (
Expand Down Expand Up @@ -384,22 +385,24 @@ func (m *Manager) downloadPlugin(downloadUrl string) (string, error) {
}

func (m *Manager) Add(pluginName, url, alias string) error {
// For compatibility with older versions of plugin names <category>/<plugin-name>
if strings.Contains(pluginName, "/") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is unlikely to be the case with custom plugins, it's harmless enough to keep.

pluginName = strings.Split(pluginName, "/")[1]
}
pluginPath := url
pname := pluginName
if len(alias) > 0 {
pname = alias
}
installPath := filepath.Join(m.PathMeta.PluginPath, pname)
if util.FileExists(installPath) {
return fmt.Errorf("plugin named %s already exists", pname)
}

// official plugin
if len(url) == 0 {
pname := pluginName
// For compatibility with older versions of plugin names <category>/<plugin-name>
if strings.Contains(pluginName, "/") {
pname = strings.Split(pluginName, "/")[1]
}

installPath := filepath.Join(m.PathMeta.PluginPath, pname)
if util.FileExists(installPath) {
return fmt.Errorf("plugin %s already exists", pname)
}

fmt.Printf("Fetching %s manifest... \n", pterm.Green(pname))
pluginManifest, err := m.fetchPluginManifest(m.GetRegistryAddress(pname + ".json"))
fmt.Printf("Fetching %s manifest... \n", pterm.Green(pluginName))
pluginManifest, err := m.fetchPluginManifest(m.GetRegistryAddress(pluginName + ".json"))
if err != nil {
return err
}
Expand All @@ -413,15 +416,6 @@ func (m *Manager) Add(pluginName, url, alias string) error {
_ = os.RemoveAll(tempPlugin.Path)
tempPlugin.Close()
}()
// set alias name
pname := tempPlugin.Name
if len(alias) > 0 {
pname = alias
}
installPath := filepath.Join(m.PathMeta.PluginPath, pname)
if util.FileExists(installPath) {
return fmt.Errorf("plugin %s already exists", pname)
}
if err = os.Rename(tempPlugin.Path, installPath); err != nil {
return fmt.Errorf("install plugin error: %w", err)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,7 @@ func (b *Sdk) preInstallSdk(info *Info, sdkDestPath string) (string, error) {
func (b *Sdk) Uninstall(version Version) error {
label := b.label(version)
if !b.checkExists(version) {
pterm.Printf("%s is not installed...\n", pterm.Red(label))
return fmt.Errorf("%s is not installed", label)
return fmt.Errorf("%s is not installed", pterm.Red(label))
}
if b.Current() == version {
b.clearEnvConfig(version)
Expand Down Expand Up @@ -597,7 +596,7 @@ func (b *Sdk) Download(u *url.URL) (string, error) {
}

func (b *Sdk) label(version Version) string {
return fmt.Sprintf("%s@%s", strings.ToLower(b.Plugin.Name), version)
return fmt.Sprintf("%s@%s", strings.ToLower(b.Plugin.SdkName), version)
}

// NewSdk creates a new SDK instance.
Expand Down