Skip to content

Commit 3b2729a

Browse files
committed
refactor(base): move Version and related types to base package
This commit refactors the code by moving the `Version` type and related constants, interfaces, and functions from the `internal` package to the `internal/base` package. This change improves code organization and maintainability by centralizing version-related types and logic in a single location. The refactor also includes updating all references to the moved types across the codebase to use the new package path. The primary motivation for this change is to reduce code duplication and improve the clarity of the codebase by grouping related types together. This refactor does not introduce any functional changes but prepares the codebase for future enhancements and easier maintenance.
1 parent 7a4d6e7 commit 3b2729a

14 files changed

Lines changed: 119 additions & 115 deletions

File tree

cmd/commands/env.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/urfave/cli/v2"
2424
"github.com/version-fox/vfox/internal"
25+
"github.com/version-fox/vfox/internal/base"
2526
"github.com/version-fox/vfox/internal/env"
2627
"github.com/version-fox/vfox/internal/shell"
2728
"github.com/version-fox/vfox/internal/toolset"
@@ -89,7 +90,7 @@ func outputJSON() error {
8990
}
9091
tvs.FilterTools(func(name, version string) bool {
9192
if lookupSdk, err := manager.LookupSdk(name); err == nil {
92-
if keys, err := lookupSdk.EnvKeys(internal.Version(version), internal.OriginalLocation); err == nil {
93+
if keys, err := lookupSdk.EnvKeys(base.Version(version), base.OriginalLocation); err == nil {
9394
data.SDKs[lookupSdk.Plugin.Name] = keys.Variables
9495
data.Paths = append(data.Paths, keys.Paths.Slice()...)
9596
return true

cmd/commands/install.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/pterm/pterm"
2626
"github.com/urfave/cli/v2"
2727
"github.com/version-fox/vfox/internal"
28+
"github.com/version-fox/vfox/internal/base"
2829
"github.com/version-fox/vfox/internal/toolset"
2930
"github.com/version-fox/vfox/internal/util"
3031
)
@@ -68,10 +69,10 @@ func installCmd(ctx *cli.Context) error {
6869
errorStore.AddAndShow(sdkArg, fmt.Errorf("your input is invalid: %s", sdkArg))
6970
} else {
7071
var name string
71-
var version internal.Version
72+
var version base.Version
7273
if argsLen == 2 {
7374
name = strings.ToLower(argArr[0])
74-
version = internal.Version(argArr[1])
75+
version = base.Version(argArr[1])
7576
} else {
7677
name = strings.ToLower(argArr[0])
7778
version = ""
@@ -174,7 +175,7 @@ func installAll() error {
174175
errorStr = fmt.Sprintf("%s\n%s", errorStr, err)
175176
continue
176177
}
177-
err = lookupSdk.Install(internal.Version(version))
178+
err = lookupSdk.Install(base.Version(version))
178179
if err != nil {
179180
errorStr = fmt.Sprintf("%s\n%s", errorStr, err)
180181
continue
@@ -212,7 +213,7 @@ func notInstalled(manager *internal.Manager) (plugins []string, sdks map[string]
212213
plugins = append(plugins, name)
213214
return true
214215
}
215-
if !lookupSdk.CheckExists(internal.Version(version)) {
216+
if !lookupSdk.CheckExists(base.Version(version)) {
216217
return true
217218
}
218219
return false

cmd/commands/search.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/urfave/cli/v2"
2626
"github.com/version-fox/vfox/internal"
27+
"github.com/version-fox/vfox/internal/base"
2728
"github.com/version-fox/vfox/internal/printer"
2829
"github.com/version-fox/vfox/internal/util"
2930
"golang.org/x/crypto/ssh/terminal"
@@ -106,7 +107,7 @@ func RunSearch(sdkName string, availableArgs []string) error {
106107
if version == nil {
107108
return nil
108109
}
109-
return source.Install(internal.Version(version.Key))
110+
return source.Install(base.Version(version.Key))
110111
}
111112

112113
func searchCmd(ctx *cli.Context) error {

cmd/commands/uninstall.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/pterm/pterm"
2525
"github.com/urfave/cli/v2"
2626
"github.com/version-fox/vfox/internal"
27+
"github.com/version-fox/vfox/internal/base"
2728
)
2829

2930
var Uninstall = &cli.Command{
@@ -48,7 +49,7 @@ func uninstallCmd(ctx *cli.Context) error {
4849
}
4950

5051
name := strings.ToLower(argArr[0])
51-
version := internal.Version(argArr[1])
52+
version := base.Version(argArr[1])
5253

5354
source, err := manager.LookupSdk(name)
5455
if err != nil {
@@ -66,7 +67,7 @@ func uninstallCmd(ctx *cli.Context) error {
6667
if cv == version {
6768
pterm.Println("Auto switch to the other version.")
6869
firstVersion := remainVersion[0]
69-
return source.Use(firstVersion, internal.Global)
70+
return source.Use(firstVersion, base.Global)
7071
}
7172
return nil
7273
}

cmd/commands/use.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323

2424
"github.com/pterm/pterm"
2525
"github.com/version-fox/vfox/internal"
26+
"github.com/version-fox/vfox/internal/base"
2627

2728
"github.com/urfave/cli/v2"
2829
)
@@ -59,24 +60,24 @@ func useCmd(ctx *cli.Context) error {
5960
}
6061
var (
6162
name string
62-
version internal.Version
63+
version base.Version
6364
)
6465
argArr := strings.Split(sdkArg, "@")
6566
if len(argArr) <= 1 {
6667
name = argArr[0]
6768
version = ""
6869
} else {
6970
name = argArr[0]
70-
version = internal.Version(argArr[1])
71+
version = base.Version(argArr[1])
7172
}
7273

73-
scope := internal.Session
74+
scope := base.Session
7475
if ctx.IsSet("global") {
75-
scope = internal.Global
76+
scope = base.Global
7677
} else if ctx.IsSet("project") {
77-
scope = internal.Project
78+
scope = base.Project
7879
} else {
79-
scope = internal.Session
80+
scope = base.Session
8081
}
8182
manager := internal.NewSdkManager()
8283
defer manager.Close()
@@ -106,7 +107,7 @@ func useCmd(ctx *cli.Context) error {
106107
},
107108
}
108109
result, _ := selectPrinter.Show(fmt.Sprintf("Please select a version of %s", name))
109-
version = internal.Version(result)
110+
version = base.Version(result)
110111
}
111112

112113
return source.Use(version, scope)
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424
"github.com/version-fox/vfox/internal/util"
2525
)
2626

27+
type Version string
28+
2729
type CheckSum struct {
2830
Sha256 string `json:"sha256"`
2931
Sha512 string `json:"sha512"`
@@ -135,7 +137,7 @@ func CreatePackages(sdkName string, hookResult []*AvailableHookResultItem) []*Pa
135137
}
136138

137139
type PreInstallHookCtx struct {
138-
Version string `json:"version"`
140+
Version Version `json:"version"`
139141
}
140142

141143
type PreInstallHookResultAdditionItem struct {
@@ -212,7 +214,7 @@ type PreUseHookCtx struct {
212214
}
213215

214216
type PreUseHookResult struct {
215-
Version string `json:"version"`
217+
Version Version `json:"version"`
216218
}
217219

218220
type PostInstallHookCtx struct {
@@ -233,13 +235,13 @@ type EnvKeysHookResultItem struct {
233235
}
234236

235237
type ParseLegacyFileHookCtx struct {
236-
Filepath string `json:"filepath"`
237-
Filename string `json:"filename"`
238-
GetInstalledVersions func() []string `json:"getInstalledVersions"`
238+
Filepath string `json:"filepath"`
239+
Filename string `json:"filename"`
240+
GetInstalledVersions func() []Version `json:"getInstalledVersions"`
239241
}
240242

241243
type ParseLegacyFileResult struct {
242-
Version string `json:"version"`
244+
Version Version `json:"version"`
243245
}
244246

245247
type PreUninstallHookCtx struct {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package internal
17+
package base
1818

1919
type UseScope int
2020

internal/luai/plugin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import (
77
"path/filepath"
88
"strings"
99

10+
"github.com/version-fox/vfox/internal/base"
1011
"github.com/version-fox/vfox/internal/cache"
1112
"github.com/version-fox/vfox/internal/config"
1213
"github.com/version-fox/vfox/internal/logger"
13-
"github.com/version-fox/vfox/internal/plugin/base"
1414
"github.com/version-fox/vfox/internal/util"
1515
lua "github.com/yuin/gopher-lua"
1616
)

internal/manager.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ import (
3434
"github.com/pterm/pterm"
3535
"github.com/shirou/gopsutil/v4/process"
3636
"github.com/urfave/cli/v2"
37+
"github.com/version-fox/vfox/internal/base"
3738
"github.com/version-fox/vfox/internal/cache"
3839
"github.com/version-fox/vfox/internal/config"
3940
"github.com/version-fox/vfox/internal/env"
4041
"github.com/version-fox/vfox/internal/logger"
42+
"github.com/version-fox/vfox/internal/plugin"
4143
"github.com/version-fox/vfox/internal/toolset"
4244
"github.com/version-fox/vfox/internal/util"
4345
)
@@ -90,7 +92,7 @@ func (m *Manager) GlobalEnvKeys() (SdkEnvs, error) {
9092
return m.EnvKeys(toolset.MultiToolVersions{
9193
workToolVersion,
9294
homeToolVersion,
93-
}, ShellLocation)
95+
}, base.ShellLocation)
9496
}
9597

9698
type SessionEnvOptions struct {
@@ -171,8 +173,8 @@ func (m *Manager) SessionEnvKeys(opt SessionEnvOptions) (SdkEnvs, error) {
171173
} else {
172174
logger.Debugf("No hit cache, name: %s cache: %s, expected: %s \n", name, string(vv), version)
173175
}
174-
v := Version(version)
175-
if keys, err := lookupSdk.EnvKeys(v, ShellLocation); err == nil {
176+
v := base.Version(version)
177+
if keys, err := lookupSdk.EnvKeys(v, base.ShellLocation); err == nil {
176178
flushCache.Set(name, cache.Value(version), cache.NeverExpired)
177179

178180
sdkEnvs = append(sdkEnvs, &SdkEnv{
@@ -189,7 +191,7 @@ func (m *Manager) SessionEnvKeys(opt SessionEnvOptions) (SdkEnvs, error) {
189191
return sdkEnvs, nil
190192
}
191193

192-
func (m *Manager) EnvKeys(tvs toolset.MultiToolVersions, location Location) (SdkEnvs, error) {
194+
func (m *Manager) EnvKeys(tvs toolset.MultiToolVersions, location base.Location) (SdkEnvs, error) {
193195
var sdkEnvs SdkEnvs
194196
tools := make(map[string]struct{})
195197
for _, t := range tvs {
@@ -198,7 +200,7 @@ func (m *Manager) EnvKeys(tvs toolset.MultiToolVersions, location Location) (Sdk
198200
continue
199201
}
200202
if lookupSdk, err := m.LookupSdk(name); err == nil {
201-
v := Version(version)
203+
v := base.Version(version)
202204
if ek, err := lookupSdk.EnvKeys(v, location); err == nil {
203205
tools[name] = struct{}{}
204206

@@ -631,7 +633,7 @@ func (m *Manager) Add(pluginName, url, alias string) error {
631633
//
632634
// 1.only support .lua or .zip file type plugin.
633635
// 2.install plugin to temp dir first, then validate the plugin, if success, return *LuaPlugin
634-
func (m *Manager) installPluginToTemp(path string) (*PluginWrapper, error) {
636+
func (m *Manager) installPluginToTemp(path string) (*plugin.PluginWrapper, error) {
635637
ext := filepath.Ext(path)
636638
if ext != ".lua" && ext != ".zip" {
637639
return nil, fmt.Errorf("unsupported %s type plugin to install, only support .lua or .zip", ext)
@@ -675,7 +677,7 @@ func (m *Manager) installPluginToTemp(path string) (*PluginWrapper, error) {
675677
// validate the plugin
676678
fmt.Printf("Validating %s ...\n", localPath)
677679

678-
plugin, err := CreatePluginFromPath(tempInstallPath, m)
680+
plugin, err := plugin.CreatePluginFromPath(tempInstallPath, m.Config, RuntimeVersion)
679681
if err != nil {
680682
return nil, fmt.Errorf("validate plugin failed: %w", err)
681683
}

0 commit comments

Comments
 (0)