|
| 1 | +// Copyright 2015-2016 Intecture Developers. See the COPYRIGHT file at the |
| 2 | +// top-level directory of this distribution and at |
| 3 | +// https://intecture.io/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Mozilla Public License 2.0 <LICENSE or |
| 6 | +// https://www.tldrlegal.com/l/mpl-2.0>. This file may not be copied, |
| 7 | +// modified, or distributed except according to those terms. |
| 8 | + |
| 9 | +use czmq::ZCert; |
| 10 | +use {Error, Result}; |
| 11 | +#[cfg(not(test))] |
| 12 | +use std::env::{self, Args}; |
| 13 | +use std::process::exit; |
| 14 | +#[cfg(test)] |
| 15 | +use tempdir::TempDir; |
| 16 | + |
| 17 | +#[cfg(test)] |
| 18 | +lazy_static! { |
| 19 | + static ref TMP_DIR: TempDir = TempDir::new("test_runtime_args").unwrap(); |
| 20 | + pub static ref RUNTIME_ARGS: RuntimeArgs = RuntimeArgs::new_test(); |
| 21 | +} |
| 22 | +#[cfg(not(test))] |
| 23 | +lazy_static! { |
| 24 | + pub static ref RUNTIME_ARGS: RuntimeArgs = RuntimeArgs::new_usage(env::args()); |
| 25 | +} |
| 26 | + |
| 27 | +pub struct RuntimeArgs { |
| 28 | + pub user_cert: ZCert, |
| 29 | + pub server_cert_path: String, |
| 30 | + pub user_args: Vec<String>, |
| 31 | +} |
| 32 | + |
| 33 | +unsafe impl Sync for RuntimeArgs {} |
| 34 | + |
| 35 | +impl RuntimeArgs { |
| 36 | + #[cfg(test)] |
| 37 | + fn new_test() -> RuntimeArgs { |
| 38 | + let server_cert = ZCert::new().unwrap(); |
| 39 | + server_cert.save(&format!("{}/localhost.crt", TMP_DIR.path().to_str().unwrap())).unwrap(); |
| 40 | + |
| 41 | + let user_cert = ZCert::new().unwrap(); |
| 42 | + let user_path = format!("{}/user.crt", TMP_DIR.path().to_str().unwrap()); |
| 43 | + user_cert.save(&user_path).unwrap(); |
| 44 | + |
| 45 | + RuntimeArgs::new(vec![ |
| 46 | + "/fake/runnable".to_string(), |
| 47 | + user_path, |
| 48 | + TMP_DIR.path().to_str().unwrap().to_string(), |
| 49 | + ]).unwrap() |
| 50 | + } |
| 51 | + |
| 52 | + #[cfg(not(test))] |
| 53 | + fn new_usage(args: Args) -> RuntimeArgs { |
| 54 | + let args: Vec<String> = args.collect(); |
| 55 | + let ra = Self::new(args); |
| 56 | + |
| 57 | + if ra.is_err() { |
| 58 | + println!("You should not invoke this project manually!"); |
| 59 | + println!("Usage: incli run [<script_args>]"); |
| 60 | + exit(1); |
| 61 | + } |
| 62 | + |
| 63 | + ra.unwrap() |
| 64 | + } |
| 65 | + |
| 66 | + fn new(args: Vec<String>) -> Result<RuntimeArgs> { |
| 67 | + let mut args = args; |
| 68 | + |
| 69 | + if args.len() < 3 { |
| 70 | + return Err(Error::Generic("Missing args".to_string())); |
| 71 | + } |
| 72 | + |
| 73 | + // Remove filename |
| 74 | + args.remove(0); |
| 75 | + |
| 76 | + Ok(RuntimeArgs { |
| 77 | + user_cert: try!(ZCert::load(&args.remove(0))), |
| 78 | + server_cert_path: args.remove(0), |
| 79 | + user_args: args, |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + pub fn expect_user_args<'a>(&'a mut self, required_args: &[&str]) -> &'a [String] { |
| 84 | + if self.user_args.len() != required_args.len() { |
| 85 | + // Generate usage string |
| 86 | + let mut usage = String::from("Usage: incli run"); |
| 87 | + for arg in required_args { |
| 88 | + usage.push_str(" <"); |
| 89 | + usage.push_str(arg); |
| 90 | + usage.push_str(">"); |
| 91 | + } |
| 92 | + |
| 93 | + println!("{}", usage); |
| 94 | + exit(1); |
| 95 | + } |
| 96 | + |
| 97 | + &self.user_args |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests { |
| 103 | + use czmq::ZCert; |
| 104 | + use super::*; |
| 105 | + use tempdir::TempDir; |
| 106 | + |
| 107 | + #[test] |
| 108 | + fn test_new_ok() { |
| 109 | + let dir = TempDir::new("test_new_ok").unwrap(); |
| 110 | + |
| 111 | + let user_path = format!("{}/user.crt", dir.path().to_str().unwrap()); |
| 112 | + let server_path = format!("{}/localhost.crt", dir.path().to_str().unwrap()); |
| 113 | + |
| 114 | + let user_cert = ZCert::new().unwrap(); |
| 115 | + user_cert.save(&user_path).unwrap(); |
| 116 | + |
| 117 | + let runtime_args = RuntimeArgs::new(vec![ |
| 118 | + "/path/to/project/runnable".to_string(), |
| 119 | + user_path, |
| 120 | + server_path |
| 121 | + ]); |
| 122 | + assert!(runtime_args.is_ok()); |
| 123 | + assert_eq!(runtime_args.unwrap().user_args.len(), 0); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn test_new_noargs() { |
| 128 | + let runtime_args = RuntimeArgs::new(vec!["/path/to/project/runnable".to_string()]); |
| 129 | + assert!(runtime_args.is_err()); |
| 130 | + } |
| 131 | + |
| 132 | + #[test] |
| 133 | + fn test_new_usrargs() { |
| 134 | + let dir = TempDir::new("test_new_usrargs").unwrap(); |
| 135 | + |
| 136 | + let user_path = format!("{}/user.crt", dir.path().to_str().unwrap()); |
| 137 | + let server_path = format!("{}/localhost.crt", dir.path().to_str().unwrap()); |
| 138 | + |
| 139 | + let user_cert = ZCert::new().unwrap(); |
| 140 | + user_cert.save(&user_path).unwrap(); |
| 141 | + |
| 142 | + let runtime_args = RuntimeArgs::new(vec![ |
| 143 | + "/path/to/project/runnable".to_string(), |
| 144 | + user_path, |
| 145 | + server_path, |
| 146 | + "user1".to_string(), |
| 147 | + "user2".to_string(), |
| 148 | + ]).unwrap(); |
| 149 | + assert_eq!(runtime_args.user_args.get(0).unwrap(), "user1"); |
| 150 | + assert_eq!(runtime_args.user_args.get(1).unwrap(), "user2"); |
| 151 | + } |
| 152 | +} |
0 commit comments