Skip to content

Commit ced6394

Browse files
committed
fix: Manage template file when path is defined (#404)
1 parent e6f0e21 commit ced6394

2 files changed

Lines changed: 14 additions & 15 deletions

File tree

src/lenra.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
use std::{
2-
fs,
3-
path::{Path, PathBuf},
4-
};
1+
use std::{fs, path::PathBuf};
52

63
use rustyline::Editor;
74

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

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

151149
// get diff between previous commit and current commit
152-
let patch_file = Path::new(LENRA_CACHE_DIRECTORY)
153-
.join(format!("patch.{}-{}.diff", commit, current_commit));
150+
let patch_file = cache_dir.join(format!("patch.{}-{}.diff", commit, current_commit));
154151
log::debug!(
155152
"create patch between {} and {}: {:?}",
156153
commit,
@@ -218,7 +215,7 @@ pub async fn upgrade_app(context: &mut CommandContext) -> Result<()> {
218215
template: template_data.template,
219216
commit: Some(current_commit),
220217
}
221-
.save()
218+
.save(context)
222219
.await
223220
}
224221

src/template.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55
};
66

77
use crate::{
8+
cli::CommandContext,
89
command::{get_command_output, run_command},
910
config::LENRA_CACHE_DIRECTORY,
1011
errors::{Error, Result},
@@ -47,8 +48,8 @@ pub struct TemplateData {
4748

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

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

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

0 commit comments

Comments
 (0)