Skip to content

Commit

Permalink
feat(turbo): add spaces link (#4632)
Browse files Browse the repository at this point in the history
  • Loading branch information
tknickman committed Apr 20, 2023
1 parent 56c3da3 commit b99c59b
Show file tree
Hide file tree
Showing 8 changed files with 451 additions and 77 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 42 additions & 0 deletions crates/turborepo-api-client/src/lib.rs
Expand Up @@ -76,11 +76,22 @@ impl Team {
}
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Space {
pub id: String,
pub name: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TeamsResponse {
pub teams: Vec<Team>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpacesResponse {
pub spaces: Vec<Space>,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
pub id: String,
Expand Down Expand Up @@ -213,6 +224,37 @@ impl APIClient {
})
}

pub async fn get_spaces(&self, token: &str, team_id: Option<&str>) -> Result<SpacesResponse> {
// create url with teamId if provided
let endpoint = match team_id {
Some(team_id) => format!("/v0/spaces?limit=100&teamId={}", team_id),
None => "/v0/spaces?limit=100".to_string(),
};

let response = self
.make_retryable_request(|| {
let request_builder = self
.client
.get(self.make_url(endpoint.as_str()))
.header("User-Agent", self.user_agent.clone())
.header("Content-Type", "application/json")
.header("Authorization", format!("Bearer {}", token));

request_builder.send()
})
.await?
.error_for_status()?;

response.json().await.map_err(|err| {
anyhow!(
"Error getting spaces: {}",
err.status()
.and_then(|status| status.canonical_reason())
.unwrap_or(&err.to_string())
)
})
}

pub async fn verify_sso_token(&self, token: &str, token_name: &str) -> Result<VerifiedSsoUser> {
let response = self
.make_retryable_request(|| {
Expand Down
34 changes: 27 additions & 7 deletions crates/turborepo-lib/src/cli.rs
Expand Up @@ -177,6 +177,12 @@ pub enum DaemonCommand {
Stop,
}

#[derive(Copy, Clone, Debug, PartialEq, Serialize, ValueEnum)]
pub enum LinkTarget {
RemoteCache,
Spaces,
}

impl Args {
pub fn new() -> Result<Self> {
let mut clap_args = match Args::try_parse() {
Expand Down Expand Up @@ -250,6 +256,10 @@ pub enum Command {
/// Do not create or modify .gitignore (default false)
#[clap(long)]
no_gitignore: bool,

/// Specify what should be linked (default "remote cache")
#[clap(long, value_enum, default_value_t = LinkTarget::RemoteCache)]
target: LinkTarget,
},
/// Login to your Vercel account
Login {
Expand Down Expand Up @@ -280,7 +290,11 @@ pub enum Command {
Run(Box<RunArgs>),
/// Unlink the current directory from your Vercel organization and disable
/// Remote Caching
Unlink {},
Unlink {
/// Specify what should be unlinked (default "remote cache")
#[clap(long, value_enum, default_value_t = LinkTarget::RemoteCache)]
target: LinkTarget,
},
}

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

Ok(Payload::Rust(Ok(0)))
}
Command::Link { no_gitignore } => {
Command::Link { no_gitignore, target} => {
if clap_args.test_run {
println!("Link test run successful");
return Ok(Payload::Rust(Ok(0)));
}

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

if let Err(err) = link::link(&mut base, modify_gitignore).await {
if let Err(err) = link::link(&mut base, modify_gitignore, to).await {
error!("error: {}", err.to_string())
};

Ok(Payload::Rust(Ok(0)))
}
Command::Unlink { .. } => {
Command::Unlink { target } => {
if clap_args.test_run {
println!("Unlink test run successful");
return Ok(Payload::Rust(Ok(0)));
}

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

unlink::unlink(&mut base)?;
unlink::unlink(&mut base, from)?;

Ok(Payload::Rust(Ok(0)))
}
Expand Down Expand Up @@ -1212,7 +1228,9 @@ mod test {
assert_eq!(
Args::try_parse_from(["turbo", "unlink"]).unwrap(),
Args {
command: Some(Command::Unlink {}),
command: Some(Command::Unlink {
target: crate::cli::LinkTarget::RemoteCache
}),
..Args::default()
}
);
Expand All @@ -1222,7 +1240,9 @@ mod test {
command_args: vec![],
global_args: vec![vec!["--cwd", "../examples/with-yarn"]],
expected_output: Args {
command: Some(Command::Unlink {}),
command: Some(Command::Unlink {
target: crate::cli::LinkTarget::RemoteCache,
}),
cwd: Some(PathBuf::from("../examples/with-yarn")),
..Args::default()
},
Expand Down

0 comments on commit b99c59b

Please sign in to comment.