11use std:: collections:: HashMap ;
22use std:: path:: PathBuf ;
33
4+ use bindle_utils:: BindleConnectionInfo ;
45use bindle_writer:: BindleWriter ;
56use clap:: { App , Arg , ArgMatches } ;
67use expander:: { ExpansionContext , InvoiceVersioning } ;
@@ -185,8 +186,7 @@ async fn prepare(args: &ArgMatches) -> anyhow::Result<()> {
185186 let output_format = OutputFormat :: parse ( args. value_of ( ARG_OUTPUT ) . unwrap ( ) ) ;
186187
187188 // NOTE: Prepare currently does not require a Bindle URL, so this could be NoPush(None)
188- let bindle_settings =
189- BindleSettings :: NoPush ( args. value_of ( ARG_BINDLE_URL ) . map ( |s| s. to_owned ( ) ) ) ;
189+ let bindle_settings = BindleSettings :: NoPush ( BindleConnectionInfo :: from_args ( args) ) ;
190190
191191 run (
192192 & source,
@@ -221,11 +221,7 @@ async fn bindle(args: &ArgMatches) -> anyhow::Result<()> {
221221 let invoice_versioning = InvoiceVersioning :: parse ( args. value_of ( ARG_VERSIONING ) . unwrap ( ) ) ;
222222 let output_format = OutputFormat :: parse ( args. value_of ( ARG_OUTPUT ) . unwrap ( ) ) ;
223223 let bindle_settings = BindleSettings :: Push (
224- args. value_of ( ARG_BINDLE_URL )
225- . map ( |s| s. to_owned ( ) )
226- . ok_or_else ( || {
227- anyhow:: anyhow!( "Bindle URL is required. Use -s|--server or $BINDLE_URL" )
228- } ) ?,
224+ BindleConnectionInfo :: from_args ( args) . ok_or_else ( bindle_url_is_required) ?,
229225 ) ;
230226
231227 run (
@@ -257,10 +253,9 @@ async fn push(args: &ArgMatches) -> anyhow::Result<()> {
257253 let output_format = OutputFormat :: parse ( output_format_arg) ;
258254
259255 // Bindle configuration
260- let bindle_url = args. value_of ( ARG_BINDLE_URL ) . map ( |s| s. to_owned ( ) ) ;
261- let bindle_settings = BindleSettings :: Push ( bindle_url. ok_or_else ( || {
262- anyhow:: anyhow!( "Bindle URL must be set for this action. Use -s|--server or $BINDLE_URL" )
263- } ) ?) ;
256+ let bindle_settings = BindleSettings :: Push (
257+ BindleConnectionInfo :: from_args ( args) . ok_or_else ( bindle_url_is_required) ?,
258+ ) ;
264259
265260 // Hippo configuration
266261 let hippo_url = args
@@ -308,7 +303,7 @@ async fn run(
308303 . to_path_buf ( ) ;
309304
310305 // Do this outside the `expand` function so `expand` is more testable
311- let external_invoices = prefetch_required_invoices ( & spec, bindle_settings. bindle_url ( ) ) . await ?;
306+ let external_invoices = prefetch_required_invoices ( & spec, bindle_settings. connection_info ( ) ) . await ?;
312307
313308 let expansion_context = ExpansionContext {
314309 relative_to : source_dir. clone ( ) ,
@@ -321,10 +316,10 @@ async fn run(
321316 let writer = BindleWriter :: new ( & source_dir, & destination) ;
322317 writer. write ( & invoice) . await ?;
323318
324- if let BindleSettings :: Push ( url ) = & & bindle_settings {
325- bindle_pusher:: push_all ( & destination, & invoice. bindle . id , & url ) . await ?;
326- if let Some ( hippo_url ) = & notify_to {
327- hippo_notifier:: register ( & invoice. bindle . id , & hippo_url ) . await ?;
319+ if let BindleSettings :: Push ( bindle_connection ) = & bindle_settings {
320+ bindle_pusher:: push_all ( & destination, & invoice. bindle . id , bindle_connection ) . await ?;
321+ if let Some ( hippo_connection ) = & notify_to {
322+ hippo_notifier:: register ( & invoice. bindle . id , hippo_connection ) . await ?;
328323 }
329324 }
330325
@@ -351,7 +346,7 @@ async fn run(
351346/// Pre-fetch any invoices that are referenced in the HIPPOFACTS.
352347async fn prefetch_required_invoices (
353348 hippofacts : & HippoFacts ,
354- bindle_url : Option < String > ,
349+ bindle_client_factory : Option < & BindleConnectionInfo > ,
355350) -> anyhow:: Result < HashMap < bindle:: Id , bindle:: Invoice > > {
356351 let mut map = HashMap :: new ( ) ;
357352
@@ -364,10 +359,9 @@ async fn prefetch_required_invoices(
364359 return Ok ( map) ;
365360 }
366361
367- let base_url = bindle_url . as_ref ( ) . ok_or_else ( || {
362+ let client = bindle_client_factory . as_ref ( ) . ok_or_else ( || {
368363 anyhow:: anyhow!( "Spec file contains external references but Bindle server URL is not set" )
369- } ) ?;
370- let client = bindle:: client:: Client :: new ( base_url) ?;
364+ } ) ?. client ( ) ?;
371365
372366 for external_ref in external_refs {
373367 let invoice = client. get_yanked_invoice ( & external_ref) . await ?;
@@ -400,6 +394,10 @@ fn sourcedir(hippofacts_arg: &str) -> anyhow::Result<PathBuf> {
400394 Ok ( source)
401395}
402396
397+ fn bindle_url_is_required ( ) -> anyhow:: Error {
398+ anyhow:: anyhow!( "Bindle URL is required. Use -s|--server or $BINDLE_URL" )
399+ }
400+
403401/// Describe the desired output format.
404402enum OutputFormat {
405403 None ,
@@ -423,17 +421,25 @@ impl OutputFormat {
423421/// Desribe the actions to be taken viz a viz a Bindle server.
424422enum BindleSettings {
425423 /// Do not push to a Bindle server, but still resolve local references.
426- NoPush ( Option < String > ) ,
424+ NoPush ( Option < BindleConnectionInfo > ) ,
427425 /// Push to a Bindle server, resolving references if necessary.
428- Push ( String ) ,
426+ Push ( BindleConnectionInfo ) ,
429427}
430428
431429impl BindleSettings {
432430 /// Get the Bindle server URL if it was set.
433- pub fn bindle_url ( & self ) -> Option < String > {
431+ pub fn connection_info ( & self ) -> Option < & BindleConnectionInfo > {
434432 match self {
435- Self :: NoPush ( opt) => opt. clone ( ) ,
436- Self :: Push ( url ) => Some ( url . clone ( ) ) ,
433+ Self :: NoPush ( opt) => opt. as_ref ( ) ,
434+ Self :: Push ( conn ) => Some ( conn ) ,
437435 }
438436 }
439437}
438+
439+ impl BindleConnectionInfo {
440+ pub fn from_args ( args : & ArgMatches ) -> Option < Self > {
441+ let allow_insecure = args. is_present ( ARG_INSECURE ) ;
442+ args. value_of ( ARG_BINDLE_URL )
443+ . map ( |base_url| Self :: new ( base_url, allow_insecure) )
444+ }
445+ }
0 commit comments