Parse command line argument.
authorGreg Burri <greg.burri@gmail.com>
Fri, 12 Jul 2019 20:45:22 +0000 (22:45 +0200)
committerGreg Burri <greg.burri@gmail.com>
Fri, 12 Jul 2019 20:45:22 +0000 (22:45 +0200)
Add a way ton encrypt a message.

.gitignore
Cargo.lock
Cargo.toml
TODO.md
generate_crypted_message.ps1 [new file with mode: 0644]
src/main.rs

index 53eaa21..c41e4eb 100644 (file)
@@ -1,2 +1,4 @@
 /target
 **/*.rs.bk
+key.secret
+WebSharper/key
index f0fe66d..59f442f 100644 (file)
@@ -720,6 +720,14 @@ dependencies = [
  "winreg 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
+[[package]]
+name = "itertools"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "either 1.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
 [[package]]
 name = "itoa"
 version = "0.4.4"
@@ -1282,6 +1290,7 @@ dependencies = [
  "actix-files 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "actix-web 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "askama 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
  "listenfd 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
  "openssl 0.10.23 (registry+https://github.com/rust-lang/crates.io-index)",
  "ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -1888,6 +1897,7 @@ dependencies = [
 "checksum indexmap 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7e81a7c05f79578dbc15793d8b619db9ba32b4577003ef3af1a91c416798c58d"
 "checksum iovec 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dbe6e417e7d0975db6512b90796e8ce223145ac4e33c377e4a42882a0e88bb08"
 "checksum ipconfig 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa79fa216fbe60834a9c0737d7fcd30425b32d1c58854663e24d4c4b328ed83f"
+"checksum itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5b8467d9c1cebe26feb08c640139247fac215782d35371ade9a2136ed6085358"
 "checksum itoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "501266b7edd0174f8530248f87f99c88fbe60ca4ef3dd486835b8d8d53136f7f"
 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
 "checksum language-tags 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a91d884b6667cd606bb5a69aa0c99ba811a115fc68915e7056ec08a46e93199a"
index a407f10..3d708a0 100644 (file)
@@ -13,4 +13,5 @@ serde = { version = "1.0", features = ["derive"] }
 openssl = "0.10"
 
 listenfd = "0.3" # To watch file modifications and automatically launch a build process (only used in dev/debug).
-ron = "0.5.1" # Rust object notation, to load configuration files.
\ No newline at end of file
+ron = "0.5.1" # Rust object notation, to load configuration files.
+itertools = "0.8.0"
\ No newline at end of file
diff --git a/TODO.md b/TODO.md
index cdb8666..fd7f80d 100644 (file)
--- a/TODO.md
+++ b/TODO.md
@@ -1,2 +1,4 @@
-* Use a setting file.\r
-* Enable Logging.
\ No newline at end of file
+* Enable Logging.\r
+\r
+[ok] Accept an argument to encrypt a message\r
+[ok] Use a setting file.
\ No newline at end of file
diff --git a/generate_crypted_message.ps1 b/generate_crypted_message.ps1
new file mode 100644 (file)
index 0000000..f50fc50
--- /dev/null
@@ -0,0 +1 @@
+cargo run --release -- --encrypt $args[0]
\ No newline at end of file
index 7a179af..09965c1 100644 (file)
@@ -9,7 +9,9 @@ use askama::Template;
 
 use ron::de::from_reader;
 use serde::Deserialize;
-use std::fs::File;
+use std::{fs::File, env::args};
+
+use itertools::Itertools;
 
 mod consts;
 mod crypto;
@@ -50,8 +52,38 @@ struct Config {
     port: u16
 }
 
+fn get_exe_name() -> String {
+    let first_arg = std::env::args().nth(0).unwrap();
+    //dbg!(&first_arg);
+    let sep: &[_] = &['\\', '/'];
+    first_arg[first_arg.rfind(sep).unwrap()+1..].to_string()
+}
+
+fn print_usage() {
+    println!("Usage:");
+    println!(" {} [--help] [--encrypt <message>]", get_exe_name());
+}
+
 fn main() -> std::io::Result<()> {
-    println!("Starting RUP...");
+    let args: Vec<String> = args().collect();
+
+    if args.iter().any(|arg| arg == "--help") {
+        print_usage();
+        return Ok(());
+    } else if let Some((position_arg_encrypt, _)) = args.iter().find_position(|arg| arg == &"--encrypt") {
+        match args.iter().nth(position_arg_encrypt + 1) {
+            Some(mess_to_encrypt) => {
+                let encrypted_mess = mess_to_encrypt;
+                //let encrypted_mess = crypto::encrypt(key: &str, plain_text: &str);
+                println!("Encrypted message: {}", encrypted_mess);
+            }
+            None => print_usage()
+        }
+
+        return Ok(());
+    }
+
+    println!("Starting RUP as web server...");
 
     let config: Config = {
         let f = File::open(consts::FILE_CONF).expect(&format!("Failed to open configuration file {}", consts::FILE_CONF));
@@ -61,6 +93,8 @@ fn main() -> std::io::Result<()> {
         }
     };
 
+    let key = File::open(consts::FILE_KEY).expect(&format!("Failed to open key file: {}", consts::FILE_KEY));
+
     println!("Configuration: {:?}", config);
 
     let mut listenfd = ListenFd::from_env();