github.com/linux4life798/complete@v1.1.2-0.20180410072631-7426158f3bcb/utils.go (about)

     1  package complete
     2  
     3  import (
     4  	"os"
     5  	"path/filepath"
     6  	"strings"
     7  )
     8  
     9  // fixPathForm changes a file name to a relative name
    10  func fixPathForm(last string, file string) string {
    11  	// get wording directory for relative name
    12  	workDir, err := os.Getwd()
    13  	if err != nil {
    14  		return file
    15  	}
    16  
    17  	abs, err := filepath.Abs(file)
    18  	if err != nil {
    19  		return file
    20  	}
    21  
    22  	// if last is absolute, return path as absolute
    23  	if filepath.IsAbs(last) {
    24  		return fixDirPath(abs)
    25  	}
    26  
    27  	rel, err := filepath.Rel(workDir, abs)
    28  	if err != nil {
    29  		return file
    30  	}
    31  
    32  	// fix ./ prefix of path
    33  	if rel != "." && strings.HasPrefix(last, ".") {
    34  		rel = "./" + rel
    35  	}
    36  
    37  	return fixDirPath(rel)
    38  }
    39  
    40  func fixDirPath(path string) string {
    41  	info, err := os.Stat(path)
    42  	if err == nil && info.IsDir() && !strings.HasSuffix(path, "/") {
    43  		path += "/"
    44  	}
    45  	return path
    46  }