| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package cipd | |
| 6 | |
| 7 import ( | |
| 8 "encoding/json" | |
| 9 "fmt" | |
| 10 "io" | |
| 11 "io/ioutil" | |
| 12 "regexp" | |
| 13 "strings" | |
| 14 | |
| 15 "infra/libs/build" | |
| 16 ) | |
| 17 | |
| 18 // Name of the directory inside the package reserved for cipd stuff. | |
| 19 const packageServiceDir = ".cipdpkg" | |
| 20 | |
| 21 // Name of the directory inside an installation root reserved for cipd stuff. | |
| 22 const siteServiceDir = ".cipd" | |
| 23 | |
| 24 // packageNameRe is a regular expression for a package name: <word>/<word/<word> | |
| 25 // Package names must be lower case. | |
| 26 var packageNameRe = regexp.MustCompile(`^([a-z0-9_\-]+/)*[a-z0-9_\-]+$`) | |
| 27 | |
| 28 // instanceTagKeyRe is a regular expression for a tag key. | |
| 29 var instanceTagKeyRe = regexp.MustCompile(`^[a-z0-9_\-]+$`) | |
| 30 | |
| 31 // Name of the manifest file inside the package. | |
| 32 const manifestName = packageServiceDir + "/manifest.json" | |
| 33 | |
| 34 // Format version to write to the manifest file. | |
| 35 const manifestFormatVersion = "1" | |
| 36 | |
| 37 // Manifest defines structure of manifest.json file. | |
| 38 type Manifest struct { | |
| 39 FormatVersion string `json:"format_version"` | |
| 40 PackageName string `json:"package_name"` | |
| 41 } | |
| 42 | |
| 43 // ValidatePackageName returns error if a string doesn't look like a valid packa
ge name. | |
| 44 func ValidatePackageName(name string) error { | |
| 45 if !packageNameRe.MatchString(name) { | |
| 46 return fmt.Errorf("Invalid package name: %s", name) | |
| 47 } | |
| 48 return nil | |
| 49 } | |
| 50 | |
| 51 // ValidateInstanceID returns error if a string doesn't look like a valid packag
e instance id. | |
| 52 func ValidateInstanceID(s string) error { | |
| 53 // Instance id is SHA1 hex digest currently. | |
| 54 if len(s) != 40 { | |
| 55 return fmt.Errorf("Not a valid package instance ID \"%s\": not 4
0 bytes", s) | |
| 56 } | |
| 57 for _, c := range s { | |
| 58 if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) { | |
| 59 return fmt.Errorf("Not a valid package instance ID \"%s\
": wrong char %c", s, c) | |
| 60 } | |
| 61 } | |
| 62 return nil | |
| 63 } | |
| 64 | |
| 65 // ValidateInstanceTag returns error if a string doesn't look like a valid tag. | |
| 66 func ValidateInstanceTag(t string) error { | |
| 67 chunks := strings.SplitN(t, ":", 2) | |
| 68 if len(chunks) != 2 { | |
| 69 return fmt.Errorf("The string %q doesn't look like a tag (a key:
value pair)", t) | |
| 70 } | |
| 71 if len(t) > 400 { | |
| 72 return fmt.Errorf("The tag is too long: %q", t) | |
| 73 } | |
| 74 if !instanceTagKeyRe.MatchString(chunks[0]) { | |
| 75 return fmt.Errorf("Invalid tag key in %q. Should be a lowercase
word.", t) | |
| 76 } | |
| 77 return nil | |
| 78 } | |
| 79 | |
| 80 // DefaultServiceURL returns URL to a backend to use by default. | |
| 81 func DefaultServiceURL() string { | |
| 82 if build.ReleaseBuild { | |
| 83 return "https://chrome-infra-packages.appspot.com" | |
| 84 } | |
| 85 return "https://chrome-infra-packages-dev.appspot.com" | |
| 86 } | |
| 87 | |
| 88 // readManifest reads and decodes manifest JSON from io.Reader. | |
| 89 func readManifest(r io.Reader) (Manifest, error) { | |
| 90 blob, err := ioutil.ReadAll(r) | |
| 91 if err != nil { | |
| 92 return Manifest{}, err | |
| 93 } | |
| 94 manifest := Manifest{} | |
| 95 err = json.Unmarshal(blob, &manifest) | |
| 96 if err != nil { | |
| 97 return Manifest{}, err | |
| 98 } | |
| 99 return manifest, nil | |
| 100 } | |
| 101 | |
| 102 // writeManifest encodes and writes manifest JSON to io.Writer. | |
| 103 func writeManifest(m *Manifest, w io.Writer) error { | |
| 104 data, err := json.MarshalIndent(m, "", " ") | |
| 105 if err != nil { | |
| 106 return err | |
| 107 } | |
| 108 _, err = w.Write(data) | |
| 109 return err | |
| 110 } | |
| 111 | |
| 112 // userAgent returns user agent string to send with each request. | |
| 113 func userAgent() string { | |
| 114 if build.ReleaseBuild { | |
| 115 return "cipd 1.0 release" | |
| 116 } | |
| 117 return "cipd 1.0 testing" | |
| 118 } | |
| OLD | NEW |