Skip to content

Commit

Permalink
bugfix: Global scope does not take effect when use out of box on Windows
Browse files Browse the repository at this point in the history
close #36
  • Loading branch information
aooohan committed Feb 5, 2024
1 parent 0f236a4 commit 32421d1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion internal/env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

type Manager interface {
Flush() error
Load(key, value string)
Load(key, value string) error
Get(key string) (string, bool)
Remove(key string) error
Paths(paths []string) string
Expand Down
3 changes: 2 additions & 1 deletion internal/env/macos_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,12 @@ func (m *macosEnvManager) Close() error {
return nil
}

func (m *macosEnvManager) Load(key, value string) {
func (m *macosEnvManager) Load(key, value string) error {
m.store.Add(&KV{
Key: key,
Value: value,
})
return nil
}
func (m *macosEnvManager) Remove(key string) error {
if key == "PATH" {
Expand Down
32 changes: 23 additions & 9 deletions internal/env/windows_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
type windowsEnvManager struct {
key registry.Key
// $PATH
paths []string
pathMap map[string]struct{}
deletedPathMap map[string]struct{}
}
Expand All @@ -53,23 +54,25 @@ func (w *windowsEnvManager) loadPathValue() error {
}
s := strings.Split(val, ";")
for _, path := range s {
w.paths = append(w.paths, path)
w.pathMap[path] = struct{}{}
}
return nil
}

func (w *windowsEnvManager) Flush() (err error) {
// TODO move this close method to other place
defer w.key.Close()
customPaths := make([]string, 0, len(w.pathMap))
customPathSet := make(map[string]struct{})
if len(w.pathMap) > 0 {
for path := range w.pathMap {
for i := len(w.paths) - 1; i >= 0; i-- {
path := w.paths[i]
customPaths = append(customPaths, path)
customPathSet[path] = struct{}{}
}
pathValue := strings.Join(customPaths, ";")
w.Load("VERSION_FOX_PATH", pathValue)
if err = w.Load("VERSION_FOX_PATH", pathValue); err != nil {
return err
}
} else {
_ = w.Remove("VERSION_FOX_PATH")
}
Expand All @@ -89,7 +92,9 @@ func (w *windowsEnvManager) Flush() (err error) {
}
userNewPaths = append(userNewPaths, v)
}
w.key.SetStringValue("PATH", strings.Join(userNewPaths, ";"))
if err = w.key.SetStringValue("PATH", strings.Join(userNewPaths, ";")); err != nil {
return err
}
// sys env
sysPath := os.Getenv("PATH")
s2 := strings.Split(sysPath, ";")
Expand All @@ -103,23 +108,31 @@ func (w *windowsEnvManager) Flush() (err error) {
}
sysNewPaths = append(sysNewPaths, v)
}
os.Setenv("PATH", strings.Join(sysNewPaths, ";"))
if err = os.Setenv("PATH", strings.Join(sysNewPaths, ";")); err != nil {
return err
}
_ = w.broadcastEnvironment()
return
}

func (w *windowsEnvManager) Load(key, value string) {
func (w *windowsEnvManager) Load(key, value string) error {
if key == "PATH" {
w.pathMap[value] = struct{}{}
_, ok := w.pathMap[value]
if !ok {
w.pathMap[value] = struct{}{}
w.paths = append(w.paths, value)
}
} else {
// TODO handle error
err := os.Setenv(key, value)
if err != nil {
return err
}
err = w.key.SetStringValue(key, value)
if err != nil {
return err
}
}
return nil
}

func (w *windowsEnvManager) Get(key string) (string, bool) {
Expand Down Expand Up @@ -182,6 +195,7 @@ func NewEnvManager(vfConfigPath string) (Manager, error) {
key: k,
pathMap: make(map[string]struct{}),
deletedPathMap: make(map[string]struct{}),
paths: make([]string, 0),
}
err = manager.loadPathValue()
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion internal/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,9 @@ func (b *Sdk) Use(version Version, scope UseScope) error {
b.clearCurrentEnvConfig()

for key, value := range keys {
b.sdkManager.EnvManager.Load(key, *value)
if err = b.sdkManager.EnvManager.Load(key, *value); err != nil {
return err
}
}
err = b.sdkManager.EnvManager.Flush()
if err != nil {
Expand Down

0 comments on commit 32421d1

Please sign in to comment.