Skip to content

Commit

Permalink
fix commands
Browse files Browse the repository at this point in the history
  • Loading branch information
YuliaProkopovych committed Apr 9, 2024
1 parent b625f86 commit 530d27f
Show file tree
Hide file tree
Showing 18 changed files with 316 additions and 228 deletions.
2 changes: 1 addition & 1 deletion module/move/unitore/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cargo run .feeds.list
```
To get custom information about feeds or frames run SQL query to storage database using command `.query.execute` with query string:
```bash
cargo run .query.execute \'SELECT title, links, MIN\(published\) FROM frame\'
cargo run .query.execute 'SELECT title, links, MIN(published) FROM frame'
```
To remove config file from storage use command `.config.delete` with path to config file:
```bash
Expand Down
23 changes: 7 additions & 16 deletions module/move/unitore/src/action/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Actions and report for commands for config files.
//! Actions and report for config files.
use std::path::PathBuf;

use crate::*;
use error_tools::{ err, for_app::Context, BasicError, Result };
use error_tools::{ for_app::Context, Result };
use sled_adapter::FeedStorage;
use entity::
{
Expand All @@ -12,14 +14,8 @@ use action::Report;
use gluesql::{ prelude::Payload, sled_storage::SledStorage };

/// Add configuration file with subscriptions to storage.
pub async fn config_add( mut storage : FeedStorage< SledStorage >, args : &wca::Args ) -> Result< impl Report >
pub async fn config_add( mut storage : FeedStorage< SledStorage >, path : &PathBuf ) -> Result< impl Report >
{
let path : std::path::PathBuf = args
.get_owned::< wca::Value >( 0 )
.ok_or_else::< BasicError, _ >( || err!( "Cannot get path argument for command .config.add" ) )?
.into()
;

let path = proper_path_tools::path::normalize( path );

let mut err_str = format!( "Invalid path for config file {:?}", path );
Expand Down Expand Up @@ -66,14 +62,9 @@ pub async fn config_add( mut storage : FeedStorage< SledStorage >, args : &wca::
}

/// Remove configuration file from storage.
pub async fn config_delete( mut storage : FeedStorage< SledStorage >, args : &wca::Args ) -> Result< impl Report >
pub async fn config_delete( mut storage : FeedStorage< SledStorage >, path : &PathBuf ) -> Result< impl Report >
{
let path : std::path::PathBuf = args
.get_owned::< wca::Value >( 0 )
.ok_or_else::< BasicError, _ >( || err!( "Cannot get path argument for command .config.delete" ) )?
.into()
;

let path = proper_path_tools::path::normalize( path );
let path = path.canonicalize().context( format!( "Invalid path for config file {:?}", path ) )?;
let config = Config::new( path.to_string_lossy().to_string() );

Expand Down
8 changes: 2 additions & 6 deletions module/move/unitore/src/action/feed.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Endpoints and report for feed commands.
//! Feed actions and reports.
use crate::*;
use action::{ Report, frame::SelectedEntries };
Expand All @@ -7,11 +7,7 @@ use entity::feed::FeedStore;
use error_tools::Result;

/// List all feeds from storage.
pub async fn feeds_list
(
mut storage : FeedStorage< gluesql::sled_storage::SledStorage >,
_args : &wca::Args,
) -> Result< impl Report >
pub async fn feeds_list( mut storage : FeedStorage< gluesql::sled_storage::SledStorage > ) -> Result< impl Report >
{
storage.feeds_list().await
}
Expand Down
11 changes: 3 additions & 8 deletions module/move/unitore/src/action/frame.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Frames commands actions.
//! Frames actions and reports.
use crate::*;
use sled_adapter::FeedStorage;
Expand All @@ -16,20 +16,15 @@ use action::Report;
// qqq : review the whole project and make sure all names are consitant: actions, commands, its tests

/// List all frames.
pub async fn frames_list
(
mut storage : FeedStorage< SledStorage >,
_args : &wca::Args,
) -> Result< impl Report >
pub async fn frames_list( mut storage : FeedStorage< SledStorage > ) -> Result< impl Report >
{
storage.frames_list().await
}

/// Update all frames from config files saved in storage.
pub async fn frames_download
(
mut storage : FeedStorage< SledStorage >,
_args : &wca::Args,
mut storage : FeedStorage< SledStorage >
) -> Result< impl Report >
{
let payload = storage.config_list().await?;
Expand Down
14 changes: 4 additions & 10 deletions module/move/unitore/src/action/query.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,21 @@
//! Query command endpoint and report.
//! Query actions and report.
// qqq : don't use both
// aaa : fixed
use crate::*;
use gluesql::core::executor::Payload;
use sled_adapter::{ FeedStorage, Store };
use action::Report;
use error_tools::{ err, BasicError, Result };
use error_tools::Result;

/// Execute query specified in query string.
pub async fn query_execute
(
mut storage : FeedStorage< gluesql::sled_storage::SledStorage >,
args : &wca::Args,
query_str : String,
) -> Result< impl Report >
{
let query = args
.get_owned::< Vec::< String > >( 0 )
.ok_or_else::< BasicError, _ >( || err!( "Cannot get Query argument for command .query.execute" ) )?
.join( " " )
;

storage.execute_query( query ).await
storage.execute_query( query_str ).await
}

const EMPTY_CELL : &'static str = "";
Expand Down
46 changes: 19 additions & 27 deletions module/move/unitore/src/action/table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Tables metadata commands actions and reports.
//! Tables metadata actions and reports.
use crate::*;
use gluesql::prelude::Payload;
Expand All @@ -12,12 +12,9 @@ use error_tools::Result;
pub async fn table_list
(
mut storage : FeedStorage< gluesql::sled_storage::SledStorage >,
args : &wca::Args,
table_name : Option< String >,
) -> Result< impl Report >
{
let table_name = args.get_owned::< String >( 0 );

// let mut manager = FeedManager::new( storage );
let mut table_names = Vec::new();
if let Some( name ) = table_name
{
Expand All @@ -27,7 +24,7 @@ pub async fn table_list
{
let tables = storage.tables_list().await?;

let names = tables.tables.keys().map( | k | k.clone() ).collect::< Vec< _ > >();
let names = tables.0.keys().map( | k | k.clone() ).collect::< Vec< _ > >();
table_names.extend( names.into_iter() );
}

Expand Down Expand Up @@ -123,21 +120,24 @@ pub async fn table_list
},
"authors" =>
{
columns_desc.insert(
columns_desc.insert
(
label.clone(),
String::from( "List of authors of the frame, optional." )
);
},
"content" =>
{
columns_desc.insert(
columns_desc.insert
(
label.clone(),
String::from( "The content of the frame in html or plain text, optional." ),
);
},
"links" =>
{
columns_desc.insert(
columns_desc.insert
(
label.clone(),
String::from( "List of links associated with this item of related Web page and attachments." ),
);
Expand Down Expand Up @@ -232,11 +232,7 @@ pub async fn table_list
}

/// Get information about tables in storage.
pub async fn tables_list
(
mut storage : FeedStorage< gluesql::sled_storage::SledStorage >,
_args : &wca::Args,
) -> Result< impl Report >
pub async fn tables_list( mut storage : FeedStorage< gluesql::sled_storage::SledStorage > ) -> Result< impl Report >
{
storage.tables_list().await
}
Expand All @@ -245,7 +241,7 @@ const EMPTY_CELL : &'static str = "";

/// Information about execution of table columns commands.
#[ derive( Debug ) ]
pub struct TablesColumnsReport( Vec< ColumnsReport > );
pub struct TablesColumnsReport( pub Vec< ColumnsReport > );

impl std::fmt::Display for TablesColumnsReport
{
Expand All @@ -262,7 +258,7 @@ impl std::fmt::Display for TablesColumnsReport

impl Report for TablesColumnsReport {}

/// Information about execution of columns commands.
/// Information about execution of columns listing action.
#[ derive( Debug ) ]
pub struct ColumnsReport
{
Expand Down Expand Up @@ -336,21 +332,18 @@ impl std::fmt::Display for ColumnsReport
impl Report for ColumnsReport {}

/// Information about execution of tables commands.
/// Contains tables name, description and list of columns.
#[ derive( Debug ) ]
pub struct TablesReport
{
tables : std::collections::HashMap< String, ( String, Vec< String > ) >
}
pub struct TablesReport( pub HashMap< String, ( String, Vec< String > ) > );

impl TablesReport
{
/// Create new report from payload.
pub fn new( payload : Vec< Payload > ) -> Self
{
let mut result = std::collections::HashMap::new();
match &payload[ 0 ]
if let Payload::Select { labels: _label_vec, rows: rows_vec } = &payload[ 0 ]
{
Payload::Select { labels: _label_vec, rows: rows_vec } =>
{
for row in rows_vec
{
Expand All @@ -367,10 +360,9 @@ impl TablesReport
.or_insert( ( table_description, vec![ String::from( row[ 1 ].clone() ) ] ) )
;
}
},
_ => {},
}
}
TablesReport{ tables : result }
TablesReport( result )
}
}

Expand All @@ -380,7 +372,7 @@ impl std::fmt::Display for TablesReport
{
writeln!( f, "Storage tables:" )?;
let mut rows = Vec::new();
for ( table_name, ( desc, columns ) ) in &self.tables
for ( table_name, ( desc, columns ) ) in &self.0
{
let columns_str = if !columns.is_empty()
{
Expand Down Expand Up @@ -409,7 +401,7 @@ impl std::fmt::Display for TablesReport
[
EMPTY_CELL.to_owned(),
"name".to_owned(),
"description".to_owned(),
"description".to_owned(),
"columns".to_owned(),
],
rows,
Expand Down
86 changes: 54 additions & 32 deletions module/move/unitore/src/command/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Config files commands.
use std::path::PathBuf;

use crate::*;
use gluesql::sled_storage::sled::Config;
use wca::{ Command, Type, VerifiedCommand };
Expand Down Expand Up @@ -37,25 +39,35 @@ impl ConfigCommand
.subject().hint( "Path" ).kind( Type::Path ).optional( false ).end()
.routine( move | o : VerifiedCommand |
{
let res = rt.block_on( async move
{
let path_to_storage = std::env::var( "UNITORE_STORAGE_PATH" )
.unwrap_or( String::from( "./_data" ) )
;

let config = Config::default()
.path( path_to_storage )
;

let feed_storage = FeedStorage::init_storage( &config ).await?;
config_add( feed_storage, &o.args ).await
});
let path_arg = o.args
.get_owned::< wca::Value >( 0 );

if let Some( path ) = path_arg
{
let path : PathBuf = path.into();

let res = rt.block_on
( async move
{
let path_to_storage = std::env::var( "UNITORE_STORAGE_PATH" )
.unwrap_or( String::from( "./_data" ) )
;

let config = Config::default()
.path( path_to_storage )
;

let feed_storage = FeedStorage::init_storage( &config ).await?;
config_add( feed_storage, &path ).await
}
);

match res
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
}

}
})
.end()
)
Expand All @@ -76,27 +88,37 @@ impl ConfigCommand
))
.subject().hint( "Path" ).kind( Type::Path ).optional( false ).end()
.routine( move | o : VerifiedCommand |
{
let res = rt.block_on( async move
{
let path_to_storage = std::env::var( "UNITORE_STORAGE_PATH" )
.unwrap_or( String::from( "./_data" ) )
;

let config = Config::default()
.path( path_to_storage )
;
{
let path_arg = o.args
.get_owned::< wca::Value >( 0 );

let feed_storage = FeedStorage::init_storage( &config ).await?;
config_delete( feed_storage, &o.args ).await
});
match res
if let Some( path ) = path_arg
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
let path : PathBuf = path.into();

let res = rt.block_on
( async move
{
let path_to_storage = std::env::var( "UNITORE_STORAGE_PATH" )
.unwrap_or( String::from( "./_data" ) )
;

let config = Config::default()
.path( path_to_storage )
;

let feed_storage = FeedStorage::init_storage( &config ).await?;
config_delete( feed_storage, &path ).await
}
);

match res
{
Ok( report ) => report.report(),
Err( err ) => println!( "{:?}", err ),
}
}

})
})
.end()
)
}
Expand Down
Loading

0 comments on commit 530d27f

Please sign in to comment.