diff --git a/.phil-project b/.phil-project index 603d8b6..c411e96 100644 --- a/.phil-project +++ b/.phil-project @@ -1,4 +1,4 @@ logo:assets/logo.png logo_small:assets/logo.png description_translate:de -version:0.2.1 +version:0.2.2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e79faf..cc868b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,13 @@ --- +## [v0.2.2](https://github.com/cophilot/templify/tree/0.2.2) (2024-2-5) + +- Added `-force` flag for `load` command +- Added `-version` flag for `update` command + +--- + ## [v0.2.1](https://github.com/cophilot/templify/tree/0.2.1) (2024-2-4) - Refactoring diff --git a/Cargo.lock b/Cargo.lock index dead3c0..0256dda 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -784,7 +784,7 @@ dependencies = [ [[package]] name = "templify" -version = "0.2.1" +version = "0.2.2" dependencies = [ "reqwest", "self-replace", diff --git a/Cargo.toml b/Cargo.toml index 329565d..2e63298 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "templify" -version = "0.2.1" +version = "0.2.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/README.md b/README.md index 1271857..8669ab0 100644 --- a/README.md +++ b/README.md @@ -206,12 +206,10 @@ tpy load https://github.com/cophilot/templify-vault/tree/main/React-ts ## [Release Notes](https://github.com/cophilot/templify/blob/master/CHANGELOG.md) -### [v0.2.1](https://github.com/cophilot/templify/tree/0.2.1) +### [v0.2.2](https://github.com/cophilot/templify/tree/0.2.2) -- Refactoring -- Added `-offline` flag for `init` command -- Added `-description` flag for `new` command -- Added `-path` flag for `new` command +- Added `-force` flag for `load` command +- Added `-version` flag for `update` command --- diff --git a/src/command_storage.rs b/src/command_storage.rs index 957f4bf..0b60ece 100644 --- a/src/command_storage.rs +++ b/src/command_storage.rs @@ -1,4 +1,4 @@ -use crate::commands; +use crate::commands::{self}; use crate::types::{Argument, Command, Flag, Status}; pub fn get_all_commands() -> Vec { @@ -26,12 +26,18 @@ pub fn get_all_commands() -> Vec { // *** update *** - let update_com = Command::new( + let mut update_com = Command::new( vec!["update".to_string()], commands::update, "Update templify to the latest version.".to_string(), ); + update_com.add_flag(Flag::new_value_flag( + vec!["version".to_string(), "v".to_string()], + "".to_string(), + "Update to a specific version.".to_string(), + )); + commands.push(update_com); // *** init *** @@ -93,7 +99,7 @@ pub fn get_all_commands() -> Vec { let mut load_com = Command::new( vec!["load".to_string(), "l".to_string()], commands::load, - "Load templates from a github repository (provide a url that points to an folder in a github repository).".to_string(), + "Load templates from a github repository.".to_string(), ); load_com.add_argument(Argument::new( @@ -103,6 +109,11 @@ pub fn get_all_commands() -> Vec { "The url of the github repository.".to_string(), )); + load_com.add_flag(Flag::new_bool_flag( + vec!["force".to_string(), "f".to_string()], + "Force the load, even if the folder already exists.".to_string(), + )); + commands.push(load_com); // *** generate *** diff --git a/src/commands.rs b/src/commands.rs index 2a0d852..f14ec1a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -54,7 +54,12 @@ pub fn load(command: &Command) -> Status { )); } println!("Loading template from {}...", url); - utils::load_remote_template_dir(".templates", url.as_str(), true); + utils::load_remote_template_dir( + ".templates", + url.as_str(), + command.get_bool_flag("force"), + true, + ); return Status::ok(); } @@ -135,19 +140,28 @@ pub fn new(command: &Command) -> Status { return Status::ok(); } -pub fn update(_command: &Command) -> Status { +pub fn update(command: &Command) -> Status { if !utils::check_internet_connection() { return Status::error("You need a internet connection for this command!".to_string()); } - if !version_control::is_newer_version_available() { + let version = command.get_value_flag("version").clone(); + + if !version_control::is_newer_version_available() && version == "" { println!("templify is already up to date."); return Status::ok(); } - println!("Updating templify..."); + if version != "" { + println!("Updating templify to version {}...", version); + } else { + println!("Updating templify..."); + } - version_control::update().unwrap(); + let st = version_control::update(version); + if st.is_err() { + return Status::error(format!("{}", st.err().unwrap())); + } println!("templify updated successfully."); std::process::exit(0); @@ -181,6 +195,7 @@ pub fn init(command: &Command) -> Status { ".templates", "https://github.com/cophilot/templify-vault/tree/main/Example", true, + true, ); } println!("templify initialized successfully."); @@ -204,5 +219,7 @@ pub fn help(_command: &Command) -> Status { println!("{}", command.to_help_string()); } + println!("To get more information please visit: https://templify.philipp-bonin.com"); + return Status::ok(); } diff --git a/src/utils.rs b/src/utils.rs index 86fadec..4bd562e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -30,8 +30,8 @@ pub fn parse_templify_file(file_path: &str) -> std::collections::HashMap Result<(), Box> { +pub fn update(v: String) -> Result<(), Box> { let mut binary_ending = ""; if env::is_windows() { binary_ending = ".exe"; } + let mut version = v; + + if version == "" { + version = get_latest_version(); + } + + if version.starts_with("v") { + version = version[1..].to_string(); + } + let url = format!( - " - https://github.com/cophilot/templify/releases/download/{}/tpy{}", - get_latest_version(), - binary_ending + "https://github.com/cophilot/templify/releases/download/{}/tpy{}", + version, binary_ending ); // download the new binary and save it somewhere temporarily - let mut response = reqwest::blocking::get(url)?; + let response = reqwest::blocking::get(url).unwrap(); + + // check if the download was successful + if response.status() != 200 { + if response.status() == 404 { + return Err(Box::new(std::io::Error::new( + std::io::ErrorKind::Other, + format!( + "Could not download the new binary. The version {} does not exist.", + version + ), + ))); + } else { + return Err(Box::new(std::io::Error::new( + std::io::ErrorKind::Other, + format!( + "Could not download the new binary. Status code: {}", + response.status() + ), + ))); + } + } + + let mut response = response; let mut dest = { let mut d = std::env::temp_dir(); d.push("tpy"); std::fs::File::create(d)? }; std::io::copy(&mut response, &mut dest)?; + // replace the current binary with the new one let new_binary = format!("{}/tpy", std::env::temp_dir().to_str().unwrap());