main.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #[macro_use]
  2. extern crate clap;
  3. use clap::App;
  4. use std::env;
  5. use std::path::Path;
  6. mod lib;
  7. use crate::lib::{
  8. config::{
  9. find_config_file,
  10. read_configuration_file,
  11. write_configuration_file,
  12. Config
  13. },
  14. repository,
  15. action,
  16. group,
  17. repotype,
  18. run
  19. };
  20. fn main() {
  21. let yaml = load_yaml!("cli.yml");
  22. let matches = App::from_yaml(yaml).version(crate_version!()).get_matches();
  23. let config_file = find_config_file(matches.value_of("config"));
  24. let mut configuration: Config = read_configuration_file(&config_file);
  25. if matches.is_present("config") {
  26. configuration.is_not_default = true;
  27. configuration.base_path = Path::new(matches.value_of("config").unwrap()).canonicalize().unwrap().parent().unwrap().to_path_buf();
  28. }
  29. match matches.subcommand_name() {
  30. Some("run") => if let Some(matches) = matches.subcommand_matches("run") {
  31. if matches.is_present("command") {
  32. println!("Running with command");
  33. run::run_with_command(&configuration, matches.value_of("command").unwrap().to_string(),
  34. matches.values_of("name").unwrap());
  35. } else {
  36. run::run(&configuration, matches.values_of("name").unwrap());
  37. }
  38. },
  39. Some("repository") => if let Some(matches) = matches.subcommand_matches("repository") {
  40. match matches.subcommand_name() {
  41. Some("register") => if let Some(matches) = matches.subcommand_matches("register") {
  42. let type_name = matches.value_of("type").unwrap().to_string();
  43. let location = match configuration.is_not_default {
  44. true => env::current_dir().unwrap().strip_prefix(&configuration.base_path).unwrap().to_path_buf(),
  45. _ => env::current_dir().unwrap(),
  46. };
  47. let location_string = location.to_str().unwrap().to_string();
  48. let name = match matches.value_of("name") {
  49. Some(string) => string.to_string(),
  50. None => location.file_name().unwrap().to_str().unwrap().to_string()
  51. };
  52. let mut option_strings: Vec<String> = Vec::new();
  53. match matches.values_of("options") {
  54. Some(option_strings_in) => {
  55. for str_thing in option_strings_in {
  56. option_strings.push(str_thing.to_string())
  57. }
  58. },
  59. None => {}
  60. }
  61. repository::register(&mut configuration, &name, location_string, type_name, option_strings);
  62. },
  63. Some("config") => if let Some(matches) = matches.subcommand_matches("config") {
  64. let name = matches.value_of("name").unwrap().to_string();
  65. if let Some(options) = matches.values_of("options") {
  66. let mut option_strings: Vec<String> = Vec::new();
  67. for str_thing in options {
  68. option_strings.push(str_thing.to_string())
  69. }
  70. repository::update_options(&mut configuration, &name, option_strings);
  71. }
  72. match matches.value_of("autocreate") {
  73. Some("YES") => repository::update_autocreate(&mut configuration, &name, true),
  74. Some("NO") => repository::update_autocreate(&mut configuration, &name, false),
  75. _ => {}
  76. }
  77. match matches.value_of("disable") {
  78. Some("YES") => repository::update_disabled(&mut configuration, &name, true),
  79. Some("NO") => repository::update_disabled(&mut configuration, &name, false),
  80. _ => {}
  81. }
  82. },
  83. Some("remove") => if let Some(matches) = matches.subcommand_matches("remove") {
  84. let name = matches.value_of("name").unwrap().to_string();
  85. repository::remove_repo(&mut configuration, &name);
  86. },
  87. Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
  88. let name = matches.value_of("name").unwrap().to_string();
  89. let repository = configuration.repositories.get(&name);
  90. match repository {
  91. Some(repository) => println!("{}", repository),
  92. None => eprintln!("No known repository named \"{}\".", name)
  93. }
  94. },
  95. Some("list") => {
  96. for key in configuration.repositories.keys() {
  97. println!(" - {}", key);
  98. }
  99. }
  100. _ => panic!("Something has gone horribly wrong...")
  101. }
  102. },
  103. Some("action") => if let Some(matches) = matches.subcommand_matches("action") {
  104. match matches.subcommand_name() {
  105. Some("create") => if let Some(matches) = matches.subcommand_matches("create") {
  106. let name = matches.value_of("name").unwrap().to_string();
  107. let command = matches.value_of("command").unwrap().to_string();
  108. let description = match matches.value_of("description") {
  109. Some(string) => string.to_string(),
  110. _ => String::from("")
  111. };
  112. action::add(&mut configuration, &name, &description, &command);
  113. },
  114. Some("config") => if let Some(matches) = matches.subcommand_matches("config") {
  115. let name = matches.value_of("name").unwrap().to_string();
  116. match matches.value_of("disabled") {
  117. Some("YES") => action::update_disabled(&mut configuration, &name, true),
  118. Some("NO") => action::update_disabled(&mut configuration, &name, false),
  119. _ => {}
  120. }
  121. match matches.value_of("command") {
  122. Some(command) => action::update_command(&mut configuration, &name, &command.to_string()),
  123. _ => {}
  124. }
  125. match matches.value_of("description") {
  126. Some(description) => action::update_description(&mut configuration, &name, &description.to_string()),
  127. _ => {}
  128. }
  129. },
  130. Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
  131. let name = matches.value_of("name").unwrap().to_string();
  132. let action = configuration.actions.get(&name);
  133. match action {
  134. Some(action) => println!("{}", action),
  135. None => eprintln!("No known action named \"{}\".", name)
  136. }
  137. },
  138. Some("list") => {
  139. for key in configuration.actions.keys() {
  140. println!(" - {}", key);
  141. }
  142. }
  143. _ => panic!("Something has gone horribly wrong...")
  144. }
  145. },
  146. Some("group") => if let Some(matches) = matches.subcommand_matches("group") {
  147. match matches.subcommand_name() {
  148. Some("create") => if let Some(matches) = matches.subcommand_matches("create") {
  149. let name = matches.value_of("name").unwrap().to_string();
  150. group::add(&mut configuration, &name);
  151. },
  152. Some("add") => if let Some(matches) = matches.subcommand_matches("add") {
  153. let name = matches.value_of("name").unwrap().to_string();
  154. let repo = matches.value_of("repo").unwrap().to_string();
  155. group::add_repo(&mut configuration, &name, &repo);
  156. },
  157. Some("act") => if let Some(matches) = matches.subcommand_matches("act") {
  158. let name = matches.value_of("name").unwrap().to_string();
  159. let action = matches.value_of("action").unwrap().to_string();
  160. group::add_action(&mut configuration, &name, &action);
  161. },
  162. Some("remove") => if let Some(matches) = matches.subcommand_matches("remove") {
  163. let name = matches.value_of("name").unwrap().to_string();
  164. let repo = matches.value_of("repo").unwrap().to_string();
  165. group::remove_repo(&mut configuration, &name, &repo);
  166. },
  167. Some("drop") => if let Some(matches) = matches.subcommand_matches("drop") {
  168. let name = matches.value_of("name").unwrap().to_string();
  169. group::remove_group(&mut configuration, &name);
  170. }
  171. Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
  172. let name = matches.value_of("name").unwrap().to_string();
  173. let group = configuration.groups.get(&name);
  174. match group {
  175. Some(group) => println!("{}", group),
  176. None => eprintln!("No known group named \"{}\".", name)
  177. }
  178. },
  179. Some("list") => {
  180. for key in configuration.groups.keys() {
  181. println!(" - {}", key);
  182. }
  183. },
  184. _ => panic!("Something has gone horribly wrong...")
  185. }
  186. },
  187. Some("type") => if let Some(matches) = matches.subcommand_matches("type") {
  188. match matches.subcommand_name() {
  189. Some("create") => if let Some(matches) = matches.subcommand_matches("create") {
  190. let name = matches.value_of("name").unwrap().to_string();
  191. let description = match matches.value_of("description") {
  192. Some(thing) => thing.to_string(),
  193. None => "".to_string()
  194. };
  195. let create = match matches.value_of("create") {
  196. Some(thing) => thing.to_string(),
  197. None => "".to_string()
  198. };
  199. let inward = match matches.value_of("inward") {
  200. Some(thing) => thing.to_string(),
  201. None => "".to_string()
  202. };
  203. let outward = match matches.value_of("outward") {
  204. Some(thing) => thing.to_string(),
  205. None => "".to_string()
  206. };
  207. let status = match matches.value_of("status") {
  208. Some(thing) => thing.to_string(),
  209. None => "".to_string()
  210. };
  211. let pre_inward = match matches.value_of("pre_inward") {
  212. Some(thing) => thing.to_string(),
  213. None => "".to_string()
  214. };
  215. let post_inward = match matches.value_of("post_inward") {
  216. Some(thing) => thing.to_string(),
  217. None => "".to_string()
  218. };
  219. let post_outward = match matches.value_of("post_outward") {
  220. Some(thing) => thing.to_string(),
  221. None => "".to_string()
  222. };
  223. repotype::add(&mut configuration, &name, &description, &create, &inward, &outward, &status, &pre_inward, &post_inward, &post_outward);
  224. },
  225. Some("config") => if let Some(matches) = matches.subcommand_matches("config") {
  226. let name = matches.value_of("name").unwrap().to_string();
  227. match matches.value_of("description") {
  228. Some(description) => repotype::update_description(&mut configuration, &name, &description.to_string()),
  229. _ => {}
  230. }
  231. match matches.value_of("create") {
  232. Some(create) => repotype::update_create(&mut configuration, &name, &create.to_string()),
  233. _ => {}
  234. }
  235. match matches.value_of("inward") {
  236. Some(inward) => repotype::update_inward(&mut configuration, &name, &inward.to_string()),
  237. _ => {}
  238. }
  239. match matches.value_of("outward") {
  240. Some(outward) => repotype::update_outward(&mut configuration, &name, &outward.to_string()),
  241. _ => {}
  242. }
  243. match matches.value_of("status") {
  244. Some(status) => repotype::update_status(&mut configuration, &name, &status.to_string()),
  245. _ => {}
  246. }
  247. match matches.value_of("pre_inward") {
  248. Some(pre_inward) => repotype::update_pre_inward(&mut configuration, &name, &pre_inward.to_string()),
  249. _ => {}
  250. }
  251. match matches.value_of("post_inward") {
  252. Some(post_inward) => repotype::update_post_inward(&mut configuration, &name, &post_inward.to_string()),
  253. _ => {}
  254. }
  255. match matches.value_of("post_outward") {
  256. Some(post_outward) => repotype::update_post_outward(&mut configuration, &name, &post_outward.to_string()),
  257. _ => {}
  258. }
  259. },
  260. Some("command") => if let Some(matches) = matches.subcommand_matches("command") {
  261. match matches.subcommand_name() {
  262. Some("add") => if let Some(matches) = matches.subcommand_matches("add") {
  263. let type_name = matches.value_of("type").unwrap().to_string();
  264. let name = matches.value_of("name").unwrap().to_string();
  265. let command = matches.value_of("command").unwrap().to_string();
  266. repotype::add_command(&mut configuration, &type_name, &name, &command);
  267. },
  268. Some("change") => if let Some(matches) = matches.subcommand_matches("change") {
  269. let type_name = matches.value_of("type").unwrap().to_string();
  270. let name = matches.value_of("name").unwrap().to_string();
  271. let command = matches.value_of("command").unwrap().to_string();
  272. repotype::change_command(&mut configuration, &type_name, &name, &command);
  273. },
  274. Some("remove") => if let Some(matches) = matches.subcommand_matches("remove") {
  275. let type_name = matches.value_of("type").unwrap().to_string();
  276. let name = matches.value_of("name").unwrap().to_string();
  277. repotype::remove_command(&mut configuration, &type_name, &name);
  278. },
  279. _ => panic!("Something has gone horribly wrong...")
  280. }
  281. },
  282. Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
  283. let name = matches.value_of("name").unwrap().to_string();
  284. let repo_type = configuration.repo_types.get(&name);
  285. match repo_type {
  286. Some(repo_type) => println!("{}", repo_type),
  287. None => eprintln!("No known repo type named \"{}\".", name)
  288. }
  289. },
  290. Some("list") => {
  291. for key in configuration.repo_types.keys() {
  292. println!(" - {}", key);
  293. }
  294. },
  295. _ => panic!("Something has gone horribly wrong...")
  296. }
  297. },
  298. Some(thing) => println!("{}", thing),
  299. _ => println!("No subcommand."),
  300. }
  301. match write_configuration_file(config_file, configuration) {
  302. Err(err) => panic!("Error writing configuration: {}.", err),
  303. _ => {}
  304. }
  305. }