-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurl.go
55 lines (50 loc) · 1.28 KB
/
url.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
)
type URL struct {
raw_url string
*url.URL
}
func Parse(raw_url string) (*URL, error) {
log(LOG_DEBUG, "Expanding potential tildes in the path\n")
raw_url = expandTilde(raw_url)
new_url := raw_url
parsed, err := url.Parse(raw_url)
if err != nil {
return nil, fmt.Errorf("Got an error while parsning: %v\n", err)
}
if parsed.Scheme == "" {
_, err := os.Open(raw_url)
if !errors.Is(err, os.ErrNotExist) { // If it exists, it is a file
raw_url, err = filepath.Abs(raw_url)
if err != nil {
return nil, fmt.Errorf("Error getting the absolute path: %w", err)
}
log(LOG_DEBUG, "Adding the file:// scheme to the url for disambiguation\n")
new_url = fmt.Sprintf("file://%s", raw_url)
parsed, err = url.Parse(new_url)
if err != nil {
return nil, fmt.Errorf("Error in reparsing the url after expanding the tilde: %v\n", err)
}
}
/*else { // If not we assume it is a URL
log(LOG_DEBUG, "Adding the http:// scheme to the url for disambiguation\n")
new_url = fmt.Sprintf("http://%s", raw_url)
}*/
}
return &URL{
raw_url: new_url,
URL: parsed,
}, err
}
func (this *URL) String() string {
if this.Path != this.raw_url {
return this.raw_url
}
return this.URL.String()
}