-
Notifications
You must be signed in to change notification settings - Fork 35
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(crates/tuono): tuono new - add --latest
flag to download latest template
#292
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -11,6 +11,9 @@ const GITHUB_TUONO_TAGS_URL: &str = "https://api.github.com/repos/tuono-labs/tuo | |||||
const GITHUB_TUONO_TAG_COMMIT_TREES_URL: &str = | ||||||
"https://api.github.com/repos/tuono-labs/tuono/git/trees/"; | ||||||
|
||||||
const GITHUB_TUONO_LATEST_TREE_URL: &str = | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need this constant? Couldn't we just do |
||||||
"https://api.github.com/repos/tuono-labs/tuono/git/trees/main?recursive=1"; | ||||||
|
||||||
const GITHUB_RAW_CONTENT_URL: &str = "https://raw.githubusercontent.com/tuono-labs/tuono"; | ||||||
|
||||||
#[derive(Deserialize, Debug)] | ||||||
|
@@ -50,7 +53,11 @@ fn create_file(path: PathBuf, content: String) -> std::io::Result<()> { | |||||
Ok(()) | ||||||
} | ||||||
|
||||||
pub fn create_new_project(folder_name: Option<String>, template: Option<String>) { | ||||||
pub fn create_new_project( | ||||||
folder_name: Option<String>, | ||||||
template: Option<String>, | ||||||
flag_latest: bool, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) { | ||||||
let folder = folder_name.unwrap_or(".".to_string()); | ||||||
|
||||||
// In case of missing select the tuono example | ||||||
|
@@ -64,23 +71,34 @@ pub fn create_new_project(folder_name: Option<String>, template: Option<String>) | |||||
// This string does not include the "v" version prefix | ||||||
let cli_version: &str = crate_version!(); | ||||||
|
||||||
let res_tag = client | ||||||
.get(format!("{}v{}", GITHUB_TUONO_TAGS_URL, cli_version)) | ||||||
.send() | ||||||
.unwrap_or_else(|_| panic!("Failed to call the tag github API for v{cli_version}")) | ||||||
.json::<GithubTagResponse>() | ||||||
.expect("Failed to parse the tag response"); | ||||||
let tree_url: String = if flag_latest { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about turning this if statement into a standalone function? I think this would improve the code readability |
||||||
GITHUB_TUONO_LATEST_TREE_URL.to_string() | ||||||
} else { | ||||||
// This string does not include the "v" version prefix | ||||||
let res_tag = client | ||||||
.get(format!("{}v{}", GITHUB_TUONO_TAGS_URL, cli_version)) | ||||||
.send() | ||||||
.unwrap_or_else(|_| panic!("Failed to call the tag github API for v{cli_version}")) | ||||||
.json::<GithubTagResponse>() | ||||||
.expect("Failed to parse the tag response"); | ||||||
|
||||||
let sha_tagged_commit = res_tag.object.sha; | ||||||
let sha_tagged_commit = res_tag.object.sha; | ||||||
|
||||||
let res_tree = client | ||||||
.get(format!( | ||||||
format!( | ||||||
"{}{}?recursive=1", | ||||||
GITHUB_TUONO_TAG_COMMIT_TREES_URL, sha_tagged_commit | ||||||
)) | ||||||
) | ||||||
}; | ||||||
|
||||||
let res_tree = client | ||||||
.get(tree_url) | ||||||
.send() | ||||||
.unwrap_or_else(|_| { | ||||||
panic!("Failed to call the tagged commit tree github API for v{cli_version}") | ||||||
if flag_latest { | ||||||
panic!("Failed to call the tagged commit tree github API for v{cli_version}"); | ||||||
} else { | ||||||
panic!("Failed to call the tagged commit tree github API for latest version"); | ||||||
} | ||||||
}) | ||||||
.json::<GithubTreeResponse<GithubFile>>() | ||||||
.expect("Failed to parse the tree structure"); | ||||||
|
@@ -119,8 +137,13 @@ pub fn create_new_project(folder_name: Option<String>, template: Option<String>) | |||||
} in new_project_files.iter() | ||||||
{ | ||||||
if let GithubFileType::Blob = element_type { | ||||||
let raw_content_url = if flag_latest { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually
|
||||||
format!("{GITHUB_RAW_CONTENT_URL}/v{cli_version}/{path}") | ||||||
} else { | ||||||
format!("{GITHUB_RAW_CONTENT_URL}/{path}") | ||||||
}; | ||||||
let file_content = client | ||||||
.get(format!("{GITHUB_RAW_CONTENT_URL}/v{cli_version}/{path}")) | ||||||
.get(raw_content_url) | ||||||
.send() | ||||||
.expect("Failed to call the folder github API") | ||||||
.text() | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.