OLD | NEW |
1 // Copyright 2017 The LUCI Authors. All rights reserved. | 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package types | 5 package types |
6 | 6 |
7 import ( | 7 import ( |
8 "flag" | 8 "flag" |
9 "net/url" | 9 "net/url" |
10 "strings" | 10 "strings" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 } | 71 } |
72 | 72 |
73 // ParseURL parses a LogDog URL into a Stream. If the URL is malformed, or | 73 // ParseURL parses a LogDog URL into a Stream. If the URL is malformed, or |
74 // if the host, project, or path is invalid, an error will be returned. | 74 // if the host, project, or path is invalid, an error will be returned. |
75 // | 75 // |
76 // A LogDog URL has the form: | 76 // A LogDog URL has the form: |
77 // logdog://<host>/<project>/<prefix>/+/<name> | 77 // logdog://<host>/<project>/<prefix>/+/<name> |
78 func ParseURL(v string) (*StreamAddr, error) { | 78 func ParseURL(v string) (*StreamAddr, error) { |
79 u, err := url.Parse(v) | 79 u, err := url.Parse(v) |
80 if err != nil { | 80 if err != nil { |
81 » » return nil, errors.Annotate(err).Reason("failed to parse URL").E
rr() | 81 » » return nil, errors.Annotate(err, "failed to parse URL").Err() |
82 } | 82 } |
83 | 83 |
84 // Validate Scheme. | 84 // Validate Scheme. |
85 if u.Scheme != logDogURLScheme { | 85 if u.Scheme != logDogURLScheme { |
86 » » return nil, errors.Reason("URL scheme %(scheme)q is not "+logDog
URLScheme). | 86 » » return nil, errors.Reason("URL scheme %q is not %s", u.Scheme, l
ogDogURLScheme).Err() |
87 » » » D("scheme", u.Scheme). | |
88 » » » Err() | |
89 } | 87 } |
90 addr := StreamAddr{ | 88 addr := StreamAddr{ |
91 Host: u.Host, | 89 Host: u.Host, |
92 } | 90 } |
93 | 91 |
94 parts := strings.SplitN(u.Path, "/", 3) | 92 parts := strings.SplitN(u.Path, "/", 3) |
95 if len(parts) != 3 || len(parts[0]) != 0 { | 93 if len(parts) != 3 || len(parts[0]) != 0 { |
96 » » return nil, errors.Reason("URL path does not include both projec
t and path components: %(path)s"). | 94 » » return nil, errors.Reason("URL path does not include both projec
t and path components: %s", u.Path).Err() |
97 » » » D("path", u.Path). | |
98 » » » Err() | |
99 } | 95 } |
100 | 96 |
101 addr.Project, addr.Path = cfgtypes.ProjectName(parts[1]), StreamPath(par
ts[2]) | 97 addr.Project, addr.Path = cfgtypes.ProjectName(parts[1]), StreamPath(par
ts[2]) |
102 if err := addr.Project.Validate(); err != nil { | 98 if err := addr.Project.Validate(); err != nil { |
103 » » return nil, errors.Annotate(err).Reason("invalid project name: %
(project)q"). | 99 » » return nil, errors.Annotate(err, "invalid project name: %q", add
r.Project).Err() |
104 » » » D("project", addr.Project). | |
105 » » » Err() | |
106 } | 100 } |
107 | 101 |
108 if err := addr.Path.Validate(); err != nil { | 102 if err := addr.Path.Validate(); err != nil { |
109 » » return nil, errors.Annotate(err).Reason("invalid stream path: %(
path)q"). | 103 » » return nil, errors.Annotate(err, "invalid stream path: %q", addr
.Path).Err() |
110 » » » D("path", addr.Path). | |
111 » » » Err() | |
112 } | 104 } |
113 | 105 |
114 return &addr, nil | 106 return &addr, nil |
115 } | 107 } |
OLD | NEW |