Skip to content

Commit

Permalink
Feat: implement --verbose flag on ion template inspect cmd (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
diversable authored Feb 22, 2023
1 parent d7826f5 commit c6e0e1c
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Download tarball in the release page and extract it to your `$HOME/.julia` direc

### build from source

Using [`just`](https://github.com/casey/just):
Using [`just`](https://github.com/casey/just) and [Rust's cargo/rustc compiler](https://rustup.rs/):

```bash
just install
Expand Down
2 changes: 2 additions & 0 deletions ion_derive/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ pub fn emit_template(ast: &DeriveInput) -> TokenStream {
#validate
Ok(())
}

}

};
gen.into()
}
17 changes: 11 additions & 6 deletions src/bin/ion/commands/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ pub fn cli() -> Command {
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(arg!(--"all" "Inspect all installed templates"))
.arg(arg!(verbose: -v --verbose "Inspect details of the template output")),
)
.arg_required_else_help(true)
}
Expand All @@ -43,17 +44,21 @@ pub fn exec(config: &mut Config, matches: &ArgMatches) -> CliResult {
RemoteTemplate::new(config).download()?;
}
Some(("inspect", matches)) => {
let all_flag = matches.get_flag("all");
let verbose_flag = matches.get_flag("verbose");

download_templates(config)?;
// Iff a template name is provided, inspect template; otherwise, check for --all flag; if no --all, ask user to select template from list

// 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::<String>("TEMPLATE") {
Some(template) => inspect_template(config, template.to_owned())?,
Some(template) => {
inspect_template(config, template.to_owned(), verbose_flag)?;
}
None => {
let all_flag = matches.get_flag("all");
if all_flag {
inspect_all_templates(config)?;
inspect_all_templates(config, verbose_flag)?;
} else {
ask_inspect_template(config)?;
ask_inspect_template(config, verbose_flag)?;
}
}
};
Expand Down
1 change: 0 additions & 1 deletion src/ion/blueprints/components/documenter.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::blueprints::*;
use crate::utils::*;
use crate::PackageSpec;
use anyhow::Ok;
use serde_derive::{Deserialize, Serialize};

#[derive(Debug, Serialize, Clone)]
Expand Down
1 change: 1 addition & 0 deletions src/ion/blueprints/components/license.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::blueprints::*;
use chrono::Datelike;
use dialoguer::Input;
use serde_derive::{Deserialize, Serialize};

use std::path::PathBuf;

#[derive(Debug, Serialize, Clone)]
Expand Down
1 change: 1 addition & 0 deletions src/ion/blueprints/components/project_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use dialoguer::Confirm;
use julia_semver::Version;
use log::debug;
use serde_derive::{Deserialize, Serialize};

use uuid::Uuid;

#[derive(Debug, Serialize, Clone)]
Expand Down
1 change: 1 addition & 0 deletions src/ion/blueprints/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::badge::Badge;
use super::components::*;
use super::Blueprint;
use super::{Julia, Project};

use ion_derive::Template;
use serde_derive::Deserialize;

Expand Down
66 changes: 45 additions & 21 deletions src/ion/blueprints/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn list_templates(config: &Config) -> Result<()> {
Ok(())
}

pub fn inspect_template(config: &Config, template_name: String) -> Result<()> {
pub fn inspect_template(config: &Config, template_name: String, verbose_flag: bool) -> Result<()> {
let templates = config.template_dir().read_dir()?;

let mut template_found: bool = false;
Expand All @@ -136,21 +136,31 @@ pub fn inspect_template(config: &Config, template_name: String) -> Result<()> {
}
};
if template.name == template_name {
template_found = true;
println!("{source}");
// If there's no verbose flag (default), print the source, otherwise, display the full template details (verbose true)
if verbose_flag {
template_found = true;
println!("{:#?}", template);
} else {
template_found = true;
println!("{source}");
}
}
}
}

// If the template the user requested is not in the list of downloaded templates, ask user to select existing template to inspect
if !template_found {
println!("The {template_name} template was not found.\nInstalled templates are:");
ask_inspect_template(config)?

ask_inspect_template(config, verbose_flag)?
}
Ok(())
}

pub fn inspect_all_templates(config: &Config) -> Result<()> {
pub fn ask_inspect_template(config: &Config, verbose_flag: bool) -> Result<()> {
// Get selection options from installed templates
let mut selection_options = vec![];

let templates = config.template_dir().read_dir()?;

for entry in templates {
Expand All @@ -165,17 +175,37 @@ pub fn inspect_all_templates(config: &Config) -> Result<()> {
if path.is_dir() {
let source = std::fs::read_to_string(path.join("template.toml"))?;

println!("\n{source}\n**********");
let template = match toml::from_str::<Template>(&source) {
Ok(t) => t,
Err(e) => {
return Err(format_err!("Error parsing template: {}", e));
}
};

selection_options.push(template.name);
}
}

// Ask for template to print to console
let template_name = Select::with_theme(&ColorfulTheme::default())
.items(&selection_options)
.default(1)
.interact_opt()?;

if let Some(template_name) = template_name {
inspect_template(
config,
selection_options[template_name].to_owned(),
verbose_flag,
)?
};

Ok(())
}

pub fn ask_inspect_template(config: &Config) -> Result<()> {
// Get selection options from installed templates
let mut selection_options = vec![];

pub fn inspect_all_templates(config: &Config, verbose_flag: bool) -> Result<()> {
let templates = config.template_dir().read_dir()?;

for entry in templates {
let entry = match entry {
Ok(e) => e,
Expand All @@ -195,20 +225,14 @@ pub fn ask_inspect_template(config: &Config) -> Result<()> {
}
};

selection_options.push(template.name);
if verbose_flag {
println!("\n{:#?}\n**********", template);
} else {
println!("\n{source}\n**********");
}
}
}

// Ask for template to print to console
let template_name = Select::with_theme(&ColorfulTheme::default())
.items(&selection_options)
.default(1)
.interact_opt()?;

if let Some(template_name) = template_name {
inspect_template(config, selection_options[template_name].to_owned())?
};

Ok(())
}

Expand Down
53 changes: 52 additions & 1 deletion tests/bin/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,64 @@ fn test_ctrl_c() -> Result<()> {
Ok(())
}

#[test]
fn test_verbose() -> Result<()> {
// Test --verbose flag
let mut p = Ion::new()
.arg("template")
.arg("inspect")
.arg("--verbose")
.arg("package")
.spawn(Some(5_000))?;

p.exp_string("template")?;
p.exp_eof()?;

Ok(())
}

#[test]
fn test_verbose_all() -> Result<()> {
// Test --verbose --all flags together
let mut p = Ion::new()
.arg("template")
.arg("inspect")
.arg("--verbose")
.arg("--all")
.spawn(Some(5_000))?;

p.exp_string("template")?;
p.exp_string("template")?;
p.exp_string("template")?;
p.exp_eof()?;

Ok(())
}

#[test]
fn test_verbose_user_input() -> Result<()> {
// Test --verbose flag, ask for user selection
let mut p = Ion::new()
.arg("template")
.arg("inspect")
.arg("--verbose")
.spawn(Some(10_000))?;

// Send <ENTER> keycode
p.send_control('j')?;
p.exp_string("template")?;
p.exp_eof()?;

Ok(())
}

#[test]
fn test_nonce() -> Result<()> {
let mut p = Ion::new()
.arg("template")
.arg("inspect")
.arg("nonce")
.spawn(Some(5_000))?;
.spawn(Some(15_000))?;

p.send_control('j')?; // skip download if there is
p.exp_string("Installed templates are:")?;
Expand Down

0 comments on commit c6e0e1c

Please sign in to comment.