Skip to content

Instantly share code, notes, and snippets.

@narma
Created August 8, 2023 15:10
Show Gist options
  • Save narma/ff339e13434a0d8acff382298ae5ac05 to your computer and use it in GitHub Desktop.
Save narma/ff339e13434a0d8acff382298ae5ac05 to your computer and use it in GitHub Desktop.
Execute SublimeText from wsl
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
const (
DISTRO = "Ubuntu"
SUBLIME_TEXT_LOCATION = "/mnt/c/Program Files/Sublime Text/subl.exe"
)
func abort(msg string) {
// fmt.Printf("Error: %s", msg)
panic(msg)
}
func transformFileArg(arg string) string {
fullPath, err := filepath.Abs(arg)
if err != nil {
abort("Can't open file")
}
return fmt.Sprintf("\\\\wsl.localhost\\%s%s", DISTRO, fullPath)
}
func transformArg(arg string) string {
if strings.HasPrefix(arg, "-") {
return arg
} else {
return transformFileArg(arg)
}
}
func main() {
argsWithoutProg := os.Args[1:]
argsCount := len(argsWithoutProg)
newArgs := make([]string, 0, argsCount)
for _, arg := range argsWithoutProg {
newArgs = append(newArgs, transformArg(arg))
}
// fmt.Printf("run with args: %v\n", newArgs)
cmd := exec.Command(SUBLIME_TEXT_LOCATION, newArgs...)
if err := cmd.Run(); err != nil {
fmt.Printf("error: %v", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment