Skip to content

Commit

Permalink
Allow cloud instance names to contain '-' and '_' in the org part to …
Browse files Browse the repository at this point in the history
…support vercel instances (#1361)
  • Loading branch information
jaclarke authored and fantix committed Aug 30, 2024
1 parent c0ff5f3 commit 53fc65a
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 57 deletions.
82 changes: 33 additions & 49 deletions Cargo.lock

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

32 changes: 30 additions & 2 deletions src/portable/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,8 +405,8 @@ pub fn is_valid_local_instance_name(name: &str) -> bool {
!was_dash
}

pub fn is_valid_cloud_name(name: &str) -> bool {
// For cloud instance name parts (organization slugs and instance names):
pub fn is_valid_cloud_instance_name(name: &str) -> bool {
// For cloud instance name part:
// 1. Allow only letters, numbers and single dashes
// 2. Must not start or end with a dash
// regex: ^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$
Expand All @@ -433,6 +433,34 @@ pub fn is_valid_cloud_name(name: &str) -> bool {
!was_dash
}

pub fn is_valid_cloud_org_name(name: &str) -> bool {
// For cloud organization slug part:
// 1. Allow only letters, numbers, underscores and single dashes
// 2. Must not end with a dash
// regex: ^-?[a-zA-Z0-9_]+(-[a-zA-Z0-9]+)*$
let mut chars = name.chars();
match chars.next() {
Some(c) if c.is_ascii_alphanumeric() || c == '-' || c == '_' => {}
_ => return false,
}
let mut was_dash = false;
for c in chars {
if c == '-' {
if was_dash {
return false;
} else {
was_dash = true;
}
} else {
if !(c.is_ascii_alphanumeric() || c == '_') {
return false;
}
was_dash = false;
}
}
!was_dash
}

#[derive(Debug, thiserror::Error)]
#[error("Not a local instance")]
pub struct NonLocalInstance;
Expand Down
10 changes: 6 additions & 4 deletions src/portable/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use serde::{Deserialize, Serialize};
use crate::cloud::ops::CloudTier;
use crate::commands::ExitCode;
use crate::options::{CloudOptions, ConnectionOptions};
use crate::portable::local::{is_valid_cloud_name, is_valid_local_instance_name};
use crate::portable::local::{
is_valid_cloud_instance_name, is_valid_cloud_org_name, is_valid_local_instance_name,
};
use crate::portable::repository::Channel;
use crate::portable::ver;
use crate::print::{echo, err_marker, warn};
Expand Down Expand Up @@ -735,17 +737,17 @@ impl FromStr for InstanceName {
type Err = anyhow::Error;
fn from_str(name: &str) -> anyhow::Result<InstanceName> {
if let Some((org_slug, instance_name)) = name.split_once('/') {
if !is_valid_cloud_name(instance_name) {
if !is_valid_cloud_instance_name(instance_name) {
anyhow::bail!(
"instance name \"{}\" must be a valid identifier, \
regex: ^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$",
instance_name,
);
}
if !is_valid_cloud_name(org_slug) {
if !is_valid_cloud_org_name(org_slug) {
anyhow::bail!(
"org name \"{}\" must be a valid identifier, \
regex: ^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$",
regex: ^-?[a-zA-Z0-9_]+(-[a-zA-Z0-9]+)*$",
org_slug,
);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/shared-client-testcases
10 changes: 9 additions & 1 deletion tests/shared-client-tests/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,15 @@ fn connection_{i}() {{
let mut buf = Vec::new();
if let Some(opts) = opts {
for (key, value) in opts {
let arg = if let Some(arg) = opt_key_mapping.get(key.as_str()) {
let arg = if key == "instance" {
let argv = value.as_str().unwrap();
write!(
buf,
r#"
.arg("--instance={argv}")"#,
);
continue;
} else if let Some(arg) = opt_key_mapping.get(key.as_str()) {
arg
} else if key == "serverSettings" {
continue 'testcase;
Expand Down

0 comments on commit 53fc65a

Please sign in to comment.