Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 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 version provides a way for CIPD packaged Go binaries to discover | 5 // Package version provides a way for CIPD packaged Go binaries to discover |
| 6 // their current package instance ID. | 6 // their current package instance ID. |
| 7 // | 7 // |
| 8 // It's safe to link this library into arbitrary executables. It is small and | 8 // It's safe to link this library into arbitrary executables. It is small and |
| 9 // doesn't pull in rest of CIPD client code. | 9 // doesn't pull in rest of CIPD client code. |
| 10 package version | 10 package version |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 return "" | 91 return "" |
| 92 } | 92 } |
| 93 // Must have at least ".cipd/pkgs/<name>/<id>/...". | 93 // Must have at least ".cipd/pkgs/<name>/<id>/...". |
| 94 if len(chunks)-i <= 4 { | 94 if len(chunks)-i <= 4 { |
| 95 return "" | 95 return "" |
| 96 } | 96 } |
| 97 // Cut out .cipd/pkgs/<name>/<id> to get A/b/c/d. | 97 // Cut out .cipd/pkgs/<name>/<id> to get A/b/c/d. |
| 98 return strings.Join(append(chunks[:i], chunks[i+4:]...), string(filepath .Separator)) | 98 return strings.Join(append(chunks[:i], chunks[i+4:]...), string(filepath .Separator)) |
| 99 } | 99 } |
| 100 | 100 |
| 101 // GetVersionFile returns the path to the version file corresponding to the | |
| 102 // provided exe. This isn't typically needed, but can be useful for debugging. | |
| 103 func GetVersionFile(exePath string) string { | |
| 104 // <root>/.versions/exename.cipd_version | |
| 105 return filepath.Join(filepath.Dir(exePath), ".versions", filepath.Base(e xePath)+".cipd_version") | |
| 106 } | |
| 107 | |
| 101 func getCurrentVersion(exePath string) (Info, error) { | 108 func getCurrentVersion(exePath string) (Info, error) { |
| 102 » // <root>/.versions/exename.cipd_version | 109 » if vf, _ := readVersionFile(GetVersionFile(exePath)); vf.InstanceID != " " { |
| 103 » p := filepath.Join(filepath.Dir(exePath), ".versions", filepath.Base(exe Path)+".cipd_version") | |
| 104 » if vf, _ := readVersionFile(p); vf.InstanceID != "" { | |
| 105 return vf, nil | 110 return vf, nil |
| 106 } | 111 } |
| 107 // <root>/exename.cipd_version | 112 // <root>/exename.cipd_version |
| 108 return readVersionFile(exePath + ".cipd_version") | 113 return readVersionFile(exePath + ".cipd_version") |
|
Vadim Sh.
2016/11/27 01:25:55
note that there's a fallback here. It's where vers
iannucci
2016/11/29 01:46:20
That's what I figured.
| |
| 109 } | 114 } |
| 110 | 115 |
| 111 // readVersionFile returns parsed version file. Returns empty struct and nil if | 116 // readVersionFile returns parsed version file. Returns empty struct and nil if |
| 112 // it is missing, error if it can't be read. | 117 // it is missing, error if it can't be read. |
| 113 func readVersionFile(path string) (Info, error) { | 118 func readVersionFile(path string) (Info, error) { |
| 114 f, err := os.Open(path) | 119 f, err := os.Open(path) |
| 115 if err != nil { | 120 if err != nil { |
| 116 if os.IsNotExist(err) { | 121 if os.IsNotExist(err) { |
| 117 err = nil | 122 err = nil |
| 118 } | 123 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 141 // Version file can also be changed. Remember the version of the started | 146 // Version file can also be changed. Remember the version of the started |
| 142 // executable. | 147 // executable. |
| 143 if initialExePathErr == nil { | 148 if initialExePathErr == nil { |
| 144 // Don't use GetCurrentVersion since we specifically do not want to use | 149 // Don't use GetCurrentVersion since we specifically do not want to use |
| 145 // the original symlink. | 150 // the original symlink. |
| 146 startupVersionFile, startupVersionFileErr = getCurrentVersion(in itialExePath) | 151 startupVersionFile, startupVersionFileErr = getCurrentVersion(in itialExePath) |
| 147 } else { | 152 } else { |
| 148 startupVersionFileErr = initialExePathErr | 153 startupVersionFileErr = initialExePathErr |
| 149 } | 154 } |
| 150 } | 155 } |
| OLD | NEW |