Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(747)

Side by Side Diff: go/src/infra/tools/cipd/common/common.go

Issue 1129043003: cipd: Refactor client to make it more readable. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « go/src/infra/tools/cipd/common.go ('k') | go/src/infra/tools/cipd/common/common.infra_testing » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package cipd 5 /*
6 Package common defines structures and functions used by all other cipd/ packages .
7 */
8 package common
6 9
7 import ( 10 import (
8 "encoding/json"
9 "fmt" 11 "fmt"
10 "io"
11 "io/ioutil"
12 "regexp" 12 "regexp"
13 "strings" 13 "strings"
14
15 "infra/libs/build"
16 ) 14 )
17 15
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> 16 // packageNameRe is a regular expression for a package name: <word>/<word/<word>
25 // Package names must be lower case. 17 // Package names must be lower case.
26 var packageNameRe = regexp.MustCompile(`^([a-z0-9_\-]+/)*[a-z0-9_\-]+$`) 18 var packageNameRe = regexp.MustCompile(`^([a-z0-9_\-]+/)*[a-z0-9_\-]+$`)
27 19
28 // instanceTagKeyRe is a regular expression for a tag key. 20 // instanceTagKeyRe is a regular expression for a tag key.
29 var instanceTagKeyRe = regexp.MustCompile(`^[a-z0-9_\-]+$`) 21 var instanceTagKeyRe = regexp.MustCompile(`^[a-z0-9_\-]+$`)
30 22
31 // Name of the manifest file inside the package. 23 // Pin uniquely identifies an instance of some package.
32 const manifestName = packageServiceDir + "/manifest.json" 24 type Pin struct {
25 » PackageName string `json:"package"`
26 » InstanceID string `json:"instance_id"`
27 }
33 28
34 // Format version to write to the manifest file. 29 // String converts pin to a human readable string.
35 const manifestFormatVersion = "1" 30 func (pin Pin) String() string {
36 31 » return fmt.Sprintf("%s:%s", pin.PackageName, pin.InstanceID)
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 } 32 }
42 33
43 // ValidatePackageName returns error if a string doesn't look like a valid packa ge name. 34 // ValidatePackageName returns error if a string doesn't look like a valid packa ge name.
44 func ValidatePackageName(name string) error { 35 func ValidatePackageName(name string) error {
45 if !packageNameRe.MatchString(name) { 36 if !packageNameRe.MatchString(name) {
46 return fmt.Errorf("Invalid package name: %s", name) 37 return fmt.Errorf("Invalid package name: %s", name)
47 } 38 }
48 return nil 39 return nil
49 } 40 }
50 41
51 // ValidateInstanceID returns error if a string doesn't look like a valid packag e instance id. 42 // ValidateInstanceID returns error if a string doesn't look like a valid packag e instance id.
52 func ValidateInstanceID(s string) error { 43 func ValidateInstanceID(s string) error {
53 // Instance id is SHA1 hex digest currently. 44 // Instance id is SHA1 hex digest currently.
54 if len(s) != 40 { 45 if len(s) != 40 {
55 return fmt.Errorf("Not a valid package instance ID \"%s\": not 4 0 bytes", s) 46 return fmt.Errorf("Not a valid package instance ID \"%s\": not 4 0 bytes", s)
56 } 47 }
57 for _, c := range s { 48 for _, c := range s {
58 if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) { 49 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) 50 return fmt.Errorf("Not a valid package instance ID \"%s\ ": wrong char %c", s, c)
60 } 51 }
61 } 52 }
62 return nil 53 return nil
63 } 54 }
64 55
56 // ValidatePin returns error if package name of instance id of a pin are not val id.
57 func ValidatePin(pin Pin) error {
58 if err := ValidatePackageName(pin.PackageName); err != nil {
59 return err
60 }
61 if err := ValidateInstanceID(pin.InstanceID); err != nil {
62 return err
63 }
64 return nil
65 }
66
65 // ValidateInstanceTag returns error if a string doesn't look like a valid tag. 67 // ValidateInstanceTag returns error if a string doesn't look like a valid tag.
66 func ValidateInstanceTag(t string) error { 68 func ValidateInstanceTag(t string) error {
67 chunks := strings.SplitN(t, ":", 2) 69 chunks := strings.SplitN(t, ":", 2)
68 if len(chunks) != 2 { 70 if len(chunks) != 2 {
69 return fmt.Errorf("The string %q doesn't look like a tag (a key: value pair)", t) 71 return fmt.Errorf("The string %q doesn't look like a tag (a key: value pair)", t)
70 } 72 }
71 if len(t) > 400 { 73 if len(t) > 400 {
72 return fmt.Errorf("The tag is too long: %q", t) 74 return fmt.Errorf("The tag is too long: %q", t)
73 } 75 }
74 if !instanceTagKeyRe.MatchString(chunks[0]) { 76 if !instanceTagKeyRe.MatchString(chunks[0]) {
75 return fmt.Errorf("Invalid tag key in %q. Should be a lowercase word.", t) 77 return fmt.Errorf("Invalid tag key in %q. Should be a lowercase word.", t)
76 } 78 }
77 return nil 79 return nil
78 } 80 }
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 }
OLDNEW
« no previous file with comments | « go/src/infra/tools/cipd/common.go ('k') | go/src/infra/tools/cipd/common/common.infra_testing » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698