Skip to content

Commit

Permalink
fix: Manage template file when path is defined (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
taorepoara authored May 15, 2024
1 parent 48eca6b commit 3e9376e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
17 changes: 7 additions & 10 deletions src/lenra.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
fs,
path::{Path, PathBuf},
};
use std::{fs, path::PathBuf};

use rustyline::Editor;

Expand Down Expand Up @@ -127,14 +124,15 @@ pub async fn update_env_images(
pub async fn upgrade_app(context: &mut CommandContext) -> Result<()> {
log::info!("Upgrading the application");
// get template data
let template_data = template::get_template_data().await?;
let git_dir = Path::new(LENRA_CACHE_DIRECTORY).join(template::TEMPLATE_GIT_DIR);
let template_data = template::get_template_data(context).await?;
let cache_dir = context.resolve_path(&PathBuf::from(LENRA_CACHE_DIRECTORY));
let git_dir = cache_dir.join(template::TEMPLATE_GIT_DIR);

if git_dir.is_dir() {
// update the template repo
git::pull(Some(git_dir.clone())).await?;
} else {
let template_tmp = Path::new(LENRA_CACHE_DIRECTORY).join(template::TEMPLATE_TEMP_DIR);
let template_tmp = cache_dir.join(template::TEMPLATE_TEMP_DIR);
// clone template project
template::clone_template(template_data.template.as_str(), &template_tmp).await?;
fs::rename(template_tmp.join(".git"), git_dir.clone())?;
Expand All @@ -149,8 +147,7 @@ pub async fn upgrade_app(context: &mut CommandContext) -> Result<()> {
}

// get diff between previous commit and current commit
let patch_file = Path::new(LENRA_CACHE_DIRECTORY)
.join(format!("patch.{}-{}.diff", commit, current_commit));
let patch_file = cache_dir.join(format!("patch.{}-{}.diff", commit, current_commit));
log::debug!(
"create patch between {} and {}: {:?}",
commit,
Expand Down Expand Up @@ -218,7 +215,7 @@ pub async fn upgrade_app(context: &mut CommandContext) -> Result<()> {
template: template_data.template,
commit: Some(current_commit),
}
.save()
.save(context)
.await
}

Expand Down
12 changes: 7 additions & 5 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::{
};

use crate::{
cli::CommandContext,
command::{get_command_output, run_command},
config::LENRA_CACHE_DIRECTORY,
errors::{Error, Result},
Expand Down Expand Up @@ -47,8 +48,8 @@ pub struct TemplateData {

#[cfg_attr(test, mockable)]
impl TemplateData {
pub async fn save(&self) -> Result<()> {
let path = Path::new(TEMPLATE_DATA_FILE);
pub async fn save(&self, context: &mut CommandContext) -> Result<()> {
let path = context.resolve_path(&PathBuf::from(TEMPLATE_DATA_FILE));
self.save_to(&path).await
}

Expand Down Expand Up @@ -129,9 +130,10 @@ pub async fn clone_template(template: &str, target_dir: &PathBuf) -> Result<()>
}

#[cfg_attr(test, mockable)]
pub async fn get_template_data() -> Result<TemplateData> {
let template_data_file = Path::new(TEMPLATE_DATA_FILE);
let git_dir = Path::new(LENRA_CACHE_DIRECTORY).join(TEMPLATE_GIT_DIR);
pub async fn get_template_data(context: &mut CommandContext) -> Result<TemplateData> {
let template_data_file = context.resolve_path(&PathBuf::from(TEMPLATE_DATA_FILE));
let git_dir =
context.resolve_path(&PathBuf::from(LENRA_CACHE_DIRECTORY).join(TEMPLATE_GIT_DIR));
if template_data_file.exists() {
let template_data = fs::read_to_string(template_data_file).map_err(Error::from)?;
let template_data: Vec<&str> = template_data.split("\n").collect();
Expand Down

0 comments on commit 3e9376e

Please sign in to comment.