Skip to content

Commit da27f1b

Browse files
authored
Merge pull request #134 from RafaelJohn9/feat/saving-with-different-name
Feat/saving with different name
2 parents 21cfc72 + 0031a56 commit da27f1b

7 files changed

Lines changed: 49 additions & 61 deletions

File tree

src/commands/gitignore/add.rs

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,6 @@ fn download_all_templates(
127127

128128
let section = format!("# ===== {} =====\n{}\n\n", remote_filename, content);
129129
file::save_file(&section, &dest_path, force)?;
130-
131-
println!(
132-
"{} Downloaded gitignore template: {} to {}",
133-
"✓".green(),
134-
remote_filename,
135-
dest_path.display()
136-
);
137130
}
138131
} else {
139132
// Merge all templates into a single .gitignore file
@@ -212,13 +205,6 @@ fn download_templates(
212205
} else {
213206
file::save_file(&section, &dest_path, force)?;
214207
}
215-
216-
println!(
217-
"{} Added gitignore template: {} to {}",
218-
"✓".green(),
219-
template_name,
220-
dest_path.display()
221-
);
222208
}
223209
} else if output.len() == templates.len() {
224210
// Save each template to its own file as specified in output
@@ -252,41 +238,57 @@ fn download_templates(
252238
);
253239
}
254240
} else if output.len() == 1 {
255-
// Merge all templates into one file
241+
// Merge all templates into one file, but skip invalid templates and collect errors
256242
let mut merged_content = String::new();
243+
let mut errors = Vec::new();
257244

258245
for template_name in templates {
259-
let template_path = find_template_in_cache(template_name, cache)?;
260-
let url = format!("{}/{}", GITHUB_RAW_BASE, template_path);
261-
262-
let msg = format!("Downloading gitignore template: {}", template_name);
263-
let pb = progress::spinner(&msg);
264-
let content = fetcher.fetch_content(&url)?;
265-
pb.set_message("Download Complete");
266-
pb.finish_and_clear();
267-
268-
merged_content.push_str(&format!(
269-
"# ===== {}.gitignore =====\n{}\n\n",
270-
template_name, content
271-
));
246+
match find_template_in_cache(template_name, cache) {
247+
Ok(template_path) => {
248+
let url = format!("{}/{}", GITHUB_RAW_BASE, template_path);
249+
250+
let msg = format!("Downloading gitignore template: {}", template_name);
251+
let pb = progress::spinner(&msg);
252+
match fetcher.fetch_content(&url) {
253+
Ok(content) => {
254+
pb.set_message("Download Complete");
255+
pb.finish_and_clear();
256+
merged_content.push_str(&format!(
257+
"# ===== {}.gitignore =====\n{}\n\n",
258+
template_name, content
259+
));
260+
}
261+
Err(e) => {
262+
pb.finish_and_clear();
263+
errors.push(format!(
264+
"Failed to fetch template '{}': {}",
265+
template_name, e
266+
));
267+
}
268+
}
269+
}
270+
Err(e) => {
271+
errors.push(format!("Template '{}' not found: {}", template_name, e));
272+
}
273+
}
272274
}
273275

274276
let dest_path = dir_path
275277
.map(|p| p.join(&output[0]))
276278
.unwrap_or_else(|| Path::new(OUTPUT_BASE_PATH).join(OUTPUT).join(&output[0]));
277279

278-
if append {
279-
file::append_file(&merged_content, &dest_path, None)?;
280-
} else {
281-
file::save_file(&merged_content, &dest_path, force)?;
280+
if !merged_content.is_empty() {
281+
if append {
282+
file::append_file(&merged_content, &dest_path, None)?;
283+
} else {
284+
file::save_file(&merged_content, &dest_path, force)?;
285+
}
282286
}
283287

284-
println!(
285-
"{} Added gitignore templates: {} to {}",
286-
"✓".green(),
287-
templates.join(", "),
288-
dest_path.display()
289-
);
288+
// Print errors for invalid templates
289+
for error in errors {
290+
eprintln!("{}", error.red());
291+
}
290292
} else {
291293
return Err(anyhow::anyhow!(
292294
"Number of output files must be either 1 or match the number of templates when not using --use-remote-name"

src/commands/issue/add.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,5 @@ fn download_single_template(
151151

152152
file::save_file(&content, &dest_path, force)?;
153153

154-
println!(
155-
"{} Added template: {} to {}",
156-
"✓".green(),
157-
template_name,
158-
dest_path.display()
159-
);
160-
161154
Ok(())
162155
}

src/commands/license/add.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,6 @@ fn download_single_license(
195195

196196
file::save_file(&processed_text, &dest_path, config.force)?;
197197

198-
println!(
199-
"{} Downloaded and added license: {}",
200-
"✓".green(),
201-
dest_path.display()
202-
);
203-
204198
Ok(())
205199
}
206200

src/commands/pr/add.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,5 @@ fn download_single_template(
165165

166166
file::save_file(&content, &dest_path, force)?;
167167

168-
println!("{} {} - has been added.", "✓".green(), dest_path.display());
169-
170168
Ok(())
171169
}

src/utils/file.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
use anyhow::Result;
21
use std::fs;
3-
use std::fs::OpenOptions;
42
use std::io::Write;
53
use std::path::{Path, PathBuf};
64

5+
use anyhow::Result;
6+
use colored::*;
7+
78
/// Save content to a file with path resolution middleware
89
pub fn save_file(content: &str, filepath: &Path, force: bool) -> Result<()> {
910
let resolved_path = resolve_output_path(filepath)?;
@@ -48,6 +49,8 @@ pub fn save_file(content: &str, filepath: &Path, force: bool) -> Result<()> {
4849
}
4950

5051
fs::write(resolved_path, content)?;
52+
53+
println!("{} {} - has been added.", "✓".green(), display_path);
5154
Ok(())
5255
}
5356

@@ -79,7 +82,7 @@ pub fn append_file(content: &str, filepath: &Path, line_position: Option<usize>)
7982
match line_position {
8083
None => {
8184
// Simple append at the end
82-
let mut file = OpenOptions::new()
85+
let mut file = fs::OpenOptions::new()
8386
.create(true)
8487
.append(true)
8588
.open(&resolved_path)?;

tests/integration/gitignore_tests.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,6 @@ fn test_gitignore_add_update_cache() {
241241
}
242242

243243
#[test]
244-
#[ignore]
245-
// TODO we should support this case forcing a clear separation of adding templates
246244
fn test_gitignore_add_valid_and_invalid_template() {
247245
let temp_dir = setup_test_env();
248246
let temp_path = temp_dir.path().to_path_buf();

tests/integration/license_tests.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ fn test_license_add_with_params() {
5353
])
5454
.assert()
5555
.success()
56-
.stdout(predicate::str::contains("added license"))
56+
.stdout(predicate::str::contains("has been added."))
5757
.stdout(predicate::str::contains("Filled"));
5858

5959
let content = fs::read_to_string(temp_path.join("LICENSE")).unwrap();
@@ -145,7 +145,7 @@ fn test_license_add_multiple_licenses_with_output_files() {
145145
])
146146
.assert()
147147
.success()
148-
.stdout(predicate::str::contains("added license"))
148+
.stdout(predicate::str::contains("has been added."))
149149
.stdout(predicate::str::contains("MIT"))
150150
.stdout(predicate::str::contains("APACHE2-0"));
151151

@@ -208,7 +208,7 @@ fn test_license_add_multiple_licenses_with_params_and_output_files() {
208208
])
209209
.assert()
210210
.success()
211-
.stdout(predicate::str::contains("added license"));
211+
.stdout(predicate::str::contains("has been added."));
212212

213213
let mit_content = fs::read_to_string(&mit_path).unwrap();
214214
let apache_content = fs::read_to_string(&apache_path).unwrap();

0 commit comments

Comments
 (0)