github.com/michaeltrobinson/godep@v0.0.0-20160912215839-8088bcf2e78b/util.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"os/exec"
     7  	"path/filepath"
     8  	"runtime"
     9  	"strings"
    10  )
    11  
    12  // Runs a command in dir.
    13  // The name and args are as in exec.Command.
    14  // Stdout, stderr, and the environment are inherited
    15  // from the current process.
    16  func runIn(dir, name string, args ...string) error {
    17  	_, err := runInWithOutput(dir, name, args...)
    18  	return err
    19  }
    20  
    21  func runInWithOutput(dir, name string, args ...string) (string, error) {
    22  	c := exec.Command(name, args...)
    23  	c.Dir = dir
    24  	o, err := c.CombinedOutput()
    25  
    26  	if debug {
    27  		fmt.Printf("execute: %+v\n", c)
    28  		fmt.Printf(" output: %s\n", string(o))
    29  	}
    30  
    31  	return string(o), err
    32  }
    33  
    34  // driveLetterToUpper converts Windows path's drive letters to uppercase. This
    35  // is needed when comparing 2 paths with different drive letter case.
    36  func driveLetterToUpper(path string) string {
    37  	if runtime.GOOS != "windows" || path == "" {
    38  		return path
    39  	}
    40  
    41  	p := path
    42  
    43  	// If path's drive letter is lowercase, change it to uppercase.
    44  	if len(p) >= 2 && p[1] == ':' && 'a' <= p[0] && p[0] <= 'z' {
    45  		p = string(p[0]+'A'-'a') + p[1:]
    46  	}
    47  
    48  	return p
    49  }
    50  
    51  // clean the path and ensure that a drive letter is upper case (if it exists).
    52  func cleanPath(path string) string {
    53  	return driveLetterToUpper(filepath.Clean(path))
    54  }
    55  
    56  // deal with case insensitive filesystems and other weirdness
    57  func pathEqual(a, b string) bool {
    58  	a = cleanPath(a)
    59  	b = cleanPath(b)
    60  
    61  	var err error
    62  	a, err = filepath.EvalSymlinks(a)
    63  	if err != nil {
    64  		log.Printf("failed to evaluate symlink: %s: %v", a, err)
    65  		return false
    66  	}
    67  	b, err = filepath.EvalSymlinks(b)
    68  	if err != nil {
    69  		log.Printf("failed to evaluate symlink: %s: %v", b, err)
    70  		return false
    71  	}
    72  
    73  	return strings.EqualFold(a, b)
    74  }