| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package types |
| 6 |
| 7 import ( |
| 8 "net/url" |
| 9 "strings" |
| 10 |
| 11 "github.com/luci/luci-go/common/errors" |
| 12 "github.com/luci/luci-go/luci_config/common/cfgtypes" |
| 13 ) |
| 14 |
| 15 const logDogURLScheme = "logdog" |
| 16 |
| 17 // StreamAddr is a fully-qualified LogDog stream address. |
| 18 type StreamAddr struct { |
| 19 // Host is the LogDog host. |
| 20 Host string |
| 21 |
| 22 // Project is the LUCI project name that this log belongs to. |
| 23 Project cfgtypes.ProjectName |
| 24 |
| 25 // Path is the LogDog stream path. |
| 26 Path StreamPath |
| 27 } |
| 28 |
| 29 // URL returns a LogDog URL that represents this Stream. |
| 30 func (s *StreamAddr) URL() *url.URL { |
| 31 return &url.URL{ |
| 32 Scheme: logDogURLScheme, |
| 33 Host: s.Host, |
| 34 Path: strings.Join([]string{"", string(s.Project), string(s.Pa
th)}, "/"), |
| 35 } |
| 36 } |
| 37 |
| 38 // ParseURL parses a LogDog URL into a Stream. If the URL is malformed, or |
| 39 // if the host, project, or path is invalid, an error will be returned. |
| 40 // |
| 41 // A LogDog URL has the form: |
| 42 // logdog://<host>/<project>/<prefix>/+/<name> |
| 43 func ParseURL(v string) (*StreamAddr, error) { |
| 44 u, err := url.Parse(v) |
| 45 if err != nil { |
| 46 return nil, errors.Annotate(err).Reason("failed to parse URL").E
rr() |
| 47 } |
| 48 |
| 49 // Validate Scheme. |
| 50 if u.Scheme != logDogURLScheme { |
| 51 return nil, errors.Reason("URL scheme %(scheme)q is not "+logDog
URLScheme). |
| 52 D("scheme", u.Scheme). |
| 53 Err() |
| 54 } |
| 55 addr := StreamAddr{ |
| 56 Host: u.Host, |
| 57 } |
| 58 |
| 59 parts := strings.SplitN(u.Path, "/", 3) |
| 60 if len(parts) != 3 || len(parts[0]) != 0 { |
| 61 return nil, errors.Reason("URL path does not include both projec
t and path components: %(path)s"). |
| 62 D("path", u.Path). |
| 63 Err() |
| 64 } |
| 65 |
| 66 addr.Project, addr.Path = cfgtypes.ProjectName(parts[1]), StreamPath(par
ts[2]) |
| 67 if err := addr.Project.Validate(); err != nil { |
| 68 return nil, errors.Annotate(err).Reason("invalid project name: %
(project)q"). |
| 69 D("project", addr.Project). |
| 70 Err() |
| 71 } |
| 72 |
| 73 if err := addr.Path.Validate(); err != nil { |
| 74 return nil, errors.Annotate(err).Reason("invalid stream path: %(
path)q"). |
| 75 D("path", addr.Path). |
| 76 Err() |
| 77 } |
| 78 |
| 79 return &addr, nil |
| 80 } |
| OLD | NEW |