-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupdate.rs
389 lines (325 loc) · 12.5 KB
/
update.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#[cfg(target_family = "unix")]
use std::os::unix::fs::PermissionsExt;
use std::{fs, io::IsTerminal, io::Write};
use anyhow::{anyhow, Context, Result};
use serde::{Deserialize, Serialize};
use crate::config_file::get_env_var;
/// ReleaseInfo stores information about a release.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ReleaseInfo {
#[serde(rename = "tag_name")]
pub version: String,
pub url: String,
pub published_at: chrono::DateTime<chrono::Utc>,
}
/// StateEntry stores information about a state.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct StateEntry {
pub checked_for_update_at: chrono::DateTime<chrono::Utc>,
pub latest_release: ReleaseInfo,
}
/// Check for updates to the cli.
///
/// Returns the latest version of the cli, or none if there is not a new
/// update or we shouldn't update.
pub async fn check_for_update(current_version: &str, force: bool) -> Result<Option<ReleaseInfo>> {
if !should_check_for_update() && !force {
return Ok(None);
}
let state_file = crate::config_file::state_file()?;
// Get our current state.
if std::path::Path::new(&state_file).exists() {
let state = get_state_entry(&state_file)?;
if !force {
let duration_since_last_check = chrono::Utc::now() - state.checked_for_update_at;
// TODO: After we make a mjor release of v1 we should bump this to like 6/12 hours.
if duration_since_last_check
< chrono::Duration::try_hours(1).ok_or_else(|| anyhow!("failed to create duration"))?
{
// If we've checked for updates in the last 1 hour, don't check again.
return Ok(None);
}
}
}
// Get the latest release.
let latest_release = get_latest_release_info().await?;
// Update our state.
set_state_entry(&state_file, chrono::Utc::now(), latest_release.clone())?;
if version_greater_then(&latest_release.version, current_version)? {
return Ok(Some(latest_release));
}
Ok(None)
}
/// If we should check for an update to the cli.
fn should_check_for_update() -> bool {
if !get_env_var("ZOO_NO_UPDATE_NOTIFIER").is_empty() {
return false;
}
!is_ci() && std::io::stdout().is_terminal() && std::io::stderr().is_terminal()
}
/// If we are running in a CI environment.
fn is_ci() -> bool {
!get_env_var("CI").is_empty() || // GitHub Actions, Travis CI, CircleCI, Cirrus CI, GitLab CI, CommandVeyor, CodeShip, dsari
!get_env_var("BUILD_NUMBER").is_empty() || // Jenkins, TeamCity
!get_env_var("RUN_ID").is_empty() // TaskCluster, dsari
}
/// Get the information about the latest version of the cli.
pub async fn get_latest_release_info() -> Result<ReleaseInfo> {
// If the user has a GITHUB_TOKEN environment variable, use it to get the latest release.
// This allows us to test this while the repo is still private.
// We might want to remove this in the future.
let github_token = crate::config_file::get_env_var("GITHUB_TOKEN");
let url = "https://api.github.com/repos/KittyCAD/cli/releases/latest";
let mut req = reqwest::Client::new().get(url);
// Set the user agent.
req = req.header("User-Agent", format!("zoo/{}", clap::crate_version!()));
if !github_token.is_empty() {
req = req.bearer_auth(github_token);
}
let resp = req.send().await?;
let text = resp.text().await?;
let latest_release: ReleaseInfo = match serde_json::from_str(&text) {
Ok(release_info) => release_info,
Err(err) => {
return Err(anyhow!(
"Failed to parse response from GitHub: {}\ntext:\n{}",
err.to_string(),
text
));
}
};
Ok(latest_release)
}
/// Get an entry in the state file.
fn get_state_entry(filepath: &str) -> Result<StateEntry> {
let file_content = fs::read_to_string(filepath)?;
let state_entry: StateEntry = toml::from_str(&file_content)?;
Ok(state_entry)
}
/// Set an entry in the state file.
fn set_state_entry(filename: &str, t: chrono::DateTime<chrono::Utc>, r: ReleaseInfo) -> Result<()> {
let data = StateEntry {
checked_for_update_at: t,
latest_release: r,
};
let content = toml::to_string(&data)?;
// Make sure we have a parent directory.
let path = std::path::Path::new(&filename);
let parent = path.parent().unwrap();
fs::create_dir_all(parent).with_context(|| format!("failed to create directory {}", parent.display()))?;
// Write the file.
fs::write(filename, content).with_context(|| format!("failed to write file {filename}"))?;
Ok(())
}
/// Return is one version is greater than another.
pub fn version_greater_then(v: &str, w: &str) -> Result<bool> {
match version_compare::compare(v, w) {
Ok(cmp) => Ok(cmp == version_compare::Cmp::Gt),
Err(_) => Err(anyhow!("failed to compare versions: {} {}", v, w)),
}
}
/// Returns if the release was published in the last 24 hours.
pub fn is_recent_release(published_at: chrono::DateTime<chrono::Utc>) -> bool {
let duration = chrono::Utc::now() - published_at;
duration.num_days() < 1
}
/// Check whether the `zoo` binary was found under the Homebrew prefix.
pub fn is_under_homebrew() -> Result<bool> {
let binary_path = std::env::current_exe()?;
let binary_path_str = binary_path.to_str().unwrap();
match std::process::Command::new("brew").args(vec!["--prefix"]).output() {
Ok(output) => {
let homebrew_prefix = String::from_utf8(output.stdout)?;
let brew_bin_prefix = std::path::Path::new(homebrew_prefix.trim()).join("bin");
Ok(binary_path_str.starts_with(brew_bin_prefix.to_str().unwrap()))
}
Err(e) => {
// brew wasn't found, so cli can't be running under homebrew.
if e.kind() == std::io::ErrorKind::NotFound {
Ok(false)
} else {
Err(e.into())
}
}
}
}
/// Takes a version string and returns the URL to download the latest release.
fn get_exe_download_url(version: &str) -> String {
// Make sure the version starts with a v.
let version = if !version.starts_with('v') {
format!("v{version}")
} else {
version.to_string()
};
format!(
"https://github.com/KittyCAD/cli/releases/download/{}/zoo-{}",
version,
crate::built_info::TARGET
)
}
/// Takes a version string and downloads the latest binary to a temp file.
/// This also checks the SHA256 hash of the file.
pub async fn download_binary_to_temp_file(version: &str) -> Result<String> {
let temp_dir = std::env::temp_dir();
let temp_file = temp_dir.join("zoo");
let url = get_exe_download_url(version);
// Get the contents of the binary.
let resp = reqwest::get(&url).await?;
let bin_body = resp.bytes().await?;
// Get the contents of the sha256sum.
let resp = reqwest::get(&format!("{url}.sha256")).await?;
let sha256_body = resp.text().await?;
let sha256_parts = sha256_body.split(' ').collect::<Vec<&str>>();
let sha256_hash = sha256_parts[0];
// Verify the sha256 hash of the binary.
let bin_hash = sha256_digest(bin_body.as_ref())?;
if bin_hash != sha256_hash {
anyhow::bail!(
"SHA256 hash mismatch: local ({}) != remote ({:?})",
bin_hash,
sha256_hash
);
}
// Write the body to the file.
let mut f = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(&temp_file)?;
f.write_all(&bin_body)?;
f.flush()?;
let temp_file_path = temp_file
.to_str()
.ok_or_else(|| anyhow::anyhow!("failed to convert temp file path to string"))?;
// Set the file permissions to be correct, executable, write (so we can update).
// TODO: make this also work on windows.
#[cfg(target_family = "unix")]
std::fs::set_permissions(temp_file_path, std::fs::Permissions::from_mode(0o755))?;
Ok(temp_file_path.to_string())
}
/// Calculates the SHA256 hash of a reader.
fn sha256_digest<R: std::io::Read>(mut reader: R) -> Result<String> {
let mut context = ring::digest::Context::new(&ring::digest::SHA256);
let mut buffer = [0; 1024];
loop {
let count = reader.read(&mut buffer)?;
if count == 0 {
break;
}
context.update(&buffer[..count]);
}
let digest = context.finish();
Ok(data_encoding::HEXLOWER.encode(digest.as_ref()))
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
#[tokio::test]
async fn test_download_binary_to_temp_file() {
if crate::update::is_ci() {
return;
}
let file = super::download_binary_to_temp_file("v0.2.42").await.unwrap();
assert_eq!(
file,
format!("{}/zoo", std::env::temp_dir().to_str().unwrap().trim_end_matches('/'))
);
}
#[test]
fn test_get_exe_download_url() {
let url = super::get_exe_download_url("0.1.0");
assert_eq!(
url,
format!(
"https://github.com/KittyCAD/cli/releases/download/v0.1.0/zoo-{}",
crate::built_info::TARGET
)
);
let url = super::get_exe_download_url("v0.2.0");
assert_eq!(
url,
format!(
"https://github.com/KittyCAD/cli/releases/download/v0.2.0/zoo-{}",
crate::built_info::TARGET
)
);
}
#[tokio::test]
#[serial_test::serial]
async fn test_check_for_update() {
let result = super::check_for_update("0.2.41", true).await.unwrap();
assert_eq!(result.is_some(), true);
let latest_release = result.unwrap();
let gh_latest_release = super::get_latest_release_info().await.unwrap();
assert_eq!(latest_release.version, gh_latest_release.version);
}
pub struct TestItem {
name: String,
current_version: String,
latest_version: String,
want_result: bool,
}
#[test]
fn test_update() {
let tests: Vec<TestItem> = vec![
TestItem {
name: "latest is newer".to_string(),
current_version: "v0.0.1".to_string(),
latest_version: "v1.0.0".to_string(),
want_result: true,
},
TestItem {
name: "current is prerelease".to_string(),
current_version: "v1.0.0-pre.1".to_string(),
latest_version: "v1.0.0".to_string(),
want_result: true,
},
TestItem {
name: "current is built from source".to_string(),
current_version: "v1.2.3-123-gdeadbeef".to_string(),
latest_version: "v1.2.3".to_string(),
want_result: false,
},
TestItem {
name: "current is built from source after a prerelease".to_string(),
current_version: "v1.2.3-rc.1-123-gdeadbeef".to_string(),
latest_version: "v1.2.3".to_string(),
want_result: true,
},
TestItem {
name: "latest is newer than version build from source".to_string(),
current_version: "v1.2.3-123-gdeadbeef".to_string(),
latest_version: "v1.2.4".to_string(),
want_result: true,
},
TestItem {
name: "latest is current".to_string(),
current_version: "v1.2.5".to_string(),
latest_version: "v1.2.5".to_string(),
want_result: false,
},
TestItem {
name: "latest is older".to_string(),
current_version: "v0.10.0-pre.1".to_string(),
latest_version: "v0.9.0".to_string(),
want_result: false,
},
TestItem {
name: "latest is a pre-release".to_string(),
current_version: "v0.1.0".to_string(),
latest_version: "v0.2.0-pre.1".to_string(),
want_result: true,
},
TestItem {
name: "latest is a pre-release (current without v)".to_string(),
current_version: "0.1.0".to_string(),
latest_version: "v0.2.0-pre.1".to_string(),
want_result: true,
},
];
for t in tests {
let result = crate::update::version_greater_then(&t.latest_version, &t.current_version).unwrap();
assert_eq!(result, t.want_result, "test {} failed", t.name);
}
}
}