Skip to content

Commit

Permalink
feat: mock invoke EnvKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
aooohan committed May 18, 2024
1 parent 9693aac commit a2645d8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 11 deletions.
2 changes: 0 additions & 2 deletions internal/env/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ func (p *Paths) ToBinPaths() (*Paths, error) {
// NewPaths returns a new Paths.
// from is the source of the paths.
// If from is OsPaths, it returns the paths from the environment variable PATH.
// If from is PreviousPaths, it returns the paths from the environment variable __VFOX_PREVIOUS_PATHS
// If from is neither OsPaths nor PreviousPaths, it returns an empty Paths.
func NewPaths(from PathFrom) *Paths {
var paths []string
switch from {
Expand Down
15 changes: 14 additions & 1 deletion internal/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type LocationPackage struct {
location Location
}

func NewLocationPackage(version Version, sdk *Sdk, location Location) (*LocationPackage, error) {
func newLocationPackage(version Version, sdk *Sdk, location Location) (*LocationPackage, error) {
var mockPath string
switch location {
case OriginalLocation:
Expand Down Expand Up @@ -112,6 +112,19 @@ func (l *LocationPackage) Link() (*Package, error) {
return targetPackage, nil
}

// checkPackageValid checks if the package is valid
func checkPackageValid(p *Package) bool {
if !util.FileExists(p.Main.Path) {
return false
}
for _, a := range p.Additions {
if !util.FileExists(a.Path) {
return false
}
}
return true
}

type Package struct {
Main *Info
Additions []*Info
Expand Down
23 changes: 18 additions & 5 deletions internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ func (b *Sdk) Available(args []string) ([]*Package, error) {
}

func (b *Sdk) ToLinkPackage(version Version, location Location) error {
linkPackage, err := b.GetLinkPackage(version, ShellLocation)
linkPackage, err := b.GetLinkPackage(version, location)
if err != nil {
return err
}
Expand All @@ -271,10 +271,13 @@ func (b *Sdk) ToLinkPackage(version Version, location Location) error {

// GetLinkPackage will make symlink according Location and return the sdk package.
func (b *Sdk) GetLinkPackage(version Version, location Location) (*LocationPackage, error) {
return NewLocationPackage(version, b, location)
return newLocationPackage(version, b, location)
}

func (b *Sdk) EnvKeysWithoutLink(version Version, location Location) (*env.Envs, error) {
// MockEnvKeys It just simulates to get the environment configuration information,
// if the corresponding location of the package does not exist, then it will return
// the empty environment information, without calling the EnvKeys hook.
func (b *Sdk) MockEnvKeys(version Version, location Location) (*env.Envs, error) {
label := b.label(version)
if !b.CheckExists(version) {
return nil, fmt.Errorf("%s is not installed", label)
Expand All @@ -284,6 +287,14 @@ func (b *Sdk) EnvKeysWithoutLink(version Version, location Location) (*env.Envs,
return nil, err
}
sdkPackage := linkPackage.ConvertLocation()
if !checkPackageValid(sdkPackage) {
logger.Debugf("Package is invalid: %v\n", sdkPackage)
return &env.Envs{
Variables: make(env.Vars),
Paths: env.NewPaths(env.EmptyPaths),
BinPaths: env.NewPaths(env.EmptyPaths),
}, nil
}
keys, err := b.Plugin.EnvKeys(sdkPackage)
if err != nil {
return nil, fmt.Errorf("plugin [EnvKeys] error: err:%w", err)
Expand All @@ -305,6 +316,7 @@ func (b *Sdk) EnvKeys(version Version, location Location) (*env.Envs, error) {
if err != nil {
return nil, err
}

keys, err := b.Plugin.EnvKeys(sdkPackage)
if err != nil {
return nil, fmt.Errorf("plugin [EnvKeys] error: err:%w", err)
Expand Down Expand Up @@ -700,7 +712,7 @@ func (b *Sdk) ClearCurrentEnv() error {
}

if current != "" {
envKeys, err := b.EnvKeysWithoutLink(current, GlobalLocation)
envKeys, err := b.MockEnvKeys(current, GlobalLocation)
if err != nil {
return err
}
Expand All @@ -715,10 +727,11 @@ func (b *Sdk) ClearCurrentEnv() error {

envManager := b.sdkManager.EnvManager
fmt.Println("Cleaning up env config...")
envKeys.Paths.Add(filepath.Join(b.InstallPath, "current"))
_ = envManager.Remove(envKeys)
_ = envManager.Flush()

envKeys, err = b.EnvKeysWithoutLink(current, OriginalLocation)
envKeys, err = b.MockEnvKeys(current, OriginalLocation)
if err != nil {
return err
}
Expand Down
25 changes: 22 additions & 3 deletions internal/util/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,30 @@ import (
)

func FileExists(filename string) bool {
_, err := os.Stat(filename)
if os.IsNotExist(err) {
fileInfo, err := os.Lstat(filename)
if err != nil {
if os.IsNotExist(err) {
return false
}
return false
}
return err == nil

if fileInfo.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(filename)
if err != nil {
return false
}
_, err = os.Stat(target)
if err != nil {
if os.IsNotExist(err) {
return false
}
return false
}
return true
}

return true
}

func CopyFile(src, dst string) error {
Expand Down

0 comments on commit a2645d8

Please sign in to comment.