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

✨ Use cached template when it is up-to-date #11

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

ion098
Copy link
Contributor

@ion098 ion098 commented Dec 20, 2024

No description provided.

src/errors.rs Outdated
#[cfg(feature = "fetch-template")]
#[error(transparent)]
#[diagnostic(code(cargo_v5::bad_response))]
BadResponse(#[from] reqwest::Error),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be renamed to ReqwestError

src/main.rs Outdated
Comment on lines 95 to 105
/// Whether or not to fetch the latest template online.
#[cfg_attr(feature = "fetch-template", arg(long, default_value = "true"))]
#[cfg_attr(not(feature = "fetch-template"), arg(skip = true))]
use_internet: bool,
},
/// Creates a new vexide project in the current directory
Init,
Init {
/// Whether or not to fetch the latest template online.
#[cfg_attr(feature = "fetch-template", arg(long, default_value = "true"))]
#[cfg_attr(not(feature = "fetch-template"), arg(skip = true))]
use_internet: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth considering separating this argument into its own struct and parsing it flattened because it's a significant amount of code duplication. Also I'm not entirely sure about its name.


#[cfg(feature = "fetch-template")]
let template = template.unwrap_or_else(|| {
warn!("No emplate found in cache, using builtin template.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo in template and I'm not sure this should be a warning.

@@ -55,7 +97,7 @@ fn unpack_template(template: Vec<u8>, dir: &Utf8PathBuf) -> io::Result<()> {
Ok(())
}

pub async fn new(path: Utf8PathBuf, name: Option<String>) -> Result<(), CliError> {
pub async fn new(path: Utf8PathBuf, name: Option<String>, use_internet: bool) -> Result<(), CliError> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that fetch_template or similar is a better name than use_internet because it clearly shows what the internet is being used for.

@ion098 ion098 marked this pull request as ready for review December 24, 2024 20:29
let response_text = response.text().await.ok().unwrap_or("{}".to_string());
match &serde_json::from_str::<Value>(&response_text).unwrap_or_default()["sha"] {
Value::String(str) => Ok(str.clone()),
_ => unreachable!("Internal error: GitHub API broken"),
Copy link
Member

@Tropix126 Tropix126 Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this should be unreachable or a panic - as unlikely as it is I don't entirely trust serde/Github to be totally reliable here as its something we don't have control over.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Unreachable is completely incorrect here anyway.

.ok()
.map(|path| path.with_file_name("vexide-template.tar.gz"))
})
fn get_cached_template() -> Option<Template> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions should probably use the tokio version of fs::write and be made async

/// Do not download the latest template online.
#[cfg_attr(feature = "fetch-template", arg(long, default_value = "false"))]
#[cfg_attr(not(feature = "fetch-template"), arg(skip = false))]
no_download_template: bool,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be named something a little more concise, maybe --offline?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants