11use crate :: args:: { Backend , ShellTrust } ;
22use crate :: config:: { Config , Content , Option , OptionValue , Template , VariableDefinition } ;
3+ use crate :: error:: Error ;
34use async_trait:: async_trait;
45use std:: collections:: { BTreeMap , HashMap } ;
56use std:: env;
@@ -58,14 +59,24 @@ pub async fn render(
5859 _: & mut handlebars:: RenderContext ,
5960 out : & mut dyn handlebars:: Output |
6061 -> handlebars:: HelperResult {
61- let param = h. param ( 0 ) . unwrap ( ) ;
62+ let param = h
63+ . param ( 0 )
64+ . ok_or_else ( || Error :: Helper ( "no parameter to helper function found" . into ( ) ) )
65+ . unwrap ( ) ;
6266 // dbg!(param);
6367 let cmd = helper. 1 . shell . to_owned ( ) ;
6468
6569 let output = std:: process:: Command :: new ( "sh" )
6670 . arg ( "-c" )
6771 . arg ( cmd)
68- . env ( "VALUE" , param. value ( ) . as_str ( ) . unwrap ( ) )
72+ . env (
73+ "VALUE" ,
74+ param
75+ . value ( )
76+ . as_str ( )
77+ . ok_or_else ( || Error :: Helper ( "parameter is not a string" . into ( ) ) )
78+ . unwrap ( ) ,
79+ )
6980 . output ( ) ?;
7081 if output. status . code ( ) . unwrap ( ) != 0 {
7182 return Err ( handlebars:: RenderError :: new ( "failed to get command status" ) ) ;
@@ -78,8 +89,7 @@ pub async fn render(
7889 }
7990 }
8091
81- let rendered_template = hb. render_template ( template, & values_json) . unwrap ( ) ;
82- Ok ( rendered_template)
92+ Ok ( hb. render_template ( template, & values_json) ?)
8393}
8494
8595#[ allow( clippy:: needless_lifetimes) ]
@@ -105,7 +115,7 @@ pub async fn select_template<'a>(
105115
106116 match config. templates . get ( & selection) {
107117 | Some ( x) => Ok ( x) ,
108- | None => Err ( Box :: new ( crate :: error :: Failed :: default ( ) ) ) ,
118+ | None => Err ( Box :: new ( Error :: Generic ( "invalid template selection" . into ( ) ) ) ) ,
109119 }
110120}
111121
@@ -132,7 +142,10 @@ pub async fn select_and_render(
132142 let cfg: Config = serde_yaml:: from_str ( & invoke_options. configuration ) . unwrap ( ) ;
133143
134144 let template = match & invoke_options. template {
135- | Some ( x) => cfg. templates . get ( x) . unwrap ( ) ,
145+ | Some ( x) => cfg
146+ . templates
147+ . get ( x)
148+ . ok_or_else ( || Error :: Generic ( "template not found" . into ( ) ) ) ?,
136149 | None => select_template ( & cfg, & invoke_options. backend , & invoke_options. shell_trust ) . await ?,
137150 } ;
138151 let template_str = match & template. content {
@@ -210,7 +223,7 @@ async fn shell(
210223 shell_trust : & ShellTrust ,
211224) -> Result < String , Box < dyn std:: error:: Error > > {
212225 match shell_trust {
213- | ShellTrust :: None => return Err ( Box :: new ( crate :: error :: NoShellTrust :: default ( ) ) ) ,
226+ | ShellTrust :: None => return Err ( Box :: new ( Error :: NoTrust ) ) ,
214227 | ShellTrust :: Ultimate => { } ,
215228 }
216229
@@ -220,7 +233,7 @@ async fn shell(
220233 . envs ( env)
221234 . output ( ) ?;
222235 if output. status . code ( ) . unwrap ( ) != 0 {
223- return Err ( Box :: new ( crate :: error :: Failed :: default ( ) ) ) ;
236+ return Err ( Box :: new ( Error :: ShellCommand ( command . into ( ) ) ) ) ;
224237 }
225- Ok ( String :: from_utf8 ( output. stdout ) . unwrap ( ) )
238+ Ok ( String :: from_utf8 ( output. stdout ) ? )
226239}
0 commit comments