From 2a3fa98fef702d174f1dc199819da1c08df9b913 Mon Sep 17 00:00:00 2001 From: vms Date: Fri, 5 Feb 2021 00:54:25 +0300 Subject: [PATCH] enable logger by default in repl --- Cargo.lock | 4 +- tools/repl/Cargo.toml | 2 +- tools/repl/src/repl.rs | 225 ++++++++++++++++++++++------------------- 3 files changed, 126 insertions(+), 105 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5edc1a0c..ab2e2f46 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -720,7 +720,7 @@ dependencies = [ [[package]] name = "fcli" -version = "0.1.30" +version = "0.1.31" dependencies = [ "anyhow", "clap", @@ -898,7 +898,7 @@ dependencies = [ [[package]] name = "frepl" -version = "0.1.30" +version = "0.1.32" dependencies = [ "anyhow", "clap", diff --git a/tools/repl/Cargo.toml b/tools/repl/Cargo.toml index ef9ff426..24ad0d10 100644 --- a/tools/repl/Cargo.toml +++ b/tools/repl/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "frepl" description = "Fluence FCE REPL intended for testing purposes" -version = "0.1.31" +version = "0.1.32" authors = ["Fluence Labs"] repository = "https://github.com/fluencelabs/fce/tools/repl" license = "Apache-2.0" diff --git a/tools/repl/src/repl.rs b/tools/repl/src/repl.rs index 840ff377..9bd5a713 100644 --- a/tools/repl/src/repl.rs +++ b/tools/repl/src/repl.rs @@ -19,6 +19,7 @@ mod print_state; use crate::ReplResult; use fluence_app_service::AppService; +use fluence_app_service::FaaSModuleConfig; use fluence_app_service::TomlAppServiceConfig; use print_state::print_envs; use print_state::print_fs_state; @@ -34,7 +35,7 @@ macro_rules! next_argument { $arg_name } else { println!($error_msg); - return true; + return; }; }; } @@ -50,108 +51,15 @@ impl REPL { } /// Returns true, it should be the last executed command. - pub fn execute<'a>(&mut self, mut args: impl Iterator) -> bool { + pub fn execute<'args>(&mut self, mut args: impl Iterator) -> bool { match args.next() { - Some("new") => { - match Self::create_app_service(args.next()) { - Ok(service) => self.app_service = service, - Err(e) => println!("failed to create a new application service: {}", e), - }; - } - Some("load") => { - next_argument!(module_name, args, "Module name should be specified"); - next_argument!(module_path, args, "Module path should be specified"); - - let wasm_bytes = fs::read(module_path); - if let Err(e) = wasm_bytes { - println!("failed to read wasm module: {}", e); - return true; - } - - let start = Instant::now(); - let result_msg = match self - .app_service - .load_module::( - module_name.into(), - &wasm_bytes.unwrap(), - None, - ) { - Ok(_) => { - let elapsed_time = start.elapsed(); - format!( - "module successfully loaded into App service\nelapsed time: {:?}", - elapsed_time - ) - } - Err(e) => format!("module loaded failed with: {:?}", e), - }; - println!("{}", result_msg); - } - Some("unload") => { - next_argument!(module_name, args, "Module name should be specified"); - - let start = Instant::now(); - let result_msg = match self.app_service.unload_module(module_name) { - Ok(_) => { - let elapsed_time = start.elapsed(); - format!( - "module successfully unloaded from App service\nelapsed time: {:?}", - elapsed_time - ) - } - Err(e) => format!("module unloaded failed with: {:?}", e), - }; - println!("{}", result_msg); - } - Some("call") => { - use itertools::Itertools; - - next_argument!(module_name, args, "Module name should be specified"); - next_argument!(func_name, args, "Function name should be specified"); - - let module_arg: String = args.join(" "); - let module_arg: serde_json::Value = match serde_json::from_str(&module_arg) { - Ok(module_arg) => module_arg, - Err(e) => { - println!("incorrect arguments {}", e); - return true; - } - }; - - let start = Instant::now(); - // TODO: add support of call parameters - let result = match self.app_service.call_with_module_name( - module_name, - func_name, - module_arg, - <_>::default(), - ) { - Ok(result) => { - let elapsed_time = start.elapsed(); - format!("result: {:?}\n elapsed time: {:?}", result, elapsed_time) - } - Err(e) => format!("execution failed with {:?}", e), - }; - println!("{}", result); - } - Some("envs") => { - next_argument!(module_name, args, "Module name should be specified"); - match self.app_service.get_wasi_state(module_name) { - Ok(wasi_state) => print_envs(module_name, wasi_state), - Err(e) => println!("{}", e), - }; - } - Some("fs") => { - next_argument!(module_name, args, "Module name should be specified"); - match self.app_service.get_wasi_state(module_name) { - Ok(wasi_state) => print_fs_state(wasi_state), - Err(e) => println!("{}", e), - }; - } - Some("interface") => { - let interface = self.app_service.get_full_interface(); - print!("Application service interface:\n{}", interface); - } + Some("new") => self.new_service(args), + Some("load") => self.load_module(args), + Some("unload") => self.unload_module(args), + Some("call") => self.call_module(args), + Some("envs") => self.show_envs(args), + Some("fs") => self.show_fs(args), + Some("interface") => self.show_interface(), Some("q") | Some("quit") => { return false; } @@ -162,6 +70,119 @@ impl REPL { true } + fn new_service<'args>(&mut self, mut args: impl Iterator) { + match Self::create_app_service(args.next()) { + Ok(service) => self.app_service = service, + Err(e) => println!("failed to create a new application service: {}", e), + }; + } + + fn load_module<'args>(&mut self, mut args: impl Iterator) { + next_argument!(module_name, args, "Module name should be specified"); + next_argument!(module_path, args, "Module path should be specified"); + + let wasm_bytes = fs::read(module_path); + if let Err(e) = wasm_bytes { + println!("failed to read wasm module: {}", e); + return; + } + + let start = Instant::now(); + let config = FaaSModuleConfig { + logger_enabled: true, + ..<_>::default() + }; + let result_msg = match self + .app_service + .load_module::( + module_name.into(), + &wasm_bytes.unwrap(), + Some(config), + ) { + Ok(_) => { + let elapsed_time = start.elapsed(); + format!( + "module successfully loaded into App service\nelapsed time: {:?}", + elapsed_time + ) + } + Err(e) => format!("module loaded failed with: {:?}", e), + }; + println!("{}", result_msg); + } + + fn unload_module<'args>(&mut self, mut args: impl Iterator) { + next_argument!(module_name, args, "Module name should be specified"); + + let start = Instant::now(); + let result_msg = match self.app_service.unload_module(module_name) { + Ok(_) => { + let elapsed_time = start.elapsed(); + format!( + "module successfully unloaded from App service\nelapsed time: {:?}", + elapsed_time + ) + } + Err(e) => format!("module unloaded failed with: {:?}", e), + }; + println!("{}", result_msg); + } + + fn call_module<'args>(&mut self, mut args: impl Iterator) { + use itertools::Itertools; + + next_argument!(module_name, args, "Module name should be specified"); + next_argument!(func_name, args, "Function name should be specified"); + + let module_arg: String = args.join(" "); + let module_arg: serde_json::Value = match serde_json::from_str(&module_arg) { + Ok(module_arg) => module_arg, + Err(e) => { + println!("incorrect arguments {}", e); + return; + } + }; + + let start = Instant::now(); + // TODO: add support of call parameters + let result = match self.app_service.call_with_module_name( + module_name, + func_name, + module_arg, + <_>::default(), + ) { + Ok(result) => { + let elapsed_time = start.elapsed(); + format!("result: {:?}\n elapsed time: {:?}", result, elapsed_time) + } + Err(e) => format!("execution failed with {:?}", e), + }; + + println!("{}", result); + } + + fn show_envs<'args>(&mut self, mut args: impl Iterator) { + next_argument!(module_name, args, "Module name should be specified"); + match self.app_service.get_wasi_state(module_name) { + Ok(wasi_state) => print_envs(module_name, wasi_state), + Err(e) => println!("{}", e), + }; + } + + fn show_fs<'args>(&mut self, mut args: impl Iterator) { + next_argument!(module_name, args, "Module name should be specified"); + match self.app_service.get_wasi_state(module_name) { + Ok(wasi_state) => print_fs_state(wasi_state), + Err(e) => println!("{}", e), + }; + } + + fn show_interface(&mut self) { + let interface = self.app_service.get_full_interface(); + + print!("Loaded modules interface:\n{}", interface); + } + fn create_app_service>(config_file_path: Option) -> ReplResult { let tmp_path: String = std::env::temp_dir().to_string_lossy().into(); let service_id = uuid::Uuid::new_v4().to_string();