From 348dc926bdd098e238f136788e42de0f6106ea70 Mon Sep 17 00:00:00 2001 From: diversable Date: Tue, 14 Feb 2023 16:10:11 -0800 Subject: [PATCH 1/8] feat: add ion template inspect cmd --- src/bin/ion/commands/template.rs | 27 ++++++++- src/ion/blueprints/utils.rs | 94 +++++++++++++++++++++++++++++++- tests/bin/mod.rs | 1 + tests/bin/template.rs | 22 ++++++++ 4 files changed, 141 insertions(+), 3 deletions(-) create mode 100644 tests/bin/template.rs diff --git a/src/bin/ion/commands/template.rs b/src/bin/ion/commands/template.rs index e6260d5..34feae4 100644 --- a/src/bin/ion/commands/template.rs +++ b/src/bin/ion/commands/template.rs @@ -1,6 +1,8 @@ use clap::parser::ArgMatches; -use clap::Command; -use ion::blueprints::list_templates; +use clap::{arg, Command}; +use ion::blueprints::{ + ask_inspect_template, inspect_all_templates, inspect_template, list_templates, +}; use ion::config::Config; use ion::errors::CliResult; use ion::template::RemoteTemplate; @@ -10,6 +12,12 @@ pub fn cli() -> Command { .about("template management") .subcommand(Command::new("list").about("list all available templates")) .subcommand(Command::new("update").about("update the templates from registry")) + .subcommand( + Command::new("inspect") + .about("inspect the contents of a template") + .arg(arg!([TEMPLATE] "Selects which template to print out")) + .arg(arg!(--"all" "Inspect all installed templates")), + ) .arg_required_else_help(true) } @@ -19,6 +27,21 @@ pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult { Some(("update", _)) => { RemoteTemplate::new(config).download()?; } + Some(("inspect", matches)) => { + // Iff a template name is provided, inspect template; otherwise, check for --all flag; if no --all, ask user to select template from list + + match matches.get_one::("TEMPLATE") { + Some(template) => inspect_template(config, template.to_owned())?, + None => { + let all_flag = matches.get_flag("all"); + if all_flag { + inspect_all_templates(config)?; + } else { + ask_inspect_template(config)?; + } + } + }; + } _ => unreachable!(), } Ok(()) diff --git a/src/ion/blueprints/utils.rs b/src/ion/blueprints/utils.rs index 07e9d8f..e98248b 100644 --- a/src/ion/blueprints/utils.rs +++ b/src/ion/blueprints/utils.rs @@ -1,7 +1,7 @@ use crate::blueprints::*; use crate::spec::Author; use anyhow::{format_err, Error, Result}; -use dialoguer::{Confirm, Input}; +use dialoguer::{theme::ColorfulTheme, Confirm, Input, Select}; pub fn git_get_user() -> Result<(String, String)> { let user = if let Some(name) = git_config_get("user.name") { @@ -112,6 +112,98 @@ pub fn list_templates(config: &Config) -> Result<()> { Ok(()) } +pub fn inspect_template(config: &Config, template_name: String) -> Result<()> { + let templates = config.template_dir().read_dir()?; + + for entry in templates { + let entry = match entry { + Ok(e) => e, + Err(e) => { + return Err(Error::new(e)); + } + }; + + let path = entry.path(); + if path.is_dir() { + let source = std::fs::read_to_string(path.join("template.toml"))?; + + let template = match toml::from_str::