main.rs 14 KB

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