Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: shuttle init --from #984

Merged
merged 19 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 35 additions & 58 deletions cargo-shuttle/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,64 +292,42 @@ pub struct TemplateLocation {
}

impl InitArgs {
pub fn git_templates(&self) -> Option<Vec<TemplateLocation>> {
pub fn git_template(&self) -> Option<TemplateLocation> {
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
if let Some(from) = self.from.clone() {
Some(vec![TemplateLocation {
Some(TemplateLocation {
auto_path: from,
subfolder: self.subfolder.clone(),
}])
})
} else {
self.template.as_ref().map(|t| t.templates())
self.template.as_ref().map(|t| t.template())
}
}
}

impl InitTemplateArg {
pub fn templates(&self) -> Vec<TemplateLocation> {
pub fn template(&self) -> TemplateLocation {
use InitTemplateArg::*;
let paths = match self {
let path = match self {
// first entry should be the default for
// that choice of framework (usually hello-world)
ActixWeb => vec![
"actix-web/hello-world",
"actix-web/postgres",
"actix-web/websocket-actorless",
],
Axum => vec![
"axum/hello-world",
"axum/static-files",
"axum/static-next-server",
"axum/websocket",
"axum/with-state",
],
Poem => vec!["poem/hello-world", "poem/mongodb", "poem/postgres"],
Poise => vec!["poise/hello-world"],
Rocket => vec![
"rocket/hello-world",
"rocket/authentication",
"rocket/dyn_template_hbs",
"rocket/persist",
"rocket/postgres",
"rocket/secrets",
"rocket/url-shortener",
"rocket/workspace",
],
Salvo => vec!["salvo/hello-world"],
Serenity => vec!["serenity/hello-world", "serenity/postgres"],
Thruster => vec!["thruster/hello-world", "thruster/postgres"],
Tide => vec!["tide/hello-world", "tide/postgres"],
Tower => vec!["tower/hello-world"],
Warp => vec!["warp/hello-world"],
None => vec!["custom/none"],
ActixWeb => "actix-web/hello-world",
Axum => "axum/hello-world",
Poem => "poem/hello-world",
Poise => "poise/hello-world",
Rocket => "rocket/hello-world",
Salvo => "salvo/hello-world",
Serenity => "serenity/hello-world",
Thruster => "thruster/hello-world",
Tide => "tide/hello-world",
Tower => "tower/hello-world",
Warp => "warp/hello-world",
None => "custom/none",
};

paths
.iter()
.map(|path| TemplateLocation {
auto_path: EXAMPLES_REPO.into(),
subfolder: Some(path.to_string()),
})
.collect()
TemplateLocation {
auto_path: EXAMPLES_REPO.into(),
subfolder: Some(path.to_string()),
}
}
}

Expand Down Expand Up @@ -389,11 +367,11 @@ mod tests {
path: PathBuf::new(),
};
assert_eq!(
init_args.git_templates(),
Some(vec![TemplateLocation {
init_args.git_template(),
Some(TemplateLocation {
auto_path: EXAMPLES_REPO.into(),
subfolder: Some("tower/hello-world".into())
}])
})
);

// pre-defined template (multiple)
Expand All @@ -406,13 +384,12 @@ mod tests {
path: PathBuf::new(),
};
assert_eq!(
init_args.git_templates().unwrap()[0],
TemplateLocation {
init_args.git_template(),
Some(TemplateLocation {
auto_path: EXAMPLES_REPO.into(),
subfolder: Some("axum/hello-world".into())
}
})
);
assert!(init_args.git_templates().unwrap().len() > 1);

// pre-defined "none" template
let init_args = InitArgs {
Expand All @@ -424,11 +401,11 @@ mod tests {
path: PathBuf::new(),
};
assert_eq!(
init_args.git_templates(),
Some(vec![TemplateLocation {
init_args.git_template(),
Some(TemplateLocation {
auto_path: EXAMPLES_REPO.into(),
subfolder: Some("custom/none".into())
}])
})
);

// git template with path
Expand All @@ -441,11 +418,11 @@ mod tests {
path: PathBuf::new(),
};
assert_eq!(
init_args.git_templates(),
Some(vec![TemplateLocation {
init_args.git_template(),
Some(TemplateLocation {
auto_path: "https://github.com/some/repo".into(),
subfolder: Some("some/path".into())
}])
})
);

// No template or repo chosen
Expand All @@ -457,7 +434,7 @@ mod tests {
login_args: LoginArgs { api_key: None },
path: PathBuf::new(),
};
assert_eq!(init_args.git_templates(), None);
assert_eq!(init_args.git_template(), None);
}

#[test]
Expand Down
89 changes: 33 additions & 56 deletions cargo-shuttle/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod provisioner_server;

use args::LogoutArgs;
use indicatif::ProgressBar;
use indoc::printdoc;
use shuttle_common::claims::{ClaimService, InjectPropagation};
use shuttle_common::models::deployment::{
get_deployments_table, DeploymentRequest, GIT_STRINGS_MAX_LENGTH,
Expand Down Expand Up @@ -157,7 +158,7 @@ impl Shuttle {
async fn init(&mut self, args: InitArgs, mut project_args: ProjectArgs) -> Result<()> {
// Turns the template or git args (if present) to a vec of repo folders that match.
// If an explicit git arg is given, or a framework only has one template, the vec length will be 1.
let git_templates = args.git_templates();
let git_templates = args.git_template();
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved

// Caveat: No way of telling if args.path was given or default
// Ideally that would be checked here (go interactive if not given)
Expand All @@ -180,7 +181,12 @@ impl Shuttle {

// 2. Ask for project name
if project_args.name.is_none() {
println!("How do you want to name your project? It will be hosted at ${{project_name}}.shuttleapp.rs.");
printdoc!(
"
What do you want to name your project?
It will be hosted at ${{project_name}}.shuttleapp.rs, so choose something unique!
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
"
);
// TODO: Check whether the project name is still available
project_args.name = Some(
Input::with_theme(&theme)
Expand Down Expand Up @@ -210,65 +216,23 @@ impl Shuttle {
};

// 4. Ask for the framework
let template = {
let templates = match git_templates {
Some(git_templates) => git_templates,
None => {
println!(
"Shuttle works with a range of web frameworks. Which one do you want to use?"
);
println!(
"Hint: Check the shuttle-examples repo for a full list of templates: {}",
EXAMPLES_REPO
);
let frameworks = args::InitTemplateArg::iter().collect::<Vec<_>>();
let index = FuzzySelect::with_theme(&theme)
.with_prompt("Framework")
.items(&frameworks)
.default(0)
.interact()?;
println!();
frameworks[index].templates()
}
};

if templates.is_empty() {
bail!("Developer error. List of templates for that framework was empty.")
} else if !interactive || templates.len() == 1
|| !Confirm::with_theme(&theme)
.with_prompt(format!(
"Use a different template than \"{}\"? ({} available)",
templates[0]
.subfolder
.as_ref()
.expect("Pre-defined templates from examples repo should always have a git path"),
templates.len() - 1
))
.default(false)
.interact()?
{
// first entry should be hello-world (the default for that choice of framework)
templates[0].clone()
} else {
let framework_templates = templates
.iter()
.map(|t|
t.subfolder
.as_ref()
.expect("Pre-defined templates from examples repo should always have a git path")
)
.collect::<Vec<_>>();
let template = match git_templates {
Some(git_templates) => git_templates,
None => {
println!(
"Shuttle works with a range of web frameworks. Which one do you want to use?"
);
let frameworks = args::InitTemplateArg::iter().collect::<Vec<_>>();
let index = FuzzySelect::with_theme(&theme)
.with_prompt("Template")
.items(&framework_templates)
.with_prompt("Framework")
.items(&frameworks)
.default(0)
.interact()?;
println!();
templates[index].clone()
frameworks[index].template()
}
};

// Covers the official examples at least...
let serenity_idle_hint = template
.subfolder
.as_ref()
Expand All @@ -285,6 +249,16 @@ impl Shuttle {
)?;
println!();

printdoc!(
"
Hint: Check the examples repo for a full list of templates:
{EXAMPLES_REPO}
Hint: You can also use `cargo shuttle init --from` to clone templates.
See the docs or help page.
"
);
println!();

// 6. Confirm that the user wants to create the project environment on Shuttle
let should_create_environment = if !interactive {
args.create_env
Expand Down Expand Up @@ -317,8 +291,11 @@ impl Shuttle {
"Run `cargo shuttle project start` to create a project environment on Shuttle."
);
if serenity_idle_hint {
println!(
"Hint: Discord bots might want to use `--idle-minutes 0` when starting the project so that they don't go offline."
printdoc!(
"
Hint: Discord bots might want to use `--idle-minutes 0` when
starting the project so that they don't go offline.
jonaro00 marked this conversation as resolved.
Show resolved Hide resolved
"
);
}
}
Expand Down