From ef083d59920e5c9142ad131b0f16f975331bac5f Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Tue, 21 May 2024 18:09:35 +0800 Subject: [PATCH 1/7] feat: sort all sdks --- cmd/commands/current.go | 6 ++++-- cmd/commands/list.go | 6 +++--- cmd/commands/update.go | 12 ++++++------ internal/manager.go | 6 ++++-- internal/sdk.go | 22 ++++++++++++++++++++++ 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/cmd/commands/current.go b/cmd/commands/current.go index 71f773fe..39f1661c 100644 --- a/cmd/commands/current.go +++ b/cmd/commands/current.go @@ -42,14 +42,16 @@ func currentCmd(ctx *cli.Context) error { if err != nil { return err } - for name, s := range allSdk { + + allSdk.ForEachBySort(func(name string, s *internal.Sdk) { 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) diff --git a/cmd/commands/list.go b/cmd/commands/list.go index 7c61ed28..89fda42e 100644 --- a/cmd/commands/list.go +++ b/cmd/commands/list.go @@ -42,16 +42,16 @@ func listCmd(ctx *cli.Context) error { if err != nil { return err } - if len(allSdk) == 0 { + if len(allSdk.SdkMap) == 0 { return fmt.Errorf("you don't have any sdk installed yet") } tree := pterm.LeveledList{} - for name, s := range allSdk { + allSdk.ForEachBySort(func(name string, s *internal.Sdk) { 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)}) } - } + }) // Generate tree from LeveledList. root := putils.TreeFromLeveledList(tree) root.Text = "All installed sdk versions" diff --git a/cmd/commands/update.go b/cmd/commands/update.go index 6072c9db..34d1696d 100644 --- a/cmd/commands/update.go +++ b/cmd/commands/update.go @@ -47,15 +47,15 @@ func updateCmd(ctx *cli.Context) error { if sdks, err := manager.LoadAllSdk(); err == nil { var ( index int - total = len(sdks) + total = len(sdks.SdkMap) ) - for sdk := range sdks { + sdks.ForEachBySort(func(sdkName string, s *internal.Sdk) { 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 { return cli.Exit(err.Error(), 1) } diff --git a/internal/manager.go b/internal/manager.go index 59ad9548..4bf9c85b 100644 --- a/internal/manager.go +++ b/internal/manager.go @@ -148,7 +148,7 @@ func (m *Manager) LookupSdkWithInstall(name string) (*Sdk, error) { } } -func (m *Manager) LoadAllSdk() (map[string]*Sdk, error) { +func (m *Manager) LoadAllSdk() (*AllSdk, error) { dir, err := os.ReadDir(m.PathMeta.PluginPath) if err != nil { return nil, fmt.Errorf("load sdks error: %w", err) @@ -179,7 +179,9 @@ func (m *Manager) LoadAllSdk() (map[string]*Sdk, error) { sdkMap[strings.ToLower(sdkName)] = sdk m.openSdks[strings.ToLower(sdkName)] = sdk } - return sdkMap, nil + return &AllSdk{ + SdkMap: sdkMap, + }, nil } func (m *Manager) Close() { diff --git a/internal/sdk.go b/internal/sdk.go index e5a06773..c7fe74fd 100644 --- a/internal/sdk.go +++ b/internal/sdk.go @@ -68,6 +68,28 @@ func (d *SdkEnvs) ToEnvs() *env.Envs { return envs } +type AllSdk struct { + SdkMap map[string]*Sdk +} + +func (b *AllSdk) ForEachBySort(apply func(name string, sdk *Sdk)) { + + // Sort the keys + keys := make([]string, 0, len(b.SdkMap)) + for key := range b.SdkMap { + keys = append(keys, key) + } + + sort.Strings(keys) + + for _, key := range keys { + sdk := b.SdkMap[key] + + // Call the function + apply(key, sdk) + } +} + type Sdk struct { sdkManager *Manager Plugin *LuaPlugin From 1e612ac8ee6c0e9d1084cf436b21971510485583 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Wed, 22 May 2024 22:03:32 +0800 Subject: [PATCH 2/7] feat: sort all sdks --- cmd/commands/current.go | 5 +++-- cmd/commands/list.go | 8 +++++--- cmd/commands/update.go | 7 ++++--- internal/manager.go | 19 +++++++++++-------- internal/plugin_test.go | 22 ++++++++++++++++++++++ internal/sdk.go | 26 +++----------------------- 6 files changed, 48 insertions(+), 39 deletions(-) diff --git a/cmd/commands/current.go b/cmd/commands/current.go index 39f1661c..b1b19886 100644 --- a/cmd/commands/current.go +++ b/cmd/commands/current.go @@ -43,14 +43,15 @@ func currentCmd(ctx *cli.Context) error { return err } - allSdk.ForEachBySort(func(name string, s *internal.Sdk) { + for _, s := range allSdk { + name := s.Name 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 } diff --git a/cmd/commands/list.go b/cmd/commands/list.go index 89fda42e..9ee11cdc 100644 --- a/cmd/commands/list.go +++ b/cmd/commands/list.go @@ -42,16 +42,18 @@ func listCmd(ctx *cli.Context) error { if err != nil { return err } - if len(allSdk.SdkMap) == 0 { + if len(allSdk) == 0 { return fmt.Errorf("you don't have any sdk installed yet") } tree := pterm.LeveledList{} - allSdk.ForEachBySort(func(name string, s *internal.Sdk) { + + for _, s := range allSdk { + name := s.Name 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)}) } - }) + } // Generate tree from LeveledList. root := putils.TreeFromLeveledList(tree) root.Text = "All installed sdk versions" diff --git a/cmd/commands/update.go b/cmd/commands/update.go index 34d1696d..3d05ac4d 100644 --- a/cmd/commands/update.go +++ b/cmd/commands/update.go @@ -47,15 +47,16 @@ func updateCmd(ctx *cli.Context) error { if sdks, err := manager.LoadAllSdk(); err == nil { var ( index int - total = len(sdks.SdkMap) + total = len(sdks) ) - sdks.ForEachBySort(func(sdkName string, s *internal.Sdk) { + for _, s := range sdks { + sdkName := s.Name index++ 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 { return cli.Exit(err.Error(), 1) } diff --git a/internal/manager.go b/internal/manager.go index 4bf9c85b..14b55f7d 100644 --- a/internal/manager.go +++ b/internal/manager.go @@ -26,6 +26,7 @@ import ( "net/url" "os" "path/filepath" + "sort" "strconv" "strings" @@ -109,7 +110,7 @@ func (m *Manager) LookupSdk(name string) (*Sdk, error) { return nil, fmt.Errorf("failed to migrate an old plug-in: %w", err) } } - sdk, err := NewSdk(m, pluginPath) + sdk, err := NewSdk(m, pluginPath, strings.ToLower(name)) if err != nil { return nil, err } @@ -148,12 +149,12 @@ func (m *Manager) LookupSdkWithInstall(name string) (*Sdk, error) { } } -func (m *Manager) LoadAllSdk() (*AllSdk, 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) @@ -175,13 +176,15 @@ func (m *Manager) LoadAllSdk() (*AllSdk, error) { } else { continue } - sdk, _ := NewSdk(m, path) - sdkMap[strings.ToLower(sdkName)] = sdk + sdk, _ := NewSdk(m, path, strings.ToLower(sdkName)) + sdkSlice = append(sdkSlice, sdk) + sort.Slice(sdkSlice, func(i, j int) bool { + return sdkSlice[j].Name > sdkSlice[i].Name + }) m.openSdks[strings.ToLower(sdkName)] = sdk } - return &AllSdk{ - SdkMap: sdkMap, - }, nil + + return sdkSlice, nil } func (m *Manager) Close() { diff --git a/internal/plugin_test.go b/internal/plugin_test.go index 2e8294e1..783ea3a0 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].Name + } else if len(sdks) != 0 { + if sdks[0].Name != firstSdkName { + t.Errorf("expected sdk sort %v", sdks) + } + } + } + }) } diff --git a/internal/sdk.go b/internal/sdk.go index c7fe74fd..6a31ba84 100644 --- a/internal/sdk.go +++ b/internal/sdk.go @@ -68,33 +68,12 @@ func (d *SdkEnvs) ToEnvs() *env.Envs { return envs } -type AllSdk struct { - SdkMap map[string]*Sdk -} - -func (b *AllSdk) ForEachBySort(apply func(name string, sdk *Sdk)) { - - // Sort the keys - keys := make([]string, 0, len(b.SdkMap)) - for key := range b.SdkMap { - keys = append(keys, key) - } - - sort.Strings(keys) - - for _, key := range keys { - sdk := b.SdkMap[key] - - // Call the function - apply(key, sdk) - } -} - type Sdk struct { sdkManager *Manager Plugin *LuaPlugin // current sdk install path InstallPath string + Name string } func (b *Sdk) Install(version Version) error { @@ -776,12 +755,13 @@ func (b *Sdk) ClearCurrentEnv() error { } // NewSdk creates a new SDK instance. -func NewSdk(manager *Manager, pluginPath string) (*Sdk, error) { +func NewSdk(manager *Manager, pluginPath string, name string) (*Sdk, error) { luaPlugin, err := NewLuaPlugin(pluginPath, manager) if err != nil { return nil, fmt.Errorf("failed to create lua plugin: %w", err) } return &Sdk{ + Name: name, sdkManager: manager, InstallPath: filepath.Join(manager.PathMeta.SdkCachePath, strings.ToLower(luaPlugin.SdkName)), Plugin: luaPlugin, From 0098ab918a9f645b56d38992ddb9e7b3241c57d3 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Wed, 22 May 2024 22:07:46 +0800 Subject: [PATCH 3/7] feat: modify gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index c9e8a97c..53bc389b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ coverage.out +**/available.cache From fff65eb1ef25bc72b31deeeb4038fda9d8dd9b21 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Thu, 23 May 2024 08:49:16 +0800 Subject: [PATCH 4/7] feat: sort all sdks --- internal/manager.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/internal/manager.go b/internal/manager.go index 14b55f7d..022f302c 100644 --- a/internal/manager.go +++ b/internal/manager.go @@ -178,12 +178,12 @@ func (m *Manager) LoadAllSdk() ([]*Sdk, error) { } sdk, _ := NewSdk(m, path, strings.ToLower(sdkName)) sdkSlice = append(sdkSlice, sdk) - sort.Slice(sdkSlice, func(i, j int) bool { - return sdkSlice[j].Name > sdkSlice[i].Name - }) m.openSdks[strings.ToLower(sdkName)] = sdk } - + // sort by name + sort.Slice(sdkSlice, func(i, j int) bool { + return sdkSlice[j].Name > sdkSlice[i].Name + }) return sdkSlice, nil } From 510d815f5a674693d7d43f05f9329e6549acc7ce Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Sun, 26 May 2024 11:40:20 +0800 Subject: [PATCH 5/7] feat: modify gitignore --- internal/manager.go | 6 +++--- internal/plugin_test.go | 4 ++-- internal/sdk.go | 4 +--- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/internal/manager.go b/internal/manager.go index 14b55f7d..88c99029 100644 --- a/internal/manager.go +++ b/internal/manager.go @@ -110,7 +110,7 @@ func (m *Manager) LookupSdk(name string) (*Sdk, error) { return nil, fmt.Errorf("failed to migrate an old plug-in: %w", err) } } - sdk, err := NewSdk(m, pluginPath, strings.ToLower(name)) + sdk, err := NewSdk(m, pluginPath) if err != nil { return nil, err } @@ -176,10 +176,10 @@ func (m *Manager) LoadAllSdk() ([]*Sdk, error) { } else { continue } - sdk, _ := NewSdk(m, path, strings.ToLower(sdkName)) + sdk, _ := NewSdk(m, path) sdkSlice = append(sdkSlice, sdk) sort.Slice(sdkSlice, func(i, j int) bool { - return sdkSlice[j].Name > sdkSlice[i].Name + return sdkSlice[j].Plugin.SdkName > sdkSlice[i].Plugin.SdkName }) m.openSdks[strings.ToLower(sdkName)] = sdk } diff --git a/internal/plugin_test.go b/internal/plugin_test.go index 783ea3a0..32c98ad2 100644 --- a/internal/plugin_test.go +++ b/internal/plugin_test.go @@ -386,9 +386,9 @@ func testHookFunc(t *testing.T, factory func() (*Manager, *LuaPlugin, error)) { t.Fatal(err) } if len(sdks) != 0 && firstSdkName == "" { - firstSdkName = sdks[0].Name + firstSdkName = sdks[0].Plugin.SdkName } else if len(sdks) != 0 { - if sdks[0].Name != firstSdkName { + if sdks[0].Plugin.SdkName != firstSdkName { t.Errorf("expected sdk sort %v", sdks) } } diff --git a/internal/sdk.go b/internal/sdk.go index 6a31ba84..e5a06773 100644 --- a/internal/sdk.go +++ b/internal/sdk.go @@ -73,7 +73,6 @@ type Sdk struct { Plugin *LuaPlugin // current sdk install path InstallPath string - Name string } func (b *Sdk) Install(version Version) error { @@ -755,13 +754,12 @@ func (b *Sdk) ClearCurrentEnv() error { } // NewSdk creates a new SDK instance. -func NewSdk(manager *Manager, pluginPath string, name string) (*Sdk, error) { +func NewSdk(manager *Manager, pluginPath string) (*Sdk, error) { luaPlugin, err := NewLuaPlugin(pluginPath, manager) if err != nil { return nil, fmt.Errorf("failed to create lua plugin: %w", err) } return &Sdk{ - Name: name, sdkManager: manager, InstallPath: filepath.Join(manager.PathMeta.SdkCachePath, strings.ToLower(luaPlugin.SdkName)), Plugin: luaPlugin, From 6e2f35c2c978e1c44a7db6520eff16bd17f941b0 Mon Sep 17 00:00:00 2001 From: gaibu <1016771049@qq.com> Date: Sun, 26 May 2024 11:48:32 +0800 Subject: [PATCH 6/7] feat: sort all sdks --- internal/manager.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/manager.go b/internal/manager.go index 88c99029..2e7f2e2d 100644 --- a/internal/manager.go +++ b/internal/manager.go @@ -178,12 +178,13 @@ func (m *Manager) LoadAllSdk() ([]*Sdk, error) { } sdk, _ := NewSdk(m, path) sdkSlice = append(sdkSlice, sdk) - sort.Slice(sdkSlice, func(i, j int) bool { - return sdkSlice[j].Plugin.SdkName > sdkSlice[i].Plugin.SdkName - }) + m.openSdks[strings.ToLower(sdkName)] = sdk } + sort.Slice(sdkSlice, func(i, j int) bool { + return sdkSlice[j].Plugin.SdkName > sdkSlice[i].Plugin.SdkName + }) return sdkSlice, nil } From 17755f219ddcad3e343a4abc68256022af056b86 Mon Sep 17 00:00:00 2001 From: Chance Date: Mon, 27 May 2024 09:07:06 +0800 Subject: [PATCH 7/7] fix --- cmd/commands/current.go | 2 +- cmd/commands/list.go | 2 +- cmd/commands/update.go | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/cmd/commands/current.go b/cmd/commands/current.go index b1b19886..285dea73 100644 --- a/cmd/commands/current.go +++ b/cmd/commands/current.go @@ -44,7 +44,7 @@ func currentCmd(ctx *cli.Context) error { } for _, s := range allSdk { - name := s.Name + name := s.Plugin.SdkName current := s.Current() if current == "" { pterm.Printf("%s -> N/A \n", name) diff --git a/cmd/commands/list.go b/cmd/commands/list.go index 9ee11cdc..72a54f8e 100644 --- a/cmd/commands/list.go +++ b/cmd/commands/list.go @@ -48,7 +48,7 @@ func listCmd(ctx *cli.Context) error { tree := pterm.LeveledList{} for _, s := range allSdk { - name := s.Name + 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 3d05ac4d..1bb46327 100644 --- a/cmd/commands/update.go +++ b/cmd/commands/update.go @@ -50,7 +50,7 @@ func updateCmd(ctx *cli.Context) error { total = len(sdks) ) for _, s := range sdks { - sdkName := s.Name + sdkName := s.Plugin.SdkName index++ pterm.Printf("[%s/%d]: Updating %s plugin...\n", pterm.Green(index), total, pterm.Green(sdkName)) if err = manager.Update(sdkName); err != nil {