Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -500,3 +500,9 @@ StarForge has comprehensive documentation covering all aspects of the project:

For a complete overview, see [DOCUMENTATION_INDEX.md](DOCUMENTATION_INDEX.md).


# Remove a template
starforge template remove my-template

# Remove template + delete all local files
starforge template remove my-template --purge
17 changes: 14 additions & 3 deletions src/commands/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ pub enum TemplateCommands {
Remove {
/// Template name
name: String,

/// Also delete cached files and downloaded assets
#[arg(long)]
purge: bool,
},
/// Initialize the template registry with example templates
Init,
Expand Down Expand Up @@ -198,6 +202,8 @@ pub fn handle(cmd: TemplateCommands) -> Result<()> {
force,
} => install(source, name, version, force),
TemplateCommands::Update { name, all } => update(name, all),
// In the match arm
TemplateCommands::Remove { name, purge } => remove(name, purge),
}
}

Expand Down Expand Up @@ -539,9 +545,14 @@ fn print_quality_signals(template: &templates::TemplateEntry) {
}
}

fn remove(name: String) -> Result<()> {
templates::remove_template(&name)?;
p::success(&format!("Template '{}' removed", name));
fn remove(name: String, purge: bool) -> Result<()> {
templates::remove_template(&name, purge)?;

if purge {
p::success(&format!("Template '{}' and all local assets removed", name));
} else {
p::success(&format!("Template '{}' removed from registry (use --purge to also delete cached files)", name));
}
Ok(())
}

Expand Down
34 changes: 33 additions & 1 deletion src/utils/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -792,16 +792,48 @@ pub fn add_template(entry: TemplateEntry) -> Result<()> {
Ok(())
}

pub fn remove_template(name: &str) -> Result<()> {
/// Remove a template from the registry.
/// If `purge` is true, also deletes any cached/downloaded assets.
pub fn remove_template(name: &str, purge: bool) -> Result<()> {
let mut registry = load_registry()?;
let before = registry.templates.len();

registry.templates.retain(|t| t.name != name);

if registry.templates.len() == before {
anyhow::bail!("Template '{}' not found in registry", name);
}

save_registry(&registry)?;

// Purge local assets if requested
if purge {
purge_template_assets(name)?;
}

Ok(())
}

/// Delete all local cached and stored assets for a template
fn purge_template_assets(name: &str) -> Result<()> {
// 1. Purge from template storage directory
if let Ok(storage_dir) = template_storage_dir() {
let template_path = storage_dir.join(name);
if template_path.exists() {
fs::remove_dir_all(&template_path)
.with_context(|| format!("Failed to purge stored template at {}", template_path.display()))?;
}
}

// 2. Purge from cache directory
if let Ok(cache_dir) = template_cache_dir() {
let cache_path = cache_dir.join(name);
if cache_path.exists() {
fs::remove_dir_all(&cache_path)
.with_context(|| format!("Failed to purge cached template at {}", cache_path.display()))?;
}
}

Ok(())
}

Expand Down