-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
link.rs
573 lines (495 loc) · 18 KB
/
link.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
use std::{
fs,
fs::{File, OpenOptions},
io,
io::{BufRead, Write},
};
use anyhow::{anyhow, Context, Result};
#[cfg(not(test))]
use console::Style;
#[cfg(not(test))]
use dialoguer::FuzzySelect;
use dialoguer::{theme::ColorfulTheme, Confirm};
use dirs_next::home_dir;
#[cfg(test)]
use rand::Rng;
use turborepo_api_client::{APIClient, CachingStatus, Space, Team};
#[cfg(not(test))]
use crate::ui::CYAN;
use crate::{
cli::LinkTarget,
commands::CommandBase,
config::{SpacesJson, TurboJson},
ui::{BOLD, GREY, UNDERLINE},
};
#[derive(Clone)]
pub(crate) enum SelectedTeam<'a> {
User,
Team(&'a Team),
}
#[derive(Clone)]
pub(crate) enum SelectedSpace<'a> {
Space(&'a Space),
}
pub(crate) const REMOTE_CACHING_INFO: &str = " Remote Caching shares your cached Turborepo task \
outputs and logs across
all your team’s Vercel projects. It also can share outputs
with other services that enable Remote Caching, like CI/CD systems.
This results in faster build times and deployments for your team.";
pub(crate) const REMOTE_CACHING_URL: &str =
"https://turbo.build/repo/docs/core-concepts/remote-caching";
pub(crate) const SPACES_URL: &str = "https://vercel.com/docs/spaces";
/// Verifies that caching status for a team is enabled, or prompts the user to
/// enable it.
///
/// # Arguments
///
/// * `team_id`: ID for team selected
/// * `token`: API token
/// * `selected_team`: The team selected
///
/// returns: Result<(), Error>
pub(crate) async fn verify_caching_enabled<'a>(
api_client: &APIClient,
team_id: &str,
token: &str,
selected_team: Option<SelectedTeam<'a>>,
) -> Result<()> {
let team_slug = selected_team.as_ref().and_then(|team| match team {
SelectedTeam::Team(team) => Some(team.slug.as_str()),
SelectedTeam::User => None,
});
let response = api_client
.get_caching_status(token, team_id, team_slug)
.await?;
match response.status {
CachingStatus::Disabled => {
let should_enable = should_enable_caching()?;
if should_enable {
match selected_team {
Some(SelectedTeam::Team(team)) if team.is_owner() => {
let url =
format!("https://vercel.com/teams/{}/settings/billing", team.slug);
enable_caching(&url)?;
}
Some(SelectedTeam::User) => {
let url = "https://vercel.com/account/billing";
enable_caching(url)?;
}
None => {
let team = api_client
.get_team(token, team_id)
.await?
.ok_or_else(|| anyhow!("unable to find team {}", team_id))?;
let url =
format!("https://vercel.com/teams/{}/settings/billing", team.slug);
enable_caching(&url)?;
}
_ => {}
}
}
Ok(())
}
CachingStatus::OverLimit => Err(anyhow!("usage limit")),
CachingStatus::Paused => Err(anyhow!("spending paused")),
CachingStatus::Enabled => Ok(()),
}
}
pub async fn link(
base: &mut CommandBase,
modify_gitignore: bool,
target: LinkTarget,
) -> Result<()> {
let homedir_path = home_dir().ok_or_else(|| anyhow!("could not find home directory."))?;
let homedir = homedir_path.to_string_lossy();
let repo_root_with_tilde = base.repo_root.to_string_lossy().replacen(&*homedir, "~", 1);
let api_client = base.api_client()?;
let token = base.user_config()?.token().ok_or_else(|| {
anyhow!(
"User not found. Please login to Turborepo first by running {}.",
BOLD.apply_to("`npx turbo login`")
)
})?;
match target {
LinkTarget::RemoteCache => {
println!(
">>> Remote Caching
{}
For more info, see {}
",
REMOTE_CACHING_INFO,
base.ui.apply(UNDERLINE.apply_to(REMOTE_CACHING_URL))
);
if !should_link_remote_cache(base, &repo_root_with_tilde)? {
return Err(anyhow!("canceled"));
}
let user_response = api_client
.get_user(token)
.await
.context("could not get user information")?;
let user_display_name = user_response
.user
.name
.as_deref()
.unwrap_or(user_response.user.username.as_str());
let teams_response = api_client
.get_teams(token)
.await
.context("could not get team information")?;
let selected_team = select_team(base, &teams_response.teams, user_display_name)?;
let team_id = match selected_team {
SelectedTeam::User => user_response.user.id.as_str(),
SelectedTeam::Team(team) => team.id.as_str(),
};
verify_caching_enabled(&api_client, team_id, token, Some(selected_team.clone()))
.await?;
fs::create_dir_all(base.repo_root.join(".turbo"))
.context("could not create .turbo directory")?;
base.repo_config_mut()?
.set_team_id(Some(team_id.to_string()))?;
let chosen_team_name = match selected_team {
SelectedTeam::User => user_display_name,
SelectedTeam::Team(team) => team.name.as_str(),
};
if modify_gitignore {
add_turbo_to_gitignore(base)?;
}
println!(
"
{} Turborepo CLI authorized for {}
{}
",
base.ui.rainbow(">>> Success!"),
base.ui.apply(BOLD.apply_to(chosen_team_name)),
GREY.apply_to("To disable Remote Caching, run `npx turbo unlink`")
);
return Ok(());
}
LinkTarget::Spaces => {
println!(
">>> Vercel Spaces (Beta)
For more info, see {}
",
base.ui.apply(UNDERLINE.apply_to(SPACES_URL))
);
if !should_link_spaces(base, &repo_root_with_tilde)? {
return Err(anyhow!("canceled"));
}
let spaces_response = api_client
.get_spaces(token, base.repo_config()?.team_id())
.await
.context("could not get spaces information")?;
let selected_space = select_space(base, &spaces_response.spaces)?;
// print result from selected_space
let space = match selected_space {
SelectedSpace::Space(space) => space,
};
add_space_id_to_turbo_json(base, &space.id).map_err(|err| {
return anyhow!(
"Could not persist selected space ({}) to `experimentalSpaces.id` in \
turbo.json {}",
space.id,
err
);
})?;
println!(
"
{} {} linked to {}
{}
",
base.ui.rainbow(">>> Success!"),
base.ui.apply(BOLD.apply_to(&repo_root_with_tilde)),
base.ui.apply(BOLD.apply_to(&space.name)),
GREY.apply_to(
"To remove Spaces integration, run `npx turbo unlink --target spaces`"
)
);
return Ok(());
}
}
}
fn should_enable_caching() -> Result<bool> {
let theme = ColorfulTheme::default();
Ok(Confirm::with_theme(&theme)
.with_prompt(
"Remote Caching was previously disabled for this team. Would you like to enable it \
now?",
)
.default(true)
.interact()?)
}
#[cfg(test)]
fn select_team<'a>(_: &CommandBase, teams: &'a [Team], _: &'a str) -> Result<SelectedTeam<'a>> {
let mut rng = rand::thread_rng();
let idx = rng.gen_range(0..=(teams.len()));
if idx == teams.len() {
Ok(SelectedTeam::User)
} else {
Ok(SelectedTeam::Team(&teams[idx]))
}
}
#[cfg(not(test))]
fn select_team<'a>(
base: &CommandBase,
teams: &'a [Team],
user_display_name: &'a str,
) -> Result<SelectedTeam<'a>> {
let mut team_names = vec![user_display_name];
team_names.extend(teams.iter().map(|team| team.name.as_str()));
let theme = ColorfulTheme {
active_item_style: Style::new().cyan().bold(),
active_item_prefix: Style::new().cyan().bold().apply_to(">".to_string()),
prompt_prefix: Style::new().dim().bold().apply_to("?".to_string()),
values_style: Style::new().cyan(),
..ColorfulTheme::default()
};
let prompt = format!(
"{}\n {}",
base.ui.apply(BOLD.apply_to(
"Which Vercel scope (and Remote Cache) do you want associated with this Turborepo?",
)),
base.ui
.apply(CYAN.apply_to("[Use arrows to move, type to filter]"))
);
let selection = FuzzySelect::with_theme(&theme)
.with_prompt(prompt)
.items(&team_names)
.default(0)
.interact()?;
if selection == 0 {
Ok(SelectedTeam::User)
} else {
Ok(SelectedTeam::Team(&teams[selection - 1]))
}
}
#[cfg(test)]
fn select_space<'a>(_: &CommandBase, spaces: &'a [Space]) -> Result<SelectedSpace<'a>> {
let mut rng = rand::thread_rng();
let idx = rng.gen_range(0..spaces.len());
Ok(SelectedSpace::Space(&spaces[idx]))
}
#[cfg(not(test))]
fn select_space<'a>(base: &CommandBase, spaces: &'a [Space]) -> Result<SelectedSpace<'a>> {
let space_names = spaces
.iter()
.map(|space| space.name.as_str())
.collect::<Vec<_>>();
let theme = ColorfulTheme {
active_item_style: Style::new().cyan().bold(),
active_item_prefix: Style::new().cyan().bold().apply_to(">".to_string()),
prompt_prefix: Style::new().dim().bold().apply_to("?".to_string()),
values_style: Style::new().cyan(),
..ColorfulTheme::default()
};
let prompt = format!(
"{}\n {}",
base.ui.apply(
BOLD.apply_to("Which Vercel space do you want associated with this Turborepo?",)
),
base.ui
.apply(CYAN.apply_to("[Use arrows to move, type to filter]"))
);
let selection = FuzzySelect::with_theme(&theme)
.with_prompt(prompt)
.items(&space_names)
.default(0)
.interact()?;
Ok(SelectedSpace::Space(&spaces[selection]))
}
#[cfg(test)]
fn should_link_remote_cache(_: &CommandBase, _: &str) -> Result<bool> {
Ok(true)
}
#[cfg(not(test))]
fn should_link_remote_cache(base: &CommandBase, location: &str) -> Result<bool> {
let prompt = format!(
"{}{} {}",
base.ui.apply(BOLD.apply_to(GREY.apply_to("? "))),
base.ui
.apply(BOLD.apply_to("Would you like to enable Remote Caching for")),
base.ui.apply(BOLD.apply_to(CYAN.apply_to(location)))
);
Ok(Confirm::new().with_prompt(prompt).interact()?)
}
#[cfg(test)]
fn should_link_spaces(_: &CommandBase, _: &str) -> Result<bool> {
Ok(true)
}
#[cfg(not(test))]
fn should_link_spaces(base: &CommandBase, location: &str) -> Result<bool> {
let prompt = format!(
"{}{} {} {}",
base.ui.apply(BOLD.apply_to(GREY.apply_to("? "))),
base.ui.apply(BOLD.apply_to("Would you like to link")),
base.ui.apply(BOLD.apply_to(CYAN.apply_to(location))),
base.ui.apply(BOLD.apply_to("to Vercel Spaces")),
);
Ok(Confirm::new().with_prompt(prompt).interact()?)
}
fn enable_caching(url: &str) -> Result<()> {
webbrowser::open(url).with_context(|| {
format!(
"Failed to open browser. Please visit {} to enable Remote Caching",
url
)
})?;
println!("Visit {} in your browser to enable Remote Caching", url);
// We return an error no matter what
Err(anyhow!("link after enabling caching"))
}
fn add_turbo_to_gitignore(base: &CommandBase) -> Result<()> {
let gitignore_path = base.repo_root.join(".gitignore");
if !gitignore_path.exists() {
let mut gitignore = File::create(gitignore_path)?;
#[cfg(unix)]
gitignore.metadata()?.permissions().set_mode(0o0644);
writeln!(gitignore, ".turbo")?;
} else {
let gitignore = File::open(&gitignore_path)?;
let mut lines = io::BufReader::new(gitignore).lines();
let has_turbo = lines.any(|line| line.map_or(false, |line| line.trim() == ".turbo"));
if !has_turbo {
let mut gitignore = OpenOptions::new()
.read(true)
.append(true)
.open(&gitignore_path)?;
writeln!(gitignore, ".turbo")?;
}
}
Ok(())
}
fn add_space_id_to_turbo_json(base: &CommandBase, space_id: &str) -> Result<()> {
let turbo_json_path = base.repo_root.join("turbo.json");
if !turbo_json_path.exists() {
return Err(anyhow!("turbo.json not found."));
}
let turbo_json_file = File::open(&turbo_json_path)?;
let mut turbo_json: TurboJson = serde_json::from_reader(turbo_json_file)?;
match turbo_json.experimental_spaces {
Some(mut spaces_config) => {
spaces_config.id = Some(space_id.to_string());
turbo_json.experimental_spaces = Some(spaces_config);
}
None => {
turbo_json.experimental_spaces = Some(SpacesJson {
id: Some(space_id.to_string()),
other: None,
});
}
}
// write turbo_json back to file
let config_file = File::create(&turbo_json_path)?;
serde_json::to_writer_pretty(&config_file, &turbo_json)?;
Ok(())
}
#[cfg(test)]
mod test {
use std::fs;
use tempfile::{NamedTempFile, TempDir};
use tokio::sync::OnceCell;
use vercel_api_mock::start_test_server;
use crate::{
cli::LinkTarget,
commands::{link, CommandBase},
config::{ClientConfigLoader, RepoConfigLoader, TurboJson, UserConfigLoader},
ui::UI,
Args,
};
#[tokio::test]
async fn test_link_remote_cache() {
let user_config_file = NamedTempFile::new().unwrap();
fs::write(user_config_file.path(), r#"{ "token": "hello" }"#).unwrap();
let repo_config_file = NamedTempFile::new().unwrap();
fs::write(
repo_config_file.path(),
r#"{ "apiurl": "http://localhost:3000" }"#,
)
.unwrap();
let port = port_scanner::request_open_port().unwrap();
let handle = tokio::spawn(start_test_server(port));
let mut base = CommandBase {
repo_root: Default::default(),
ui: UI::new(false),
client_config: OnceCell::from(ClientConfigLoader::new().load().unwrap()),
user_config: OnceCell::from(
UserConfigLoader::new(user_config_file.path().to_path_buf())
.with_token(Some("token".to_string()))
.load()
.unwrap(),
),
repo_config: OnceCell::from(
RepoConfigLoader::new(repo_config_file.path().to_path_buf())
.with_api(Some(format!("http://localhost:{}", port)))
.with_login(Some(format!("http://localhost:{}", port)))
.load()
.unwrap(),
),
args: Args::default(),
version: "",
};
link::link(&mut base, false, LinkTarget::RemoteCache)
.await
.unwrap();
handle.abort();
let team_id = base.repo_config().unwrap().team_id();
assert!(
team_id == Some(vercel_api_mock::EXPECTED_USER_ID)
|| team_id == Some(vercel_api_mock::EXPECTED_TEAM_ID)
);
}
#[tokio::test]
async fn test_link_spaces() {
// user config
let user_config_file = NamedTempFile::new().unwrap();
fs::write(user_config_file.path(), r#"{ "token": "hello" }"#).unwrap();
// repo config
let repo_config_file = NamedTempFile::new().unwrap();
fs::write(
repo_config_file.path(),
r#"{ "apiurl": "http://localhost:3000" }"#,
)
.unwrap();
let port = port_scanner::request_open_port().unwrap();
let handle = tokio::spawn(start_test_server(port));
let mut base = CommandBase {
repo_root: TempDir::new().unwrap().into_path(),
ui: UI::new(false),
client_config: OnceCell::from(ClientConfigLoader::new().load().unwrap()),
user_config: OnceCell::from(
UserConfigLoader::new(user_config_file.path().to_path_buf())
.with_token(Some("token".to_string()))
.load()
.unwrap(),
),
repo_config: OnceCell::from(
RepoConfigLoader::new(repo_config_file.path().to_path_buf())
.with_api(Some(format!("http://localhost:{}", port)))
.with_login(Some(format!("http://localhost:{}", port)))
.load()
.unwrap(),
),
args: Args::default(),
version: "",
};
// turbo config
let turbo_json_file = base.repo_root.join("turbo.json");
fs::write(
turbo_json_file.as_path(),
r#"{ "globalEnv": [], "pipeline": {} }"#,
)
.unwrap();
link::link(&mut base, false, LinkTarget::Spaces)
.await
.unwrap();
handle.abort();
// verify space id is added to turbo.json
let turbo_json_file = fs::File::open(&turbo_json_file).unwrap();
let turbo_json: TurboJson = serde_json::from_reader(turbo_json_file).unwrap();
assert_eq!(
turbo_json.experimental_spaces.unwrap().id.unwrap(),
vercel_api_mock::EXPECTED_SPACE_ID
);
}
}