@@ -229,3 +229,27 @@ func getWslStatus(instName string) (string, error) {
229229
230230 return instState , nil
231231}
232+
233+ func getSSHAddress (ctx context.Context , instName string ) (string , error ) {
234+ distroName := "lima-" + instName
235+ // Ubuntu
236+ cmd := exec .CommandContext (ctx , "wsl.exe" , "-d" , distroName , "bash" , "-c" , `hostname -I | cut -d ' ' -f1` )
237+ out , err := cmd .CombinedOutput ()
238+ if err == nil {
239+ return strings .TrimSpace (string (out )), nil
240+ }
241+ // Alpine
242+ cmd = exec .CommandContext (ctx , "wsl.exe" , "-d" , distroName , "sh" , "-c" , `ip route get 1 | awk '{gsub("^.*src ",""); print $1; exit}'` )
243+ out , err = cmd .CombinedOutput ()
244+ if err == nil {
245+ return strings .TrimSpace (string (out )), nil
246+ }
247+ // fallback
248+ cmd = exec .CommandContext (ctx , "wsl.exe" , "-d" , distroName , "hostname" , "-i" )
249+ out , err = cmd .CombinedOutput ()
250+ if err != nil || strings .HasPrefix (string (out ), "127." ) {
251+ return "" , fmt .Errorf ("failed to get hostname for instance %q, err: %w (out=%q)" , instName , err , string (out ))
252+ }
253+
254+ return strings .TrimSpace (string (out )), nil
255+ }
0 commit comments