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: sort all sdks #285

Merged
merged 8 commits into from
May 28, 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@


coverage.out
**/available.cache
5 changes: 4 additions & 1 deletion cmd/commands/current.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ func currentCmd(ctx *cli.Context) error {
if err != nil {
return err
}
for name, s := range allSdk {

for _, s := range allSdk {
name := s.Plugin.SdkName
current := s.Current()
if current == "" {
pterm.Printf("%s -> N/A \n", name)
} else {
pterm.Printf("%s -> %s\n", name, pterm.LightGreen("v"+string(current)))
}
}

return nil
}
source, err := manager.LookupSdk(sdkName)
Expand Down
4 changes: 3 additions & 1 deletion cmd/commands/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ func listCmd(ctx *cli.Context) error {
return fmt.Errorf("you don't have any sdk installed yet")
}
tree := pterm.LeveledList{}
for name, s := range allSdk {

for _, s := range allSdk {
name := s.Plugin.SdkName
tree = append(tree, pterm.LeveledListItem{Level: 0, Text: name})
for _, version := range s.List() {
tree = append(tree, pterm.LeveledListItem{Level: 1, Text: "v" + string(version)})
Expand Down
9 changes: 5 additions & 4 deletions cmd/commands/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ func updateCmd(ctx *cli.Context) error {
index int
total = len(sdks)
)
for sdk := range sdks {
for _, s := range sdks {
sdkName := s.Plugin.SdkName
index++
pterm.Printf("[%s/%d]: Updating %s plugin...\n", pterm.Green(index), total, pterm.Green(sdk))
if err = manager.Update(sdk); err != nil {
pterm.Println(fmt.Sprintf("Update plugin(%s) failed, %s", sdk, err.Error()))
pterm.Printf("[%s/%d]: Updating %s plugin...\n", pterm.Green(index), total, pterm.Green(sdkName))
if err = manager.Update(sdkName); err != nil {
pterm.Println(fmt.Sprintf("Update plugin(%s) failed, %s", sdkName, err.Error()))
}
}
} else {
Expand Down
14 changes: 10 additions & 4 deletions internal/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/url"
"os"
"path/filepath"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -148,12 +149,12 @@ func (m *Manager) LookupSdkWithInstall(name string) (*Sdk, error) {
}
}

func (m *Manager) LoadAllSdk() (map[string]*Sdk, error) {
func (m *Manager) LoadAllSdk() ([]*Sdk, error) {
dir, err := os.ReadDir(m.PathMeta.PluginPath)
if err != nil {
return nil, fmt.Errorf("load sdks error: %w", err)
}
sdkMap := make(map[string]*Sdk)
sdkSlice := make([]*Sdk, 0)
for _, d := range dir {
sdkName := d.Name()
path := filepath.Join(m.PathMeta.PluginPath, sdkName)
Expand All @@ -176,10 +177,15 @@ func (m *Manager) LoadAllSdk() (map[string]*Sdk, error) {
continue
}
sdk, _ := NewSdk(m, path)
sdkMap[strings.ToLower(sdkName)] = sdk
sdkSlice = append(sdkSlice, sdk)

m.openSdks[strings.ToLower(sdkName)] = sdk
}
return sdkMap, nil

sort.Slice(sdkSlice, func(i, j int) bool {
return sdkSlice[j].Plugin.SdkName > sdkSlice[i].Plugin.SdkName
})
return sdkSlice, nil
}

func (m *Manager) Close() {
Expand Down
22 changes: 22 additions & 0 deletions internal/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,26 @@ func testHookFunc(t *testing.T, factory func() (*Manager, *LuaPlugin, error)) {
t.Fatal(err)
}
})

t.Run("LoadAllSdk", func(t *testing.T) {
Chance-fyi marked this conversation as resolved.
Show resolved Hide resolved
s, _, err := factory()
if err != nil {
t.Fatal(err)
}

firstSdkName := ""
for i := 0; i < 10; i++ {
sdks, err := s.LoadAllSdk()
if err != nil {
t.Fatal(err)
}
if len(sdks) != 0 && firstSdkName == "" {
firstSdkName = sdks[0].Plugin.SdkName
} else if len(sdks) != 0 {
if sdks[0].Plugin.SdkName != firstSdkName {
t.Errorf("expected sdk sort %v", sdks)
}
}
}
})
}