main.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. };
  18. fn main() {
  19. let yaml = load_yaml!("cli.yml");
  20. let matches = App::from_yaml(yaml).get_matches();
  21. let config_file = find_config_file(matches.value_of("config"));
  22. let mut configuration: Config = read_configuration_file(&config_file);
  23. match matches.subcommand_name() {
  24. Some("run") => println!("Running..."),
  25. Some("repository") => if let Some(matches) = matches.subcommand_matches("repository") {
  26. match matches.subcommand_name() {
  27. Some("register") => if let Some(matches) = matches.subcommand_matches("register") {
  28. let type_name = matches.value_of("type").unwrap().to_string();
  29. let name = matches.value_of("name").unwrap().to_string();
  30. let option_strings_in: Vec<&str> = matches.values_of("options").unwrap().collect();
  31. let mut option_strings: Vec<String> = Vec::new();
  32. for str_thing in option_strings_in {
  33. option_strings.push(str_thing.to_string())
  34. }
  35. let location = env::current_dir().unwrap().to_str().unwrap().to_string();
  36. repository::register(&mut configuration, &name, location, type_name, option_strings);
  37. },
  38. Some("config") => if let Some(matches) = matches.subcommand_matches("config") {
  39. let name = matches.value_of("name").unwrap().to_string();
  40. if let Some(options) = matches.values_of("options") {
  41. let mut option_strings: Vec<String> = Vec::new();
  42. for str_thing in options {
  43. option_strings.push(str_thing.to_string())
  44. }
  45. repository::update_options(&mut configuration, &name, option_strings);
  46. }
  47. match matches.value_of("autocreate") {
  48. Some("YES") => repository::update_autocreate(&mut configuration, &name, true),
  49. Some("NO") => repository::update_autocreate(&mut configuration, &name, false),
  50. _ => {}
  51. }
  52. match matches.value_of("disable") {
  53. Some("YES") => repository::update_disabled(&mut configuration, &name, true),
  54. Some("NO") => repository::update_disabled(&mut configuration, &name, false),
  55. _ => {}
  56. }
  57. },
  58. Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
  59. let name = matches.value_of("name").unwrap().to_string();
  60. let repository = configuration.repositories.get(&name);
  61. match repository {
  62. Some(repository) => println!("{}", repository),
  63. None => eprintln!("No known repository named \"{}\".", name)
  64. }
  65. }
  66. _ => panic!("Something has gone horribly wrong...")
  67. }
  68. },
  69. Some("action") => if let Some(matches) = matches.subcommand_matches("action") {
  70. match matches.subcommand_name() {
  71. Some("create") => if let Some(matches) = matches.subcommand_matches("create") {
  72. let name = matches.value_of("name").unwrap().to_string();
  73. let command = matches.value_of("command").unwrap().to_string();
  74. let description = match matches.value_of("description") {
  75. Some(string) => string.to_string(),
  76. _ => String::from("")
  77. };
  78. action::add(&mut configuration, &name, &command, &description);
  79. },
  80. Some("config") => if let Some(matches) = matches.subcommand_matches("config") {
  81. let name = matches.value_of("name").unwrap().to_string();
  82. match matches.value_of("disabled") {
  83. Some("YES") => action::update_disabled(&mut configuration, &name, true),
  84. Some("NO") => action::update_disabled(&mut configuration, &name, false),
  85. _ => {}
  86. }
  87. match matches.value_of("command") {
  88. Some(command) => action::update_command(&mut configuration, &name, &command.to_string()),
  89. _ => {}
  90. }
  91. match matches.value_of("description") {
  92. Some(description) => action::update_description(&mut configuration, &name, &description.to_string()),
  93. _ => {}
  94. }
  95. },
  96. Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
  97. let name = matches.value_of("name").unwrap().to_string();
  98. let action = configuration.actions.get(&name);
  99. match action {
  100. Some(action) => println!("{}", action),
  101. None => eprintln!("No known action named \"{}\".", name)
  102. }
  103. },
  104. _ => panic!("Something has gone horribly wrong...")
  105. }
  106. },
  107. Some("group") => if let Some(matches) = matches.subcommand_matches("group") {
  108. match matches.subcommand_name() {
  109. Some("create") => if let Some(matches) = matches.subcommand_matches("create") {
  110. let name = matches.value_of("group").unwrap().to_string();
  111. group::add(&mut configuration, &name);
  112. },
  113. Some("add") => if let Some(matches) = matches.subcommand_matches("add") {
  114. let name = matches.value_of("group").unwrap().to_string();
  115. let repo = matches.value_of("repo").unwrap().to_string();
  116. group::add_repo(&mut configuration, &name, &repo);
  117. },
  118. Some("act") => if let Some(matches) = matches.subcommand_matches("act") {
  119. let name = matches.value_of("group").unwrap().to_string();
  120. let action = matches.value_of("action").unwrap().to_string();
  121. group::add_action(&mut configuration, &name, &action);
  122. },
  123. Some("remove") => if let Some(matches) = matches.subcommand_matches("remove") {
  124. let name = matches.value_of("group").unwrap().to_string();
  125. let repo = matches.value_of("repo").unwrap().to_string();
  126. group::remove_repo(&mut configuration, &name, &repo);
  127. },
  128. Some("drop") => if let Some(matches) = matches.subcommand_matches("drop") {
  129. let name = matches.value_of("group").unwrap().to_string();
  130. group::remove_group(&mut configuration, &name);
  131. }
  132. Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
  133. let name = matches.value_of("group").unwrap().to_string();
  134. let group = configuration.groups.get(&name);
  135. match group {
  136. Some(group) => println!("{}", group),
  137. None => eprintln!("No known group named \"{}\".", name)
  138. }
  139. },
  140. _ => panic!("Something has gone horribly wrong...")
  141. }
  142. },
  143. Some("type") => if let Some(matches) = matches.subcommand_matches("type") {
  144. match matches.subcommand_name() {
  145. Some("create") => if let Some(matches) = matches.subcommand_matches("create") {
  146. let name = matches.value_of("name").unwrap().to_string();
  147. let description = matches.value_of("description").unwrap().to_string();
  148. let create = match matches.value_of("create") {
  149. Some(thing) => thing.to_string(),
  150. None => "".to_string()
  151. };
  152. let inward = match matches.value_of("inward") {
  153. Some(thing) => thing.to_string(),
  154. None => "".to_string()
  155. };
  156. let outward = match matches.value_of("outward") {
  157. Some(thing) => thing.to_string(),
  158. None => "".to_string()
  159. };
  160. let status = match matches.value_of("status") {
  161. Some(thing) => thing.to_string(),
  162. None => "".to_string()
  163. };
  164. let pre_inward = match matches.value_of("pre_inward") {
  165. Some(thing) => thing.to_string(),
  166. None => "".to_string()
  167. };
  168. let post_inward = match matches.value_of("post_inward") {
  169. Some(thing) => thing.to_string(),
  170. None => "".to_string()
  171. };
  172. let post_outward = match matches.value_of("post_outward") {
  173. Some(thing) => thing.to_string(),
  174. None => "".to_string()
  175. };
  176. repotype::add(&mut configuration, &name, &description, &create, &inward, &outward, &status, &pre_inward, &post_inward, &post_outward);
  177. },
  178. Some("show") => if let Some(matches) = matches.subcommand_matches("show") {
  179. let name = matches.value_of("name").unwrap().to_string();
  180. let repo_type = configuration.repo_types.get(&name);
  181. match repo_type {
  182. Some(repo_type) => println!("{}", repo_type),
  183. None => eprintln!("No known repo type named \"{}\".", name)
  184. }
  185. },
  186. _ => panic!("Something has gone horribly wrong...")
  187. }
  188. },
  189. Some(thing) => println!("{}", thing),
  190. _ => println!("No subcommand."),
  191. }
  192. match write_configuration_file(config_file, configuration) {
  193. Err(err) => panic!("Error writing configuration: {}.", err),
  194. _ => {}
  195. }
  196. }