OLD | NEW |
1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 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 main | 5 package main |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "os/user" | 9 "os/user" |
10 "strings" | 10 "strings" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
44 switch vs := cp.VersionScheme; vs { | 44 switch vs := cp.VersionScheme; vs { |
45 case deploy.Deployment_CloudProject_DEFAULT: | 45 case deploy.Deployment_CloudProject_DEFAULT: |
46 cpv := structuredCloudProjectVersion{ | 46 cpv := structuredCloudProjectVersion{ |
47 minorSourceVersion: cloudVersionStringNormalize(src.Mino
rVersion), | 47 minorSourceVersion: cloudVersionStringNormalize(src.Mino
rVersion), |
48 majorSourceVersion: cloudVersionStringNormalize(src.Majo
rVersion), | 48 majorSourceVersion: cloudVersionStringNormalize(src.Majo
rVersion), |
49 } | 49 } |
50 | 50 |
51 if src.sg.Tainted { | 51 if src.sg.Tainted { |
52 username, err := b.getCurrentUser() | 52 username, err := b.getCurrentUser() |
53 if err != nil { | 53 if err != nil { |
54 » » » » return nil, errors.Annotate(err).Reason("could n
ot get tained user").Err() | 54 » » » » return nil, errors.Annotate(err, "could not get
tained user").Err() |
55 } | 55 } |
56 cpv.taintedUser = cloudVersionStringNormalize(username) | 56 cpv.taintedUser = cloudVersionStringNormalize(username) |
57 } | 57 } |
58 return &cpv, nil | 58 return &cpv, nil |
59 | 59 |
60 default: | 60 default: |
61 » » return nil, errors.Reason("unknown version scheme %(scheme)v").D
("scheme", vs).Err() | 61 » » return nil, errors.Reason("unknown version scheme %v", vs).Err() |
62 } | 62 } |
63 } | 63 } |
64 | 64 |
65 func (b *cloudProjectVersionBuilder) getCurrentUser() (string, error) { | 65 func (b *cloudProjectVersionBuilder) getCurrentUser() (string, error) { |
66 if b.currentUser != nil { | 66 if b.currentUser != nil { |
67 return b.currentUser() | 67 return b.currentUser() |
68 } | 68 } |
69 | 69 |
70 // Default "currentUser" function uses "os.User". | 70 // Default "currentUser" function uses "os.User". |
71 user, err := user.Current() | 71 user, err := user.Current() |
72 if err != nil { | 72 if err != nil { |
73 » » return "", errors.Annotate(err).Reason("could not get tained use
r").Err() | 73 » » return "", errors.Annotate(err, "could not get tained user").Err
() |
74 } | 74 } |
75 return user.Username, nil | 75 return user.Username, nil |
76 } | 76 } |
77 | 77 |
78 func parseCloudProjectVersion(vs deploy.Deployment_CloudProject_VersionScheme, v
string) ( | 78 func parseCloudProjectVersion(vs deploy.Deployment_CloudProject_VersionScheme, v
string) ( |
79 cloudProjectVersion, error) { | 79 cloudProjectVersion, error) { |
80 var cpv structuredCloudProjectVersion | 80 var cpv structuredCloudProjectVersion |
81 switch vs { | 81 switch vs { |
82 case deploy.Deployment_CloudProject_DEFAULT: | 82 case deploy.Deployment_CloudProject_DEFAULT: |
83 parts := strings.Split(v, "-") | 83 parts := strings.Split(v, "-") |
84 | 84 |
85 switch len(parts) { | 85 switch len(parts) { |
86 case 4: | 86 case 4: |
87 // <min>-<maj>-tainted-<user> | 87 // <min>-<maj>-tainted-<user> |
88 cpv.taintedUser = parts[3] | 88 cpv.taintedUser = parts[3] |
89 fallthrough | 89 fallthrough |
90 | 90 |
91 case 2: | 91 case 2: |
92 // <min>-<maj> | 92 // <min>-<maj> |
93 cpv.minorSourceVersion = parts[0] | 93 cpv.minorSourceVersion = parts[0] |
94 cpv.majorSourceVersion = parts[1] | 94 cpv.majorSourceVersion = parts[1] |
95 return &cpv, nil | 95 return &cpv, nil |
96 | 96 |
97 default: | 97 default: |
98 » » » return nil, errors.Reason("bad version %(version)q for s
cheme %(scheme)T"). | 98 » » » return nil, errors.Reason("bad version %q for scheme %T"
, v, vs).Err() |
99 » » » » D("version", v).D("scheme", vs).Err() | |
100 } | 99 } |
101 | 100 |
102 default: | 101 default: |
103 » » return nil, errors.Reason("unknown version scheme %(scheme)T").D
("scheme", vs).Err() | 102 » » return nil, errors.Reason("unknown version scheme %T", vs).Err() |
104 } | 103 } |
105 } | 104 } |
106 | 105 |
107 func (v *structuredCloudProjectVersion) String() string { | 106 func (v *structuredCloudProjectVersion) String() string { |
108 var partsArray [5]string | 107 var partsArray [5]string |
109 parts := partsArray[:0] | 108 parts := partsArray[:0] |
110 | 109 |
111 parts = append(parts, []string{v.minorSourceVersion, v.majorSourceVersio
n}...) | 110 parts = append(parts, []string{v.minorSourceVersion, v.majorSourceVersio
n}...) |
112 if tu := v.taintedUser; tu != "" { | 111 if tu := v.taintedUser; tu != "" { |
113 parts = append(parts, []string{"tainted", tu}...) | 112 parts = append(parts, []string{"tainted", tu}...) |
(...skipping 19 matching lines...) Expand all Loading... |
133 func cloudVersionStringNormalize(v string) string { | 132 func cloudVersionStringNormalize(v string) string { |
134 return strings.Map(func(r rune) rune { | 133 return strings.Map(func(r rune) rune { |
135 if (r >= 'a' && r <= 'z') || | 134 if (r >= 'a' && r <= 'z') || |
136 (r >= 'A' && r <= 'Z') || | 135 (r >= 'A' && r <= 'Z') || |
137 (r >= '0' && r <= '9') { | 136 (r >= '0' && r <= '9') { |
138 return r | 137 return r |
139 } | 138 } |
140 return '_' | 139 return '_' |
141 }, v) | 140 }, v) |
142 } | 141 } |
OLD | NEW |