| OLD | NEW |
| 1 // Copyright 2014 The LUCI Authors. All rights reserved. | 1 // Copyright 2014 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 common defines structures and functions used by cipd/* packages. | 5 // Package common defines structures and functions used by cipd/* packages. |
| 6 package common | 6 package common |
| 7 | 7 |
| 8 import ( | 8 import ( |
| 9 "fmt" | 9 "fmt" |
| 10 "path" | 10 "path" |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 // 1) Instance ID (hash, e.g "1234deadbeef2234..."). | 116 // 1) Instance ID (hash, e.g "1234deadbeef2234..."). |
| 117 // 2) Package ref (e.g. "latest"). | 117 // 2) Package ref (e.g. "latest"). |
| 118 // 3) Instance tag (e.g. "git_revision:abcdef..."). | 118 // 3) Instance tag (e.g. "git_revision:abcdef..."). |
| 119 func ValidateInstanceVersion(v string) error { | 119 func ValidateInstanceVersion(v string) error { |
| 120 if ValidateInstanceID(v) == nil || ValidatePackageRef(v) == nil || Valid
ateInstanceTag(v) == nil { | 120 if ValidateInstanceID(v) == nil || ValidatePackageRef(v) == nil || Valid
ateInstanceTag(v) == nil { |
| 121 return nil | 121 return nil |
| 122 } | 122 } |
| 123 return fmt.Errorf("bad version (not an instance ID, a ref or a tag): %q"
, v) | 123 return fmt.Errorf("bad version (not an instance ID, a ref or a tag): %q"
, v) |
| 124 } | 124 } |
| 125 | 125 |
| 126 // ValidateRoot returns an error if the string can't be used as an ensure-file | 126 // ValidateSubdir returns an error if the string can't be used as an ensure-file |
| 127 // root. | 127 // subdir. |
| 128 func ValidateRoot(root string) error { | 128 func ValidateSubdir(subdir string) error { |
| 129 » if root == "" { // empty is fine | 129 » if subdir == "" { // empty is fine |
| 130 return nil | 130 return nil |
| 131 } | 131 } |
| 132 » if strings.Contains(root, "\\") { | 132 » if strings.Contains(subdir, "\\") { |
| 133 » » return fmt.Errorf(`bad root path: backslashes not allowed (use "
/"): %q`, root) | 133 » » return fmt.Errorf(`bad subdir: backslashes not allowed (use "/")
: %q`, subdir) |
| 134 } | 134 } |
| 135 » if strings.Contains(root, ":") { | 135 » if strings.Contains(subdir, ":") { |
| 136 » » return fmt.Errorf(`bad root path: colons are not allowed: %q`, r
oot) | 136 » » return fmt.Errorf(`bad subdir: colons are not allowed: %q`, subd
ir) |
| 137 } | 137 } |
| 138 » if cleaned := path.Clean(root); cleaned != root { | 138 » if cleaned := path.Clean(subdir); cleaned != subdir { |
| 139 » » return fmt.Errorf("bad root path: %q (should be %q)", root, clea
ned) | 139 » » return fmt.Errorf("bad subdir: %q (should be %q)", subdir, clean
ed) |
| 140 } | 140 } |
| 141 » if strings.HasPrefix(root, "./") || strings.HasPrefix(root, "../") || ro
ot == "." { | 141 » if strings.HasPrefix(subdir, "./") || strings.HasPrefix(subdir, "../") |
| subdir == "." { |
| 142 » » return fmt.Errorf(`bad root path: invalid ".": %q`, root) | 142 » » return fmt.Errorf(`bad subdir: invalid ".": %q`, subdir) |
| 143 } | 143 } |
| 144 » if strings.HasPrefix(root, "/") { | 144 » if strings.HasPrefix(subdir, "/") { |
| 145 » » return fmt.Errorf("bad root path: absolute paths not allowed: %q
", root) | 145 » » return fmt.Errorf("bad subdir: absolute paths not allowed: %q",
subdir) |
| 146 } | 146 } |
| 147 return nil | 147 return nil |
| 148 } | 148 } |
| 149 | 149 |
| 150 // GetInstanceTagKey returns key portion of the instance tag or empty string. | 150 // GetInstanceTagKey returns key portion of the instance tag or empty string. |
| 151 func GetInstanceTagKey(t string) string { | 151 func GetInstanceTagKey(t string) string { |
| 152 chunks := strings.SplitN(t, ":", 2) | 152 chunks := strings.SplitN(t, ":", 2) |
| 153 if len(chunks) != 2 { | 153 if len(chunks) != 2 { |
| 154 return "" | 154 return "" |
| 155 } | 155 } |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 225 for k := range m { | 225 for k := range m { |
| 226 pkgs = append(pkgs, k) | 226 pkgs = append(pkgs, k) |
| 227 } | 227 } |
| 228 pkgs.Sort() | 228 pkgs.Sort() |
| 229 for _, pkg := range pkgs { | 229 for _, pkg := range pkgs { |
| 230 s = append(s, Pin{pkg, m[pkg]}) | 230 s = append(s, Pin{pkg, m[pkg]}) |
| 231 } | 231 } |
| 232 return s | 232 return s |
| 233 } | 233 } |
| 234 | 234 |
| 235 // PinSliceByRoot is a simple mapping of root path to pin. | 235 // PinSliceBySubdir is a simple mapping of subdir to pin slice. |
| 236 type PinSliceByRoot map[string]PinSlice | 236 type PinSliceBySubdir map[string]PinSlice |
| 237 | 237 |
| 238 // Validate ensures that this doesn't contain any invalid | 238 // Validate ensures that this doesn't contain any invalid |
| 239 // root paths, duplicate packages within the same root, or invalid pins. | 239 // subdirs, duplicate packages within the same subdir, or invalid pins. |
| 240 func (p PinSliceByRoot) Validate() error { | 240 func (p PinSliceBySubdir) Validate() error { |
| 241 » for root, pkgs := range p { | 241 » for subdir, pkgs := range p { |
| 242 » » if err := ValidateRoot(root); err != nil { | 242 » » if err := ValidateSubdir(subdir); err != nil { |
| 243 return err | 243 return err |
| 244 } | 244 } |
| 245 if err := pkgs.Validate(); err != nil { | 245 if err := pkgs.Validate(); err != nil { |
| 246 » » » return fmt.Errorf("root %q: %s", root, err) | 246 » » » return fmt.Errorf("subdir %q: %s", subdir, err) |
| 247 } | 247 } |
| 248 } | 248 } |
| 249 return nil | 249 return nil |
| 250 } | 250 } |
| 251 | 251 |
| 252 // ToMap converts this to a PinMapByRoot | 252 // ToMap converts this to a PinMapBySubdir |
| 253 func (p PinSliceByRoot) ToMap() PinMapByRoot { | 253 func (p PinSliceBySubdir) ToMap() PinMapBySubdir { |
| 254 » ret := make(PinMapByRoot, len(p)) | 254 » ret := make(PinMapBySubdir, len(p)) |
| 255 » for root, pkgs := range p { | 255 » for subdir, pkgs := range p { |
| 256 » » ret[root] = pkgs.ToMap() | 256 » » ret[subdir] = pkgs.ToMap() |
| 257 } | 257 } |
| 258 return ret | 258 return ret |
| 259 } | 259 } |
| 260 | 260 |
| 261 // PinMapByRoot is a simple mapping of root -> package_name -> instanceID | 261 // PinMapBySubdir is a simple mapping of subdir -> package_name -> instanceID |
| 262 type PinMapByRoot map[string]PinMap | 262 type PinMapBySubdir map[string]PinMap |
| 263 | 263 |
| 264 // ToSlice converts this to a PinSliceByRoot | 264 // ToSlice converts this to a PinSliceBySubdir |
| 265 func (p PinMapByRoot) ToSlice() PinSliceByRoot { | 265 func (p PinMapBySubdir) ToSlice() PinSliceBySubdir { |
| 266 » ret := make(PinSliceByRoot, len(p)) | 266 » ret := make(PinSliceBySubdir, len(p)) |
| 267 » for root, pkgs := range p { | 267 » for subdir, pkgs := range p { |
| 268 » » ret[root] = pkgs.ToSlice() | 268 » » ret[subdir] = pkgs.ToSlice() |
| 269 } | 269 } |
| 270 return ret | 270 return ret |
| 271 } | 271 } |
| OLD | NEW |