Files
releases/internal/file/url/url.go
T
Torin Sandall 6a0fec7fb4 cmd: Fix eval and fmt to support file:// URLs
Users should be able to pass file:// URLs to any of the
sub-commands. In 3be55ed6 the eval and fmt sub-commands were not
updated to accept file:// URLs for the input file and normal paths
(respectively).

This commit just moves the unexported cleanFileURL function from the
loader package into it's own internal package so that it can be shared
in OPA.

Signed-off-by: Torin Sandall <torinsandall@gmail.com>
2019-09-06 12:29:37 -07: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
}