Skip to content

Commit

Permalink
- removed helper flag and old files
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Weber committed Mar 4, 2023
1 parent 1157019 commit 21f4f7a
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 120 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

48 changes: 0 additions & 48 deletions .vscode/launch.json

This file was deleted.

39 changes: 10 additions & 29 deletions src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@ pub struct CallArgs {
impl CallArgs {
#[allow(clippy::single_match)]
pub async fn validate(&self) -> Result<(), Box<dyn std::error::Error>> {
match &self.command {
| Command::Render(args) => {
if args.helpers && args.shell_trust != ShellTrust::Ultimate {
return Err(Box::new(Error::NoTrust));
}
},
| _ => {},
}

match self.privileges {
| Privilege::Normal => match &self.command {
| Command::Render(args) => {
Expand All @@ -34,9 +25,6 @@ impl CallArgs {
if args.value_overrides.len() > 0 {
return Err(Box::new(Error::ExperimentalCommand));
}
if args.helpers {
return Err(Box::new(Error::ExperimentalCommand));
}
#[allow(unreachable_code)]
Ok(())
},
Expand All @@ -61,8 +49,8 @@ pub enum ManualFormat {

#[derive(Debug)]
pub enum Command {
Manual(String, ManualFormat),
Autocomplete(String, clap_complete::Shell),
Manual { path: String, format: ManualFormat },
Autocomplete { path: String, shell: clap_complete::Shell },
Init,
Render(RenderArguments),
}
Expand All @@ -72,7 +60,6 @@ pub struct RenderArguments {
pub configuration: String,
pub template: Option<String>,
pub value_overrides: std::collections::HashMap<String, String>,
pub helpers: bool,
pub shell_trust: ShellTrust,
pub loose: bool,
pub backend: Backend,
Expand Down Expand Up @@ -161,10 +148,6 @@ impl ClapArgumentLoader {
.long("loose")
.action(ArgAction::SetTrue)
.help("Defines that the templating is done in non-strict mode (allow missing value for variable)."))
.arg(clap::Arg::new("helpers")
.long("helpers")
.action(ArgAction::SetTrue)
.help("Enables handlebar helpers."))
.arg(clap::Arg::new("backend")
.short('b')
.long("backend")
Expand All @@ -189,22 +172,22 @@ impl ClapArgumentLoader {

if let Some(subc) = command_matches.subcommand_matches("man") {
Ok(CallArgs {
command: Command::Manual(
subc.get_one::<String>("out").unwrap().into(),
match subc.get_one::<String>("format").unwrap().as_str() {
command: Command::Manual {
path: subc.get_one::<String>("out").unwrap().into(),
format: match subc.get_one::<String>("format").unwrap().as_str() {
| "manpages" => ManualFormat::Manpages,
| "markdown" => ManualFormat::Markdown,
| _ => return Err(Box::new(Error::Argument("unknown format".into()))),
},
),
},
privileges,
})
} else if let Some(subc) = command_matches.subcommand_matches("autocomplete") {
Ok(CallArgs {
command: Command::Autocomplete(
subc.get_one::<String>("out").unwrap().into(),
clap_complete::Shell::from_str(subc.get_one::<String>("shell").unwrap().as_str()).unwrap(),
),
command: Command::Autocomplete {
path: subc.get_one::<String>("out").unwrap().into(),
shell: clap_complete::Shell::from_str(subc.get_one::<String>("shell").unwrap().as_str()).unwrap(),
},
privileges,
})
} else if let Some(..) = command_matches.subcommand_matches("init") {
Expand Down Expand Up @@ -236,7 +219,6 @@ impl ClapArgumentLoader {
| "ui" => Backend::UI,
| _ => return Err(Box::new(Error::Argument("no backend specified".into()))),
};
let helpers = subc.get_flag("helpers");

Ok(CallArgs {
privileges,
Expand All @@ -247,7 +229,6 @@ impl ClapArgumentLoader {
shell_trust,
loose,
backend,
helpers,
}),
})
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) enum Error {
#[error("unknown command")]
UnknownCommand,
#[error("no trust")]
NoTrust,
NoTrust(String),
#[error("shell command")]
ShellCommand(String),
#[error("user interact abort")]
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
cmd.validate().await?;

match cmd.command {
| crate::args::Command::Manual(path, format) => {
| crate::args::Command::Manual { path, format } => {
let out_path = PathBuf::from(path);
std::fs::create_dir_all(&out_path)?;
match format {
Expand All @@ -31,7 +31,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}
Ok(())
},
| crate::args::Command::Autocomplete(path, shell) => {
| crate::args::Command::Autocomplete { path, shell } => {
let out_path = PathBuf::from(path);
std::fs::create_dir_all(&out_path)?;
reference::build_shell_completion(&out_path, &shell)?;
Expand Down
81 changes: 44 additions & 37 deletions src/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,42 +49,45 @@ pub async fn render(
hb.register_escape_fn(|s| s.to_owned());
hb.set_strict_mode(!args.loose);

if args.helpers {
for helper in helpers {
let h_func = move |h: &handlebars::Helper,
_: &handlebars::Handlebars,
_: &handlebars::Context,
_: &mut handlebars::RenderContext,
out: &mut dyn handlebars::Output|
-> handlebars::HelperResult {
let param = h
.param(0)
.ok_or_else(|| Error::Helper("no parameter to helper function found".into()))
.unwrap();
// dbg!(param);
let cmd = helper.1.shell.to_owned();

let output = std::process::Command::new("sh")
.arg("-c")
.arg(cmd)
.env(
"VALUE",
param
.value()
.as_str()
.ok_or_else(|| Error::Helper("parameter is not a string".into()))
.unwrap(),
)
.output()?;
if output.status.code().unwrap() != 0 {
return Err(handlebars::RenderError::new("failed to get command status"));
}

out.write(String::from_utf8(output.stdout).unwrap().as_str())?;
Ok(())
};
hb.register_helper(helper.0, Box::new(h_func))
}
if helpers.len() > 0 && args.shell_trust != ShellTrust::Ultimate {
return Err(Box::new(Error::NoTrust(
"need trust for executing helper functions".into(),
)));
}
for helper in helpers {
let h_func = move |h: &handlebars::Helper,
_: &handlebars::Handlebars,
_: &handlebars::Context,
_: &mut handlebars::RenderContext,
out: &mut dyn handlebars::Output|
-> handlebars::HelperResult {
let param = h
.param(0)
.ok_or_else(|| Error::Helper("no parameter to helper function found".into()))
.unwrap();
// dbg!(param);
let cmd = helper.1.shell.to_owned();

let output = std::process::Command::new("sh")
.arg("-c")
.arg(cmd)
.env(
"VALUE",
param
.value()
.as_str()
.ok_or_else(|| Error::Helper("parameter is not a string".into()))
.unwrap(),
)
.output()?;
if output.status.code().unwrap() != 0 {
return Err(handlebars::RenderError::new("failed to get command status"));
}

out.write(String::from_utf8(output.stdout).unwrap().as_str())?;
Ok(())
};
hb.register_helper(helper.0, Box::new(h_func))
}

Ok(hb.render_template(template, &values_json)?)
Expand Down Expand Up @@ -221,7 +224,11 @@ async fn shell(
shell_trust: &ShellTrust,
) -> Result<String, Box<dyn std::error::Error>> {
match shell_trust {
| ShellTrust::None => return Err(Box::new(Error::NoTrust)),
| ShellTrust::None => {
return Err(Box::new(Error::NoTrust(
"need trust for executing shell commands".into(),
)))
},
| ShellTrust::Ultimate => {},
}

Expand Down

0 comments on commit 21f4f7a

Please sign in to comment.