main.rs 14 KB

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