github.com/ezbuy/gauge@v0.9.4-0.20171013092048-7ac5bd3931cd/util/uriUtils.go (about)

     1  // Copyright 2015 ThoughtWorks, Inc.
     2  
     3  // This file is part of Gauge.
     4  
     5  // Gauge is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  
    10  // Gauge is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU General Public License for more details.
    14  
    15  // You should have received a copy of the GNU General Public License
    16  // along with Gauge.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package util
    19  
    20  import (
    21  	"strings"
    22  )
    23  
    24  const (
    25  	uriPrefix      = "file://"
    26  	unixSep        = "/"
    27  	windowColonRep = "%3A"
    28  	colon          = ":"
    29  	windowsSep     = "\\"
    30  )
    31  
    32  // ConvertURItoFilePath - converts file uri (eg: file://example.spec) to OS specific file paths.
    33  func ConvertURItoFilePath(uri string) string {
    34  	if IsWindows() {
    35  		return convertURIToWindowsPath(uri)
    36  	}
    37  	return convertURIToUnixPath(uri)
    38  }
    39  
    40  func convertURIToWindowsPath(uri string) string {
    41  	uri = strings.TrimPrefix(uri, uriPrefix+unixSep)
    42  	uri = strings.Replace(uri, windowColonRep, colon, -1)
    43  	return strings.Replace(uri, unixSep, windowsSep, -1)
    44  }
    45  
    46  func convertURIToUnixPath(uri string) string {
    47  	return strings.TrimPrefix(uri, uriPrefix)
    48  }
    49  
    50  // ConvertPathToURI - converts OS specific file paths to file uri (eg: file://example.spec).
    51  func ConvertPathToURI(path string) string {
    52  	if IsWindows() {
    53  		return convertWindowsPathToURI(path)
    54  	}
    55  	return convertUnixPathToURI(path)
    56  }
    57  
    58  func convertWindowsPathToURI(path string) string {
    59  	path = strings.Replace(path, colon, windowColonRep, -1)
    60  	return uriPrefix + unixSep + strings.Replace(path, windowsSep, unixSep, -1)
    61  }
    62  
    63  func convertUnixPathToURI(path string) string {
    64  	return uriPrefix + path
    65  }