Skip to content

Commit

Permalink
bugfix for uninstall
Browse files Browse the repository at this point in the history
  • Loading branch information
moqsien committed Jul 3, 2024
1 parent 9f97629 commit e9a84fc
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
11 changes: 8 additions & 3 deletions internal/download/sdk_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,18 @@ type SDKVersion []Item

type VersionList map[string]SDKVersion

func GetVersionFilePath(sdkName string) (fPath string) {
versionFileCacheDir := filepath.Join(cnf.GetCacheDir(), sdkName)
os.MkdirAll(versionFileCacheDir, os.ModePerm)
fPath = filepath.Join(versionFileCacheDir, strings.Trim(fmt.Sprintf(cnf.VersionFileUrlPattern, sdkName), "/"))
return
}

/*
Download version list file.
*/
func CheckSumForVersionFile(sdkName, newSha256 string) (ok bool, fPath string) {
versionFileCacheDir := filepath.Join(cnf.GetCacheDir(), sdkName)
os.MkdirAll(versionFileCacheDir, os.ModePerm)
fPath = filepath.Join(versionFileCacheDir, strings.Trim(fmt.Sprintf(cnf.VersionFileUrlPattern, sdkName), "/"))
fPath = GetVersionFilePath(sdkName)
content, _ := os.ReadFile(fPath)

h := sha256.New()
Expand Down
22 changes: 17 additions & 5 deletions internal/installer/installed.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package installer

import (
"encoding/json"
"fmt"
"os"
"path/filepath"
Expand All @@ -16,16 +17,27 @@ import (
*/

type InstalledVersionFinder struct {
SDKName string
OriginSDKName string
InstalledVersions []string
CurrentVersion string
Installer *Installer
}

func NewIVFinder(sdkName string) (i *InstalledVersionFinder) {
versionFilePath := download.GetVersionFilePath(sdkName)
content, _ := os.ReadFile(versionFilePath)
rawVersionList := make(download.VersionList)
json.Unmarshal(content, &rawVersionList)
installerType := "unarchiver"
for _, vl := range rawVersionList {
if len(vl) > 0 {
installerType = vl[0].Installer
break
}
}
i = &InstalledVersionFinder{
SDKName: sdkName,
Installer: NewInstaller(sdkName, "", "", download.Item{}),
OriginSDKName: sdkName,
Installer: NewInstaller(sdkName, "", "", download.Item{Installer: installerType}),
}
return
}
Expand All @@ -37,7 +49,7 @@ func (i *InstalledVersionFinder) findCurrentVersion(symbolPath string) {

slink, _ := os.Readlink(symbolPath)
fName := filepath.Base(slink)
namePrefix := fmt.Sprintf("%s-", i.SDKName)
namePrefix := fmt.Sprintf("%s-", i.OriginSDKName)
if strings.HasPrefix(fName, namePrefix) {
i.CurrentVersion = strings.TrimPrefix(fName, namePrefix)
}
Expand All @@ -56,7 +68,7 @@ func (i *InstalledVersionFinder) FindAll() (r []string, current string) {
}
i.findCurrentVersion(symbolPath)

namePrefix := fmt.Sprintf("%s-", i.SDKName)
namePrefix := fmt.Sprintf("%s-", i.OriginSDKName)
dList, _ := os.ReadDir(versionDir)
for _, d := range dList {
if d.IsDir() && strings.HasPrefix(d.Name(), namePrefix) {
Expand Down
16 changes: 15 additions & 1 deletion internal/tui/cmds/cmd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package cmds

import (
"encoding/json"
"os"

"github.com/gvcgo/version-manager/internal/download"
"github.com/gvcgo/version-manager/internal/installer"
)
Expand Down Expand Up @@ -116,6 +119,17 @@ func (v *VmrTUI) RemoveInstalledVersions(sdkName string) {
}

func (v *VmrTUI) RemoveSelectedVersion(sdkName, versionName string) {
ins := installer.NewInstaller(sdkName, versionName, "", download.Item{})
versionFilePath := download.GetVersionFilePath(sdkName)
content, _ := os.ReadFile(versionFilePath)
rawVersionList := make(download.VersionList)
json.Unmarshal(content, &rawVersionList)
installerType := "unarchiver"
for _, vl := range rawVersionList {
if len(vl) > 0 {
installerType = vl[0].Installer
break
}
}
ins := installer.NewInstaller(sdkName, versionName, "", download.Item{Installer: installerType})
ins.Uninstall()
}

0 comments on commit e9a84fc

Please sign in to comment.