forked from gopasspw/gopass
/
url.go
171 lines (157 loc) · 3.4 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package backend
import (
"fmt"
"net"
"net/url"
"path/filepath"
"runtime"
"strings"
homedir "github.com/mitchellh/go-homedir"
)
// URL is a parsed backend URL
type URL struct {
url *url.URL
Crypto CryptoBackend
RCS RCSBackend
Storage StorageBackend
Scheme string
Host string
Port string
Path string
Username string
Password string
Query url.Values
}
// FromPath returns a new backend URL with the given path
// and default backends (GitCLI, GPGCLI, FS)
func FromPath(path string) *URL {
return &URL{
Crypto: GPGCLI,
RCS: GitCLI,
Storage: FS,
Scheme: "file",
Path: path,
}
}
// ParseURL attempts to parse an backend URL
func ParseURL(us string) (*URL, error) {
// if it's no URL build file URL and parse that
nu, err := url.Parse(us)
if err != nil {
nu, err = url.Parse("gpgcli-gitcli-fs+file://" + us)
if err != nil {
return nil, err
}
}
u := &URL{
url: nu,
}
if err := u.parseScheme(); err != nil {
return u, err
}
u.Path = nu.Path
if nu.User != nil {
u.Username = nu.User.Username()
u.Password, _ = nu.User.Password()
}
if nu.Host == "~" {
hd, err := homedir.Dir()
if err != nil {
return u, err
}
u.Path = filepath.Join(hd, u.Path)
nu.Host = ""
}
u.Query = nu.Query()
if nu.Host != "" {
h, p, err := net.SplitHostPort(nu.Host)
if err == nil {
u.Host = h
u.Port = p
}
} else if runtime.GOOS == "windows" {
// only trim if this is a local path, e. g. file:///C:/Users/...
// (specified in https://blogs.msdn.microsoft.com/ie/2006/12/06/file-uris-in-windows/)
// In that case, u.Path will contain a leading slash.
// (correctly, as per RFC 3986, see https://github.com/golang/go/issues/6027)
u.Path = strings.TrimPrefix(u.Path, "/")
}
return u, nil
}
// String implements fmt.Stringer
func (u *URL) String() string {
if u.url == nil {
u.url = &url.URL{}
}
scheme := u.Scheme
if scheme == "" {
scheme = "file"
}
u.url.Scheme = fmt.Sprintf(
"%s-%s-%s+%s",
u.Crypto,
u.RCS,
u.Storage,
scheme,
)
if !strings.HasPrefix(u.Path, "/") && filepath.IsAbs(u.Path) {
u.url.Path = "/" + u.Path
} else {
u.url.Path = u.Path
}
if u.Username != "" {
u.url.User = url.UserPassword(u.Username, u.Password)
}
if u.Host != "" {
u.url.Host = u.Host
if u.Port != "" {
u.url.Host += ":" + u.Port
}
}
u.url.RawQuery = u.Query.Encode()
return u.url.String()
}
func (u *URL) parseScheme() error {
crypto, sync, store, scheme, err := splitBackends(u.url.Scheme)
if err != nil {
return err
}
u.Crypto = cryptoBackendFromName(crypto)
u.RCS = rcsBackendFromName(sync)
u.Storage = storageBackendFromName(store)
u.Scheme = scheme
return nil
}
// MarshalYAML implements yaml.Marshaler
func (u *URL) MarshalYAML() (interface{}, error) {
return u.String(), nil
}
// UnmarshalYAML implements yaml.Unmarshaler
func (u *URL) UnmarshalYAML(umf func(interface{}) error) error {
path := ""
if err := umf(&path); err != nil {
return err
}
um, err := ParseURL(path)
if err != nil {
return err
}
*u = *um
return nil
}
func splitBackends(in string) (string, string, string, string, error) {
p := strings.Split(in, "+")
if len(p) < 2 {
return "gpgcli", "gitcli", "fs", "file", nil
}
backends := p[0]
scheme := p[1]
p = strings.Split(backends, "-")
if len(p) < 1 {
return "", "", "", "", fmt.Errorf("invalid")
}
if len(p) < 3 {
return p[0], "", "", scheme, nil
}
return p[0], p[1], p[2], scheme, nil
}