Skip to content

Commit 8b72367

Browse files
committed
chore: build fix
1 parent 1ef00ae commit 8b72367

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

utils/env_unix.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//go:build !windows
2+
3+
package utils
4+
5+
import "os"
6+
7+
// GetFreshEnviron returns the current environment variables.
8+
// On Unix systems, there's no central registry for environment variables,
9+
// so this just returns os.Environ().
10+
// If you need to pick up changes from shell profiles (.bashrc, .zshrc),
11+
// the shell itself will handle that when started as a login shell.
12+
func GetFreshEnviron() []string {
13+
return os.Environ()
14+
}
15+
16+
// GetFreshPath returns the current PATH environment variable.
17+
// On Unix, environment changes require re-sourcing shell profiles.
18+
func GetFreshPath() string {
19+
return os.Getenv("PATH")
20+
}
21+
22+
// RefreshProcessEnviron is a no-op on Unix systems.
23+
// Shell profiles are sourced when shells start, not at process level.
24+
func RefreshProcessEnviron() error {
25+
return nil
26+
}

utils/env_windows.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
//go:build windows
2+
3+
package utils
4+
5+
import (
6+
"os"
7+
"strings"
8+
9+
"golang.org/x/sys/windows/registry"
10+
)
11+
12+
// GetFreshEnviron returns the current environment variables with PATH refreshed from Windows registry.
13+
// This is useful for terminal managers that need to pick up newly installed tools.
14+
func GetFreshEnviron() []string {
15+
// Start with current environment
16+
env := os.Environ()
17+
18+
// Get fresh PATH from registry
19+
freshPath := GetFreshPath()
20+
if freshPath == "" {
21+
return env
22+
}
23+
24+
// Replace PATH in the environment
25+
result := make([]string, 0, len(env))
26+
pathReplaced := false
27+
for _, e := range env {
28+
if strings.HasPrefix(strings.ToUpper(e), "PATH=") {
29+
result = append(result, "PATH="+freshPath)
30+
pathReplaced = true
31+
} else {
32+
result = append(result, e)
33+
}
34+
}
35+
36+
if !pathReplaced {
37+
result = append(result, "PATH="+freshPath)
38+
}
39+
40+
return result
41+
}
42+
43+
// GetFreshPath reads and combines PATH from Windows registry (system + user).
44+
// Returns the combined PATH or empty string on error.
45+
func GetFreshPath() string {
46+
var paths []string
47+
48+
// System PATH (HKEY_LOCAL_MACHINE)
49+
if sysPath := getRegistryPath(
50+
registry.LOCAL_MACHINE,
51+
`SYSTEM\CurrentControlSet\Control\Session Manager\Environment`,
52+
"Path",
53+
); sysPath != "" {
54+
paths = append(paths, sysPath)
55+
}
56+
57+
// User PATH (HKEY_CURRENT_USER)
58+
if userPath := getRegistryPath(
59+
registry.CURRENT_USER,
60+
`Environment`,
61+
"Path",
62+
); userPath != "" {
63+
paths = append(paths, userPath)
64+
}
65+
66+
if len(paths) == 0 {
67+
return ""
68+
}
69+
70+
return strings.Join(paths, ";")
71+
}
72+
73+
// getRegistryPath reads a string value from the Windows registry.
74+
func getRegistryPath(root registry.Key, keyPath, valueName string) string {
75+
key, err := registry.OpenKey(root, keyPath, registry.QUERY_VALUE)
76+
if err != nil {
77+
return ""
78+
}
79+
defer key.Close()
80+
81+
// Try to read as expandable string first (REG_EXPAND_SZ)
82+
value, valType, err := key.GetStringValue(valueName)
83+
if err != nil {
84+
return ""
85+
}
86+
87+
// Expand environment variables if it's a REG_EXPAND_SZ type
88+
if valType == registry.EXPAND_SZ {
89+
value = os.ExpandEnv(value)
90+
}
91+
92+
return value
93+
}
94+
95+
// RefreshProcessEnviron updates the current process's PATH environment variable
96+
// from the Windows registry. Call this if you want all future os.Environ() calls
97+
// to include the updated PATH.
98+
func RefreshProcessEnviron() error {
99+
freshPath := GetFreshPath()
100+
if freshPath == "" {
101+
return nil
102+
}
103+
return os.Setenv("PATH", freshPath)
104+
}

0 commit comments

Comments
 (0)