I need to execute some CLI commands, each command uses the path of the workingDir (directory where the call has been made) as parameter without leaving me the opportunity to specify it manually, ok, so I solve it like this:
// from "os/exec" package
cmd:= exec.Command("sommeCLI", "-someParam")
customWorkPath := "C:\\someFolder\\sub\\src"
cmd.Dir = customWorkPath
out, err := cmd.Output()
This works and no errors, but if customWorkPath is equal to this:
customWorkPath := "C:\\Program Files\\someProgramName"
I get this error (this error is from the CLI programs I use, not from the cmd that executes the command):
chdir "C:\Program: The filename, directory name, or volume label syntax is incorrect.
That happens because customWorkPath has spaces I have tried the following:
customWorkPath := fmt.Sprintf("\"%s\"", somePath)
customWorkPath := fmt.Sprintf(`%s`, somePath)
But still errors keep occurring so far this is the only thing that has worked for me:
customWorkPath := "C:\\Progra~1\\Go\\src\\_local_"
But that is a very ugly solution since I do it manually, I don’t know if there is a package that does it automatically on Linux and Windows (I have looked for it with no luck).
Anyway, this is my question, what options do I have to make cmd.Dir work with paths that have spaces?