github.com/arvindram03/terraform@v0.3.7-0.20150212015210-408f838db36d/config/module/url_helper.go (about)

     1  package module
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"path/filepath"
     7  	"runtime"
     8  	"strings"
     9  )
    10  
    11  func urlParse(rawURL string) (*url.URL, error) {
    12  	if runtime.GOOS == "windows" {
    13  		// Make sure we're using "/" on Windows. URLs are "/"-based.
    14  		rawURL = filepath.ToSlash(rawURL)
    15  	}
    16  	u, err := url.Parse(rawURL)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	if runtime.GOOS != "windows" {
    22  		return u, err
    23  	}
    24  
    25  	if len(rawURL) > 1 && rawURL[1] == ':' {
    26  		// Assume we're dealing with a drive letter file path on Windows.
    27  		// We need to adjust the URL Path for drive letter file paths
    28  		// because url.Parse("c:/users/user") yields URL Scheme = "c"
    29  		// and URL path = "/users/user".
    30  		u.Path = fmt.Sprintf("%s:%s", u.Scheme, u.Path)
    31  		u.Scheme = ""
    32  	}
    33  
    34  	if len(u.Host) > 1 && u.Host[1] == ':' && strings.HasPrefix(rawURL, "file://") {
    35  		// Assume we're dealing with a drive letter file path on Windows
    36  		// where the drive letter has been parsed into the URL Host.
    37  		u.Path = fmt.Sprintf("%s%s", u.Host, u.Path)
    38  		u.Host = ""
    39  	}
    40  
    41  	// Remove leading slash for absolute file paths on Windows.
    42  	// For example, url.Parse yields u.Path = "/C:/Users/user" for
    43  	// rawURL = "file:///C:/Users/user", which is an incorrect syntax.
    44  	if len(u.Path) > 2 && u.Path[0] == '/' && u.Path[2] == ':' {
    45  		u.Path = u.Path[1:]
    46  	}
    47  
    48  	return u, err
    49  }
    50  
    51  func fmtFileURL(path string) string {
    52  	if runtime.GOOS == "windows" {
    53  		// Make sure we're using "/" on Windows. URLs are "/"-based.
    54  		path = filepath.ToSlash(path)
    55  	}
    56  
    57  	// Make sure that we don't start with "/" since we add that below.
    58  	if path[0] == '/' {
    59  		path = path[1:]
    60  	}
    61  
    62  	return fmt.Sprintf("file:///%s", path)
    63  }