run.rs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 = &repository.location;
  78. if !Path::new(&location).exists() {
  79. if repository.auto_create {
  80. run_repository_creation(config, name);
  81. }
  82. } else {
  83. let mut options: HashMap<&str, &str> = HashMap::new();
  84. for (key, value) in &repository.options {
  85. options.insert(key, value);
  86. }
  87. options.insert("location", location);
  88. let repo_type = config.repo_types.get(&repository.repo_type);
  89. match repo_type {
  90. Some(repo_type) => {
  91. println!("\n\nRepository {} ({}):", name, location);
  92. run_command_in_directory(location.to_string(),
  93. Template::new(&repo_type.pre_inward).render(&options));
  94. run_command_in_directory(location.to_string(),
  95. Template::new(&repo_type.inward).render(&options));
  96. run_command_in_directory(location.to_string(),
  97. Template::new(&repo_type.post_inward).render(&options));
  98. run_command_in_directory(location.to_string(),
  99. Template::new(&repo_type.outward).render(&options));
  100. run_command_in_directory(location.to_string(),
  101. Template::new(&repo_type.post_outward).render(&options));
  102. },
  103. None => panic!("No known repository type named \"{}\".", &repository.repo_type)
  104. }
  105. }
  106. }
  107. },
  108. None => panic!("No known repository named \"{}\".", name)
  109. }
  110. }
  111. pub fn run_repository_creation(config: &Config, name: String) {
  112. let repository = config.repositories.get(&name.to_string());
  113. match repository {
  114. Some(repository) => {
  115. let repository_type_name = &repository.repo_type;
  116. let repository_type = config.repo_types.get(repository_type_name);
  117. match repository_type {
  118. Some(repository_type) => {
  119. if !repository.disabled {
  120. let mut options: HashMap<&str, &str> = HashMap::new();
  121. for (key, value) in &repository.options {
  122. options.insert(key, value);
  123. }
  124. options.insert("location", &repository.location);
  125. run_command(Template::new(&repository_type.create).render(&options));
  126. }
  127. },
  128. None => panic!("No known repository type named \"{}\".", repository_type_name)
  129. }
  130. },
  131. None => panic!("No known repository named \"{}\".", name)
  132. }
  133. }
  134. pub fn run_group(config: &Config, name: String) {
  135. let group = config.groups.get(&name.to_string());
  136. match group {
  137. Some(group) => {
  138. for member in &group.members {
  139. if config.repositories.contains_key(member) {
  140. run_repository_sync(&config, member.to_string());
  141. } else if config.groups.contains_key(member) {
  142. run_group(&config, member.to_string());
  143. } else {
  144. println!("\"{}\" is neither a group nor a repository.", member);
  145. }
  146. }
  147. for action in &group.actions_after {
  148. run_action(&config, action.to_string());
  149. }
  150. },
  151. None => panic!("No known group named \"{}\".", name)
  152. }
  153. }