Skip to content

Commit c44da5f

Browse files
authored
Merge pull request #2505 from itowlson/templates-so-tired-of-typing-github
`spin templates install`: allow repo name instead of URL
2 parents 042ce94 + aa4db7b commit c44da5f

File tree

1 file changed

+38
-1
lines changed

1 file changed

+38
-1
lines changed

src/commands/templates.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,10 @@ impl Install {
120120
let template_manager = TemplateManager::try_default()
121121
.context("Failed to construct template directory path")?;
122122
let source = match (&self.git, &self.dir) {
123-
(Some(git), None) => TemplateSource::try_from_git(git, &self.branch, SPIN_VERSION)?,
123+
(Some(git), None) => {
124+
let git_url = infer_github(git);
125+
TemplateSource::try_from_git(git_url, &self.branch, SPIN_VERSION)?
126+
}
124127
(None, Some(dir)) => {
125128
let abs_dir = dir.absolutize().map(|d| d.to_path_buf());
126129
TemplateSource::File(abs_dir.unwrap_or_else(|_| dir.clone()))
@@ -180,6 +183,19 @@ impl Install {
180183
}
181184
}
182185

186+
fn infer_github(raw: &str) -> String {
187+
match url::Url::parse(raw) {
188+
Err(url::ParseError::RelativeUrlWithoutBase) => {
189+
if raw.starts_with('/') {
190+
format!("https://github.com{raw}")
191+
} else {
192+
format!("https://github.com/{raw}")
193+
}
194+
}
195+
_ => raw.to_string(), // pass it through even if error, so Git can have a try
196+
}
197+
}
198+
183199
impl Upgrade {
184200
pub async fn run(&self) -> Result<()> {
185201
if self.git.is_some() {
@@ -612,3 +628,24 @@ async fn install_default_templates() -> anyhow::Result<()> {
612628
.context("Failed to install the default templates")?;
613629
Ok(())
614630
}
631+
632+
#[cfg(test)]
633+
mod tests {
634+
use super::*;
635+
636+
#[test]
637+
fn infers_github_url_if_needed() {
638+
assert_eq!(
639+
"https://github.com/fermyon/spin",
640+
infer_github("fermyon/spin")
641+
);
642+
assert_eq!(
643+
"https://github.com/fermyon/spin",
644+
infer_github("/fermyon/spin")
645+
);
646+
assert_eq!(
647+
"https://github.com/fermyon/spin",
648+
infer_github("https://github.com/fermyon/spin")
649+
);
650+
}
651+
}

0 commit comments

Comments
 (0)