diff --git a/.gitignore b/.gitignore index c9e8a97c..53bc389b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ coverage.out +**/available.cache diff --git a/cmd/commands/current.go b/cmd/commands/current.go index 71f773fe..285dea73 100644 --- a/cmd/commands/current.go +++ b/cmd/commands/current.go @@ -42,7 +42,9 @@ 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) @@ -50,6 +52,7 @@ func currentCmd(ctx *cli.Context) error { pterm.Printf("%s -> %s\n", name, pterm.LightGreen("v"+string(current))) } } + return nil } source, err := manager.LookupSdk(sdkName) diff --git a/cmd/commands/list.go b/cmd/commands/list.go index 7c61ed28..72a54f8e 100644 --- a/cmd/commands/list.go +++ b/cmd/commands/list.go @@ -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)}) diff --git a/cmd/commands/update.go b/cmd/commands/update.go index 6072c9db..1bb46327 100644 --- a/cmd/commands/update.go +++ b/cmd/commands/update.go @@ -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 { diff --git a/internal/manager.go b/internal/manager.go index 59ad9548..2e7f2e2d 100644 --- a/internal/manager.go +++ b/internal/manager.go @@ -26,6 +26,7 @@ import ( "net/url" "os" "path/filepath" + "sort" "strconv" "strings" @@ -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) @@ -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() { diff --git a/internal/plugin_test.go b/internal/plugin_test.go index 2e8294e1..32c98ad2 100644 --- a/internal/plugin_test.go +++ b/internal/plugin_test.go @@ -372,4 +372,26 @@ func testHookFunc(t *testing.T, factory func() (*Manager, *LuaPlugin, error)) { t.Fatal(err) } }) + + t.Run("LoadAllSdk", func(t *testing.T) { + 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) + } + } + } + }) }