OLD | NEW |
1 package tools | 1 package tools |
2 | 2 |
3 import ( | 3 import ( |
| 4 "launchpad.net/juju-core/log" |
4 "launchpad.net/juju-core/state" | 5 "launchpad.net/juju-core/state" |
5 "launchpad.net/juju-core/utils/set" | 6 "launchpad.net/juju-core/utils/set" |
6 "launchpad.net/juju-core/version" | 7 "launchpad.net/juju-core/version" |
7 "strings" | 8 "strings" |
8 ) | 9 ) |
9 | 10 |
10 // List holds tools available in an environment. The order of tools within | 11 // List holds tools available in an environment. The order of tools within |
11 // a List is not significant. | 12 // a List is not significant. |
12 type List []*state.Tools | 13 type List []*state.Tools |
13 | 14 |
(...skipping 23 matching lines...) Expand all Loading... |
37 // collect calls f on all values in src and returns an alphabetically | 38 // collect calls f on all values in src and returns an alphabetically |
38 // ordered list of the returned results without duplicates. | 39 // ordered list of the returned results without duplicates. |
39 func (src List) collect(f func(*state.Tools) string) []string { | 40 func (src List) collect(f func(*state.Tools) string) []string { |
40 var seen set.Strings | 41 var seen set.Strings |
41 for _, tools := range src { | 42 for _, tools := range src { |
42 seen.Add(f(tools)) | 43 seen.Add(f(tools)) |
43 } | 44 } |
44 return seen.SortedValues() | 45 return seen.SortedValues() |
45 } | 46 } |
46 | 47 |
47 // Newest returns the tools in src with the greatest version. | 48 // URLs returns download URLs for the tools in src, keyed by binary version. |
48 func (src List) Newest() List { | 49 func (src List) URLs() map[version.Binary]string { |
| 50 » result := map[version.Binary]string{} |
| 51 » for _, tools := range src { |
| 52 » » result[tools.Binary] = tools.URL |
| 53 » } |
| 54 » return result |
| 55 } |
| 56 |
| 57 // Newest returns the greatest version in src, and the tools with that version. |
| 58 func (src List) Newest() (version.Number, List) { |
49 var result List | 59 var result List |
50 var best version.Number | 60 var best version.Number |
51 for _, tools := range src { | 61 for _, tools := range src { |
52 if best.Less(tools.Number) { | 62 if best.Less(tools.Number) { |
53 // Found new best number; reset result list. | 63 // Found new best number; reset result list. |
54 best = tools.Number | 64 best = tools.Number |
55 result = append(result[:0], tools) | 65 result = append(result[:0], tools) |
56 } else if tools.Number == best { | 66 } else if tools.Number == best { |
57 result = append(result, tools) | 67 result = append(result, tools) |
58 } | 68 } |
59 } | 69 } |
60 » return result | 70 » return best, result |
61 } | 71 } |
62 | 72 |
63 // Difference returns the tools in src that are not in excluded. | 73 // Difference returns the tools in src that are not in excluded. |
64 func (src List) Exclude(excluded List) List { | 74 func (src List) Exclude(excluded List) List { |
65 ignore := make(map[version.Binary]bool, len(excluded)) | 75 ignore := make(map[version.Binary]bool, len(excluded)) |
66 for _, tool := range excluded { | 76 for _, tool := range excluded { |
67 ignore[tool.Binary] = true | 77 ignore[tool.Binary] = true |
68 } | 78 } |
69 var result List | 79 var result List |
70 for _, tool := range src { | 80 for _, tool := range src { |
71 if !ignore[tool.Binary] { | 81 if !ignore[tool.Binary] { |
72 result = append(result, tool) | 82 result = append(result, tool) |
73 } | 83 } |
74 } | 84 } |
75 return result | 85 return result |
76 } | 86 } |
77 | 87 |
78 // Match returns a List, derived from src, containing only those tools that | 88 // Match returns a List, derived from src, containing only those tools that |
79 // match the supplied Filter. If no tools match, it returns ErrNoMatches. | 89 // match the supplied Filter. If no tools match, it returns ErrNoMatches. |
80 func (src List) Match(f Filter) (List, error) { | 90 func (src List) Match(f Filter) (List, error) { |
81 var result List | 91 var result List |
82 for _, tools := range src { | 92 for _, tools := range src { |
83 if f.match(tools) { | 93 if f.match(tools) { |
84 result = append(result, tools) | 94 result = append(result, tools) |
85 } | 95 } |
86 } | 96 } |
87 if len(result) == 0 { | 97 if len(result) == 0 { |
| 98 log.Errorf("environs/tools: cannot match %#v", f) |
88 return nil, ErrNoMatches | 99 return nil, ErrNoMatches |
89 } | 100 } |
90 return result, nil | 101 return result, nil |
91 } | 102 } |
92 | 103 |
93 // Filter holds criteria for choosing tools. | 104 // Filter holds criteria for choosing tools. |
94 type Filter struct { | 105 type Filter struct { |
95 | 106 |
96 // Release, if true, causes the filter to match only tools with a | 107 // Release, if true, causes the filter to match only tools with a |
97 // non-development version number. | 108 // non-development version number. |
(...skipping 10 matching lines...) Expand all Loading... |
108 // Arch, if not empty, causes the filter to match only tools with | 119 // Arch, if not empty, causes the filter to match only tools with |
109 // that architecture. | 120 // that architecture. |
110 Arch string | 121 Arch string |
111 } | 122 } |
112 | 123 |
113 // match returns true if the supplied tools match f. | 124 // match returns true if the supplied tools match f. |
114 func (f Filter) match(tools *state.Tools) bool { | 125 func (f Filter) match(tools *state.Tools) bool { |
115 if f.Released && tools.IsDev() { | 126 if f.Released && tools.IsDev() { |
116 return false | 127 return false |
117 } | 128 } |
118 » if f.Number != (version.Number{}) && tools.Number != f.Number { | 129 » if f.Number != version.Zero && tools.Number != f.Number { |
119 return false | 130 return false |
120 } | 131 } |
121 if f.Series != "" && tools.Series != f.Series { | 132 if f.Series != "" && tools.Series != f.Series { |
122 return false | 133 return false |
123 } | 134 } |
124 if f.Arch != "" && tools.Arch != f.Arch { | 135 if f.Arch != "" && tools.Arch != f.Arch { |
125 return false | 136 return false |
126 } | 137 } |
127 return true | 138 return true |
128 } | 139 } |
OLD | NEW |