tools: wrap os.Getwd with cygwin support

This commit is contained in:
Taylor Blau 2017-02-20 15:22:00 -07:00
parent 48f55dddf5
commit 88522b4e04
3 changed files with 76 additions and 0 deletions

7
tools/cygwin.go Normal file

@ -0,0 +1,7 @@
// +build !windows
package tools
func isCygwin() bool {
return false
}

18
tools/cygwin_windows.go Normal file

@ -0,0 +1,18 @@
// +build windows
package tools
import (
"bytes"
"github.com/git-lfs/git-lfs/subprocess"
)
func isCygwin() bool {
cmd := subprocess.ExecCommand("uname")
out, err := cmd.Output()
if err != nil {
return false
}
return bytes.Contains(out, []byte("CYGWIN"))
}

51
tools/os_tools.go Normal file

@ -0,0 +1,51 @@
package tools
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/git-lfs/git-lfs/subprocess"
"github.com/pkg/errors"
)
func Getwd() (dir string, err error) {
dir, err = os.Getwd()
if err != nil {
return
}
if isCygwin() {
dir, err = translateCygwinPath(dir)
if err != nil {
return "", errors.Wrap(err, "convert wd to cygwin")
}
}
return
}
func translateCygwinPath(path string) (string, error) {
cmd := subprocess.ExecCommand("cygpath", "-w", path)
buf := &bytes.Buffer{}
cmd.Stderr = buf
out, err := cmd.Output()
output := strings.TrimSpace(string(out))
if err != nil {
return path, fmt.Errorf("Failed to translate path from cygwin to windows: %s", buf.String())
}
return output, nil
}
func TranslateIfCygwin(path string) (string, error) {
if isCygwin() {
var err error
path, err = translateCygwinPath(path)
if err != nil {
return "", err
}
}
return path, nil
}