TODO(jieyu): Use regex to parse and verify the reference.
| 31 | |
| 32 | // TODO(jieyu): Use regex to parse and verify the reference. |
| 33 | Try<ImageReference> parseImageReference(const string& _s) |
| 34 | { |
| 35 | ImageReference reference; |
| 36 | string s(_s); |
| 37 | |
| 38 | // Extract the digest. |
| 39 | if (strings::contains(s, "@")) { |
| 40 | vector<string> split = strings::split(s, "@"); |
| 41 | if (split.size() != 2) { |
| 42 | return Error("Multiple '@' symbols found"); |
| 43 | } |
| 44 | |
| 45 | s = split[0]; |
| 46 | reference.set_digest(split[1]); |
| 47 | } |
| 48 | |
| 49 | // Remove the tag. We need to watch out for a |
| 50 | // host:port registry, which also contains ':'. |
| 51 | if (strings::contains(s, ":")) { |
| 52 | vector<string> split = strings::split(s, ":"); |
| 53 | |
| 54 | // The tag must be the last component. If a slash is |
| 55 | // present there is a registry port and no tag. |
| 56 | if (!strings::contains(split.back(), "/")) { |
| 57 | reference.set_tag(split.back()); |
| 58 | split.pop_back(); |
| 59 | |
| 60 | s = strings::join(":", split); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // Extract the registry and repository. The first component can |
| 65 | // either be the registry, or the first part of the repository! |
| 66 | // We resolve this ambiguity using the same hacks used in the |
| 67 | // docker code ('.', ':', 'localhost' indicate a registry). |
| 68 | vector<string> split = strings::split(s, "/", 2); |
| 69 | |
| 70 | if (split.size() == 1) { |
| 71 | reference.set_repository(s); |
| 72 | } else if (strings::contains(split[0], ".") || |
| 73 | strings::contains(split[0], ":") || |
| 74 | split[0] == "localhost") { |
| 75 | reference.set_registry(split[0]); |
| 76 | reference.set_repository(split[1]); |
| 77 | } else { |
| 78 | reference.set_repository(s); |
| 79 | } |
| 80 | |
| 81 | return reference; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | ostream& operator<<(ostream& stream, const ImageReference& reference) |