skratchdot / open-golang

Open a file, directory, or URI using the OS's default application for that object type. Optionally, you can specify an application to use.
MIT License
783 stars 65 forks source link

wsl support #29

Open mcandre opened 3 years ago

mcandre commented 3 years ago

Note that WSL environments should invoke powershell.exe Start <pth>. I've updated my little karp utility to account for this.

https://github.com/mcandre/karp

coezbek commented 2 years ago

A workaround until WSL is supported:

var is_wsl bool = isWSL()

func isWSL() bool {

    if runtime.GOOS != "linux" {
        return false
    }

    cmd := exec.Command("uname", "-a")
    if output, err := cmd.Output(); err == nil {
        if strings.Contains(strings.ToLower(string(output)), "microsoft") {
            return true
        }
    }
    return false
}

func openFile(filename string) error {

    if is_wsl {
        return exec.Command("wslview", filename).Start()
    } else {
        return open.Start(filename)
    }
}
coezbek commented 2 years ago

How does that work with WSL paths?

sstrudeau commented 1 year ago

I ran into this issue when working with fly.io's flyctl command line app in Ubuntu on WSL for Windows. I ended up creating a small shell script and putting it in my path to override the default xdg-open behavior to invoke powershell instead:

#!/bin/sh
powershell.exe /C start $@