From e9a33688e17b31c330a0c712b67a5a8621b206a7 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 4 Jun 2020 10:46:27 +0100 Subject: [PATCH] Missing file --- internal/sqlutil/uri.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 internal/sqlutil/uri.go diff --git a/internal/sqlutil/uri.go b/internal/sqlutil/uri.go new file mode 100644 index 000000000..f72e02426 --- /dev/null +++ b/internal/sqlutil/uri.go @@ -0,0 +1,24 @@ +package sqlutil + +import ( + "fmt" + "net/url" +) + +// ParseFileURI returns the filepath in the given file: URI. Specifically, this will handle +// both relative (file:foo.db) and absolute (file:///path/to/foo) paths. +func ParseFileURI(dataSourceName string) (string, error) { + uri, err := url.Parse(dataSourceName) + if err != nil { + return "", err + } + var cs string + if uri.Opaque != "" { // file:filename.db + cs = uri.Opaque + } else if uri.Path != "" { // file:///path/to/filename.db + cs = uri.Path + } else { + return "", fmt.Errorf("invalid file uri: %s", dataSourceName) + } + return cs, nil +}