run.rs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. use clap::Values;
  2. use crate::lib::config::Config;
  3. use string_template::Template;
  4. use std::process::Command;
  5. use std::path::Path;
  6. use std::collections::HashMap;
  7. pub fn run(config: &Config, names: Values<'_>) {
  8. for name in names {
  9. if config.repositories.contains_key(name) {
  10. run_repository_sync(&config, name.to_string());
  11. } else if config.groups.contains_key(name) {
  12. run_group(&config, name.to_string());
  13. } else {
  14. println!("\"{}\" is neither a group nor a repository.", name);
  15. }
  16. }
  17. }
  18. fn run_command(command: String) {
  19. if !command.is_empty() {
  20. match Command::new("sh")
  21. .arg("-c")
  22. .arg(command)
  23. .spawn() {
  24. Ok(mut child) => {
  25. let mut status = child.try_wait();
  26. loop {
  27. match status {
  28. Ok(Some(_)) => break,
  29. Ok(None) => {},
  30. _ => {}
  31. }
  32. status = child.try_wait();
  33. }
  34. },
  35. _ => {}
  36. }
  37. }
  38. }
  39. fn run_command_in_directory(directory: String, command: String) {
  40. if !command.is_empty() {
  41. match Command::new("sh")
  42. .current_dir(directory)
  43. .arg("-c")
  44. .arg(command)
  45. .spawn() {
  46. Ok(mut child) => {
  47. let mut status = child.try_wait();
  48. loop {
  49. match status {
  50. Ok(Some(_)) => break,
  51. Ok(None) => {},
  52. _ => {}
  53. }
  54. status = child.try_wait();
  55. }
  56. },
  57. _ => {}
  58. }
  59. }
  60. }
  61. pub fn run_action(config: &Config, name: String) {
  62. let action = config.actions.get(&name.to_string());
  63. match action {
  64. Some(action) => {
  65. if !action.disabled {
  66. run_command(action.command.to_string());
  67. }
  68. },
  69. None => panic!("No known action named \"{}\".", name)
  70. }
  71. }
  72. pub fn run_repository_sync(config: &Config, name: String) {
  73. let repository = config.repositories.get(&name.to_string());
  74. match repository {
  75. Some(repository) => {
  76. if !repository.disabled {
  77. let location = match config.is_not_default {
  78. true => {
  79. let thing = config.base_path.join(Path::new(&repository.location.to_string()));
  80. thing.to_str().unwrap()
  81. },
  82. _ => repository.location.as_str()
  83. };
  84. if !Path::new(&location).exists() {
  85. if repository.auto_create {
  86. run_repository_creation(config, name);
  87. }
  88. } else {
  89. let mut options: HashMap<&str, &str> = HashMap::new();
  90. for (key, value) in &repository.options {
  91. options.insert(key, value);
  92. }
  93. options.insert("location", location);
  94. let repo_type = config.repo_types.get(&repository.repo_type);
  95. match repo_type {
  96. Some(repo_type) => {
  97. println!("\n\nRepository {} ({}):", name, location);
  98. run_command_in_directory(location.to_string(),
  99. Template::new(&repo_type.pre_inward).render(&options));
  100. run_command_in_directory(location.to_string(),
  101. Template::new(&repo_type.inward).render(&options));
  102. run_command_in_directory(location.to_string(),
  103. Template::new(&repo_type.post_inward).render(&options));
  104. run_command_in_directory(location.to_string(),
  105. Template::new(&repo_type.outward).render(&options));
  106. run_command_in_directory(location.to_string(),
  107. Template::new(&repo_type.post_outward).render(&options));
  108. },
  109. None => panic!("No known repository type named \"{}\".", &repository.repo_type)
  110. }
  111. }
  112. }
  113. },
  114. None => panic!("No known repository named \"{}\".", name)
  115. }
  116. }
  117. pub fn run_repository_creation(config: &Config, name: String) {
  118. let repository = config.repositories.get(&name.to_string());
  119. match repository {
  120. Some(repository) => {
  121. let repository_type_name = &repository.repo_type;
  122. let repository_type = config.repo_types.get(repository_type_name);
  123. match repository_type {
  124. Some(repository_type) => {
  125. if !repository.disabled {
  126. let mut options: HashMap<&str, &str> = HashMap::new();
  127. for (key, value) in &repository.options {
  128. options.insert(key, value);
  129. }
  130. options.insert("location", &repository.location);
  131. run_command(Template::new(&repository_type.create).render(&options));
  132. }
  133. },
  134. None => panic!("No known repository type named \"{}\".", repository_type_name)
  135. }
  136. },
  137. None => panic!("No known repository named \"{}\".", name)
  138. }
  139. }
  140. pub fn run_group(config: &Config, name: String) {
  141. let group = config.groups.get(&name.to_string());
  142. match group {
  143. Some(group) => {
  144. for member in &group.members {
  145. if config.repositories.contains_key(member) {
  146. run_repository_sync(&config, member.to_string());
  147. } else if config.groups.contains_key(member) {
  148. run_group(&config, member.to_string());
  149. } else {
  150. println!("\"{}\" is neither a group nor a repository.", member);
  151. }
  152. }
  153. for action in &group.actions_after {
  154. run_action(&config, action.to_string());
  155. }
  156. },
  157. None => panic!("No known group named \"{}\".", name)
  158. }
  159. }