gitcraft 0.1.123

A template project for GitHub-related utilities.
use colored::*;

use crate::utils::get_comment;
use crate::utils::manifest_navigator::ManifestNavigator;
use crate::utils::remote::Fetcher;

use super::GITHUB_RAW_BASE;

#[derive(clap::Args, Clone, Debug)]
pub struct ListArgs {
    // You can add options here if needed in the future
}

impl super::Runnable for ListArgs {
    fn run(&self) -> anyhow::Result<()> {
        list_all_templates()
    }
}

fn list_all_templates() -> anyhow::Result<()> {
    let fetcher = Fetcher::new();

    let manifest_url = format!("{}/issue-templates/manifest.yml", GITHUB_RAW_BASE);
    let manifest_navigator = ManifestNavigator::new(&manifest_url)?;
    let template_entries = manifest_navigator.list_entries()?;

    for entry in template_entries {
        let file_url = &entry.full_url;
        let extension = std::path::Path::new(file_url)
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("");
        let comment = match fetcher.fetch_content(file_url) {
            Ok(text) => text
                .lines()
                .next()
                .and_then(|line| get_comment::extract_comment(line, extension)),
            _ => None,
        };

        let file_name = std::path::Path::new(&entry.name)
            .file_stem()
            .and_then(|stem| stem.to_str())
            .unwrap_or("");

        println!(
            "{} {} - {}",
            ">".green(),
            file_name,
            comment.unwrap_or_default()
        );
    }
    Ok(())
}