@@ -21,12 +21,12 @@ use anyhow::{bail, Context};
2121use clap:: ArgMatches ;
2222use itertools:: Itertools ;
2323use quickwit_common:: uri:: Uri ;
24- use quickwit_config:: SourceConfig ;
24+ use quickwit_config:: { SourceConfig , SourceParams } ;
2525use quickwit_indexing:: check_source_connectivity;
2626use quickwit_metastore:: checkpoint:: SourceCheckpoint ;
2727use quickwit_metastore:: { quickwit_metastore_uri_resolver, IndexMetadata } ;
2828use quickwit_storage:: load_file;
29- use serde_json:: Value ;
29+ use serde_json:: { Map , Value } ;
3030use tabled:: { Table , Tabled } ;
3131
3232use crate :: { load_quickwit_config, make_table} ;
@@ -184,12 +184,17 @@ async fn add_source_cli(args: AddSourceArgs) -> anyhow::Result<()> {
184184 . resolve ( & config. metastore_uri )
185185 . await ?;
186186 let params = sniff_params ( & args. params ) . await ?;
187+ let mut source_params_json: Map < String , Value > = Map :: new ( ) ;
188+ source_params_json. insert ( "source_type" . to_string ( ) , Value :: String ( args. source_type ) ) ;
189+ source_params_json. insert ( "params" . to_string ( ) , Value :: Object ( params) ) ;
190+ let source_params: SourceParams = serde_json:: from_value ( Value :: Object ( source_params_json) ) ?;
187191 let source = SourceConfig {
188192 source_id : args. source_id . clone ( ) ,
189- source_type : args. source_type ,
190- params,
193+ source_params,
191194 } ;
195+ source. validate ( ) ?;
192196 check_source_connectivity ( & source) . await ?;
197+
193198 metastore. add_source ( & args. index_id , source) . await ?;
194199 println ! (
195200 "Source `{}` successfully created for index `{}`." ,
@@ -244,12 +249,12 @@ where
244249 . with_context ( || format ! ( "Source `{}` does not exist." , source_id) ) ?;
245250
246251 let source_rows = vec ! [ SourceRow {
247- source_id: source. source_id,
248- source_type: source. source_type,
252+ source_id: source. source_id. clone ( ) ,
253+ source_type: source. source_type( ) . to_string ( ) ,
249254 } ] ;
250255 let source_table = make_table ( "Source" , source_rows) ;
251256
252- let params_rows = flatten_json ( source. params )
257+ let params_rows = flatten_json ( source. params ( ) )
253258 . into_iter ( )
254259 . map ( |( key, value) | ParamsRow { key, value } )
255260 . sorted_by ( |left, right| left. key . cmp ( & right. key ) ) ;
@@ -279,8 +284,8 @@ where I: IntoIterator<Item = SourceConfig> {
279284 let rows = sources
280285 . into_iter ( )
281286 . map ( |source| SourceRow {
287+ source_type : source. source_type ( ) . to_string ( ) ,
282288 source_id : source. source_id ,
283- source_type : source. source_type ,
284289 } )
285290 . sorted_by ( |left, right| left. source_id . cmp ( & right. source_id ) ) ;
286291 make_table ( "Sources" , rows)
@@ -346,14 +351,14 @@ fn flatten_json(value: Value) -> Vec<(String, Value)> {
346351
347352/// Tries to read a JSON object from a string, assuming the string is an inline JSON object or a
348353/// path to a file holding a JSON object.
349- async fn sniff_params ( params : & str ) -> anyhow:: Result < Value > {
350- if let Ok ( object @ Value :: Object ( _ ) ) = serde_json:: from_str ( params) {
351- return Ok ( object ) ;
354+ async fn sniff_params ( params : & str ) -> anyhow:: Result < Map < String , Value > > {
355+ if let Ok ( Value :: Object ( values ) ) = serde_json:: from_str ( params) {
356+ return Ok ( values ) ;
352357 }
353358 let params_uri = Uri :: try_new ( params) ?;
354359 let params_bytes = load_file ( & params_uri) . await ?;
355- if let Ok ( object @ Value :: Object ( _ ) ) = serde_json:: from_slice ( params_bytes. as_slice ( ) ) {
356- return Ok ( object ) ;
360+ if let Ok ( Value :: Object ( values ) ) = serde_json:: from_slice ( params_bytes. as_slice ( ) ) {
361+ return Ok ( values ) ;
357362 }
358363 bail ! ( "Failed to parse JSON object from `{}`." , params)
359364}
@@ -402,10 +407,10 @@ mod tests {
402407 sniff_params ( "0" ) . await . unwrap_err ( ) ;
403408 sniff_params ( "[]" ) . await . unwrap_err ( ) ;
404409
405- assert ! ( matches! (
406- sniff_params ( r#"{"foo": 0}"# ) . await . unwrap ( ) ,
407- Value :: Object ( map ) if map . contains_key ( "foo" )
408- ) ) ;
410+ assert ! ( sniff_params ( r#"{"foo": 0}"# )
411+ . await
412+ . unwrap ( )
413+ . contains_key ( "foo" ) ) ;
409414
410415 let storage = quickwit_storage_uri_resolver ( )
411416 . resolve ( "ram:///tmp" )
@@ -416,10 +421,10 @@ mod tests {
416421 . await
417422 . unwrap ( ) ;
418423
419- assert ! ( matches! (
420- sniff_params ( "ram:///tmp/params.json" ) . await . unwrap ( ) ,
421- Value :: Object ( map ) if map . contains_key ( "bar" )
422- ) ) ;
424+ assert ! ( sniff_params ( "ram:///tmp/params.json" )
425+ . await
426+ . unwrap ( )
427+ . contains_key ( "bar" ) ) ;
423428 }
424429
425430 #[ test]
@@ -520,8 +525,7 @@ mod tests {
520525 . collect ( ) ;
521526 let sources = vec ! [ SourceConfig {
522527 source_id: "foo-source" . to_string( ) ,
523- source_type: "file" . to_string( ) ,
524- params: json!( { "filepath" : "path/to/file" } ) ,
528+ source_params: SourceParams :: file( "path/to/file" ) ,
525529 } ] ;
526530 let expected_source = vec ! [ SourceRow {
527531 source_id: "foo-source" . to_string( ) ,
@@ -584,13 +588,11 @@ mod tests {
584588 let sources = [
585589 SourceConfig {
586590 source_id : "foo-source" . to_string ( ) ,
587- source_type : "file" . to_string ( ) ,
588- params : json ! ( { } ) ,
591+ source_params : SourceParams :: stdin ( ) ,
589592 } ,
590593 SourceConfig {
591594 source_id : "bar-source" . to_string ( ) ,
592- source_type : "file" . to_string ( ) ,
593- params : json ! ( { } ) ,
595+ source_params : SourceParams :: stdin ( ) ,
594596 } ,
595597 ] ;
596598 let expected_sources = [
0 commit comments