Skip to content

Commit b99c59b

Browse files
authored
feat(turbo): add spaces link (vercel#4632)
1 parent 56c3da3 commit b99c59b

File tree

8 files changed

+451
-77
lines changed

8 files changed

+451
-77
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/turborepo-api-client/src/lib.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,22 @@ impl Team {
7676
}
7777
}
7878

79+
#[derive(Debug, Clone, Serialize, Deserialize)]
80+
pub struct Space {
81+
pub id: String,
82+
pub name: String,
83+
}
84+
7985
#[derive(Debug, Clone, Serialize, Deserialize)]
8086
pub struct TeamsResponse {
8187
pub teams: Vec<Team>,
8288
}
8389

90+
#[derive(Debug, Clone, Serialize, Deserialize)]
91+
pub struct SpacesResponse {
92+
pub spaces: Vec<Space>,
93+
}
94+
8495
#[derive(Debug, Clone, Serialize, Deserialize)]
8596
pub struct User {
8697
pub id: String,
@@ -213,6 +224,37 @@ impl APIClient {
213224
})
214225
}
215226

227+
pub async fn get_spaces(&self, token: &str, team_id: Option<&str>) -> Result<SpacesResponse> {
228+
// create url with teamId if provided
229+
let endpoint = match team_id {
230+
Some(team_id) => format!("/v0/spaces?limit=100&teamId={}", team_id),
231+
None => "/v0/spaces?limit=100".to_string(),
232+
};
233+
234+
let response = self
235+
.make_retryable_request(|| {
236+
let request_builder = self
237+
.client
238+
.get(self.make_url(endpoint.as_str()))
239+
.header("User-Agent", self.user_agent.clone())
240+
.header("Content-Type", "application/json")
241+
.header("Authorization", format!("Bearer {}", token));
242+
243+
request_builder.send()
244+
})
245+
.await?
246+
.error_for_status()?;
247+
248+
response.json().await.map_err(|err| {
249+
anyhow!(
250+
"Error getting spaces: {}",
251+
err.status()
252+
.and_then(|status| status.canonical_reason())
253+
.unwrap_or(&err.to_string())
254+
)
255+
})
256+
}
257+
216258
pub async fn verify_sso_token(&self, token: &str, token_name: &str) -> Result<VerifiedSsoUser> {
217259
let response = self
218260
.make_retryable_request(|| {

crates/turborepo-lib/src/cli.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,12 @@ pub enum DaemonCommand {
177177
Stop,
178178
}
179179

180+
#[derive(Copy, Clone, Debug, PartialEq, Serialize, ValueEnum)]
181+
pub enum LinkTarget {
182+
RemoteCache,
183+
Spaces,
184+
}
185+
180186
impl Args {
181187
pub fn new() -> Result<Self> {
182188
let mut clap_args = match Args::try_parse() {
@@ -250,6 +256,10 @@ pub enum Command {
250256
/// Do not create or modify .gitignore (default false)
251257
#[clap(long)]
252258
no_gitignore: bool,
259+
260+
/// Specify what should be linked (default "remote cache")
261+
#[clap(long, value_enum, default_value_t = LinkTarget::RemoteCache)]
262+
target: LinkTarget,
253263
},
254264
/// Login to your Vercel account
255265
Login {
@@ -280,7 +290,11 @@ pub enum Command {
280290
Run(Box<RunArgs>),
281291
/// Unlink the current directory from your Vercel organization and disable
282292
/// Remote Caching
283-
Unlink {},
293+
Unlink {
294+
/// Specify what should be unlinked (default "remote cache")
295+
#[clap(long, value_enum, default_value_t = LinkTarget::RemoteCache)]
296+
target: LinkTarget,
297+
},
284298
}
285299

286300
#[derive(Parser, Clone, Debug, Default, Serialize, PartialEq)]
@@ -506,30 +520,32 @@ pub async fn run(repo_state: Option<RepoState>) -> Result<Payload> {
506520

507521
Ok(Payload::Rust(Ok(0)))
508522
}
509-
Command::Link { no_gitignore } => {
523+
Command::Link { no_gitignore, target} => {
510524
if clap_args.test_run {
511525
println!("Link test run successful");
512526
return Ok(Payload::Rust(Ok(0)));
513527
}
514528

515529
let modify_gitignore = !*no_gitignore;
530+
let to = *target;
516531
let mut base = CommandBase::new(clap_args, repo_root, version)?;
517532

518-
if let Err(err) = link::link(&mut base, modify_gitignore).await {
533+
if let Err(err) = link::link(&mut base, modify_gitignore, to).await {
519534
error!("error: {}", err.to_string())
520535
};
521536

522537
Ok(Payload::Rust(Ok(0)))
523538
}
524-
Command::Unlink { .. } => {
539+
Command::Unlink { target } => {
525540
if clap_args.test_run {
526541
println!("Unlink test run successful");
527542
return Ok(Payload::Rust(Ok(0)));
528543
}
529544

545+
let from = *target;
530546
let mut base = CommandBase::new(clap_args, repo_root, version)?;
531547

532-
unlink::unlink(&mut base)?;
548+
unlink::unlink(&mut base, from)?;
533549

534550
Ok(Payload::Rust(Ok(0)))
535551
}
@@ -1212,7 +1228,9 @@ mod test {
12121228
assert_eq!(
12131229
Args::try_parse_from(["turbo", "unlink"]).unwrap(),
12141230
Args {
1215-
command: Some(Command::Unlink {}),
1231+
command: Some(Command::Unlink {
1232+
target: crate::cli::LinkTarget::RemoteCache
1233+
}),
12161234
..Args::default()
12171235
}
12181236
);
@@ -1222,7 +1240,9 @@ mod test {
12221240
command_args: vec![],
12231241
global_args: vec![vec!["--cwd", "../examples/with-yarn"]],
12241242
expected_output: Args {
1225-
command: Some(Command::Unlink {}),
1243+
command: Some(Command::Unlink {
1244+
target: crate::cli::LinkTarget::RemoteCache,
1245+
}),
12261246
cwd: Some(PathBuf::from("../examples/with-yarn")),
12271247
..Args::default()
12281248
},

0 commit comments

Comments
 (0)