forked from djui/await
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource.go
More file actions
71 lines (63 loc) · 1.54 KB
/
resource.go
File metadata and controls
71 lines (63 loc) · 1.54 KB
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
package main
import (
"context"
"fmt"
"net/url"
)
type resource interface {
fmt.Stringer
Await(context.Context) error
}
type unavailabilityError struct {
Reason error
}
// Error implements the error interface.
func (e *unavailabilityError) Error() string {
return e.Reason.Error()
}
func parseResources(urlArgs []string) ([]resource, error) {
var resources []resource
for _, urlArg := range urlArgs {
// Leveraging the fact the Go's URL parser matches e.g. `curl -s
// http://example.com` as url.Path instead of throwing an error.
u, err := url.Parse(urlArg)
if err != nil {
return nil, err
}
res, err := identifyResource(*u)
if err != nil {
return nil, err
}
resources = append(resources, res)
}
return resources, nil
}
func identifyResource(u url.URL) (resource, error) {
switch u.Scheme {
case "http", "https":
return &httpResource{u}, nil
case "ws", "wss":
return &websocketResource{u}, nil
case "tcp", "tcp4", "tcp6":
return &tcpResource{u}, nil
case "file":
return &fileResource{u}, nil
case "postgres":
return &postgresqlResource{u}, nil
case "mysql":
return &mysqlResource{u}, nil
case "amqp", "amqps":
return &amqpResource{u}, nil
case "":
return &commandResource{u}, nil
default:
return nil, fmt.Errorf("unsupported resource scheme: %v", u.Scheme)
}
}
func parseFragment(fragment string) url.Values {
// Skip encountered decoding errors on invalid format for now
v, _ := url.ParseQuery(fragment)
// Maintain backwards-compatibility for the case that key is empty
delete(v, "")
return v
}