github.com/mayur-tolexo/godep@v0.0.0-20170205210856-a9cd0561f946/util.go (about)

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