@@ -12,11 +12,11 @@ var once sync.Once
1212var info * WSLInfo
1313
1414type WSLInfo struct {
15- IsWSL bool // True if running under WSL1 or WSL2
16- DistroName string // Value of WSL_DISTRO_NAME
17- KernelVersion string // Contents of /proc/version
18- InteropPath string // Value of WSL_INTEROP
19- WindowsHome string // Windows home directory as a WSL path
15+ IsWSL bool // True if running under WSL1 or WSL2
16+ DistroName string // Value of WSL_DISTRO_NAME
17+ KernelVersion string // Contents of /proc/version
18+ InteropPath string // Value of WSL_INTEROP
19+ WindowsHome string // Windows home directory as a WSL path
2020}
2121
2222// loadWSLInfo detects WSL environment details and caches them in the info variable.
@@ -27,100 +27,100 @@ type WSLInfo struct {
2727// This is called lazily by public helper functions to avoid unnecessary overhead
2828// of init() in non-WSL environments.
2929func loadWSLInfo () {
30- once .Do (func () {
31- i := & WSLInfo {}
30+ once .Do (func () {
31+ i := & WSLInfo {}
3232
33- // Detect WSL via environment variable
34- if dn := os .Getenv ("WSL_DISTRO_NAME" ); dn != "" {
35- i .IsWSL = true
36- i .DistroName = dn
37- }
33+ // Detect WSL via environment variable
34+ if dn := os .Getenv ("WSL_DISTRO_NAME" ); dn != "" {
35+ i .IsWSL = true
36+ i .DistroName = dn
37+ }
3838
39- // Kernel signature fallback
40- if data , err := os .ReadFile ("/proc/version" ); err == nil {
41- ver := strings .ToLower (string (data ))
42- i .KernelVersion = ver
39+ // Kernel signature fallback
40+ if data , err := os .ReadFile ("/proc/version" ); err == nil {
41+ ver := strings .ToLower (string (data ))
42+ i .KernelVersion = ver
4343
4444 // Check for "microsoft" in kernel version to detect WSL if env var is missing
45- if strings .Contains (ver , "microsoft" ) {
46- i .IsWSL = true
47- }
48- }
49-
50- // Interop path (could be useful someday)
51- i .InteropPath = os .Getenv ("WSL_INTEROP" )
52-
53- // Windows home directory (only if WSL)
54- if i .IsWSL {
55- home , err := getWindowsHomePath ()
56- if err == nil {
57- i .WindowsHome = home
58- }
59- }
60-
61- info = i
62- })
45+ if strings .Contains (ver , "microsoft" ) {
46+ i .IsWSL = true
47+ }
48+ }
49+
50+ // Interop path (could be useful someday)
51+ i .InteropPath = os .Getenv ("WSL_INTEROP" )
52+
53+ // Windows home directory (only if WSL)
54+ if i .IsWSL {
55+ home , err := getWindowsHomePath ()
56+ if err == nil {
57+ i .WindowsHome = home
58+ }
59+ }
60+
61+ info = i
62+ })
6363}
6464
6565// Info returns cached WSL environment information.
6666func Info () * WSLInfo {
67- loadWSLInfo ()
68- return info
67+ loadWSLInfo ()
68+ return info
6969}
7070
7171// IsWSL returns true if running under WSL.
7272func IsWSL () bool {
73- loadWSLInfo ()
74- return info .IsWSL
73+ loadWSLInfo ()
74+ return info .IsWSL
7575}
7676
7777// WindowsHome returns the Windows user's home directory as a WSL path.
7878func WindowsHome () (string , error ) {
79- loadWSLInfo ()
80- if info .WindowsHome == "" {
81- return "" , fmt .Errorf ("unable to determine Windows home directory" )
82- }
83- return info .WindowsHome , nil
79+ loadWSLInfo ()
80+ if info .WindowsHome == "" {
81+ return "" , fmt .Errorf ("unable to determine Windows home directory" )
82+ }
83+ return info .WindowsHome , nil
8484}
8585
8686// ToWSLPath converts a Windows path (C:\Users\Me) → /mnt/c/Users/Me.
8787func ToWSLPath (winPath string ) (string , error ) {
88- out , err := exec .Command ("wslpath" , "-u" , winPath ).Output ()
89- if err != nil {
90- return "" , fmt .Errorf ("wslpath failed: %w" , err )
91- }
92- return strings .TrimSpace (string (out )), nil
88+ out , err := exec .Command ("wslpath" , "-u" , winPath ).Output ()
89+ if err != nil {
90+ return "" , fmt .Errorf ("wslpath failed: %w" , err )
91+ }
92+ return strings .TrimSpace (string (out )), nil
9393}
9494
9595// ToWindowsPath converts a WSL path (/mnt/c/Users/Me) → C:\Users\Me.
9696func ToWindowsPath (wslPath string ) (string , error ) {
97- out , err := exec .Command ("wslpath" , "-w" , wslPath ).Output ()
98- if err != nil {
99- return "" , fmt .Errorf ("wslpath failed: %w" , err )
100- }
101- return strings .TrimSpace (string (out )), nil
97+ out , err := exec .Command ("wslpath" , "-w" , wslPath ).Output ()
98+ if err != nil {
99+ return "" , fmt .Errorf ("wslpath failed: %w" , err )
100+ }
101+ return strings .TrimSpace (string (out )), nil
102102}
103103
104104// ExecWindows runs a Windows command and returns stdout.
105105func ExecWindows (command string ) (string , error ) {
106106 // Use cmd.exe to run the command
107- cmd := exec .Command ("cmd.exe" , "/c" , command )
108- out , err := cmd .Output ()
109- if err != nil {
110- return "" , fmt .Errorf ("cmd.exe failed: %w" , err )
111- }
107+ cmd := exec .Command ("cmd.exe" , "/c" , command )
108+ out , err := cmd .Output ()
109+ if err != nil {
110+ return "" , fmt .Errorf ("cmd.exe failed: %w" , err )
111+ }
112112
113113 // Clean CRLF from Windows output
114- return strings .TrimSpace (strings .ReplaceAll (string (out ), "\r " , "" )), nil
114+ return strings .TrimSpace (strings .ReplaceAll (string (out ), "\r " , "" )), nil
115115}
116116
117117func getWindowsHomePath () (string , error ) {
118- // Use ExecWindows helper
119- winPath , err := ExecWindows ("echo %USERPROFILE%" )
120- if err != nil {
121- return "" , err
122- }
123-
124- // Convert to WSL path
125- return ToWSLPath (winPath )
118+ // Use ExecWindows helper
119+ winPath , err := ExecWindows ("echo %USERPROFILE%" )
120+ if err != nil {
121+ return "" , err
122+ }
123+
124+ // Convert to WSL path
125+ return ToWSLPath (winPath )
126126}
0 commit comments