|
| 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