Files
kubesphere/vendor/github.com/open-policy-agent/opa/internal/file/url/url.go
hongming 9769357005 update
Signed-off-by: hongming <talonwan@yunify.com>
2020-03-20 02:16:11 +08:00

43 lines
985 B
Go

// Copyright 2019 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
// Package url contains helpers for dealing with file paths and URLs.
package url
import (
"fmt"
"net/url"
"runtime"
"strings"
)
var goos = runtime.GOOS
// Clean returns a cleaned file path that may or may not be a URL.
func Clean(path string) (string, error) {
if strings.Contains(path, "://") {
url, err := url.Parse(path)
if err != nil {
return "", err
}
if url.Scheme != "file" {
return "", fmt.Errorf("unsupported URL scheme: %v", path)
}
path = url.Path
// Trim leading slash on Windows if present. The url.Path field returned
// by url.Parse has leading slash that causes CreateFile() calls to fail
// on Windows. See https://github.com/golang/go/issues/6027 for details.
if goos == "windows" && len(path) >= 1 && path[0] == '/' {
path = path[1:]
}
}
return path, nil
}