You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
4.6KB

  1. use std::env;
  2. use std::path::PathBuf;
  3. use std::time::Instant;
  4. use utils::net::{get_available_port, port_is_available};
  5. mod cli;
  6. mod cmd;
  7. mod console;
  8. mod prompt;
  9. fn main() {
  10. let matches = cli::build_cli().get_matches();
  11. let root_dir = match matches.value_of("root").unwrap() {
  12. "." => env::current_dir().unwrap(),
  13. path => PathBuf::from(path),
  14. };
  15. let config_file = matches.value_of("config").unwrap();
  16. match matches.subcommand() {
  17. ("init", Some(matches)) => {
  18. match cmd::create_new_project(matches.value_of("name").unwrap()) {
  19. Ok(()) => (),
  20. Err(e) => {
  21. console::unravel_errors("Failed to create the project", &e);
  22. ::std::process::exit(1);
  23. }
  24. };
  25. }
  26. ("build", Some(matches)) => {
  27. console::info("Building site...");
  28. let start = Instant::now();
  29. let output_dir = matches.value_of("output_dir").unwrap();
  30. match cmd::build(
  31. &root_dir,
  32. config_file,
  33. matches.value_of("base_url"),
  34. output_dir,
  35. matches.is_present("drafts"),
  36. ) {
  37. Ok(()) => console::report_elapsed_time(start),
  38. Err(e) => {
  39. console::unravel_errors("Failed to build the site", &e);
  40. ::std::process::exit(1);
  41. }
  42. };
  43. }
  44. ("serve", Some(matches)) => {
  45. let interface = matches.value_of("interface").unwrap_or("127.0.0.1");
  46. let mut port: u16 = match matches.value_of("port").unwrap().parse() {
  47. Ok(x) => x,
  48. Err(_) => {
  49. console::error("The request port needs to be an integer");
  50. ::std::process::exit(1);
  51. }
  52. };
  53. let watch_only = matches.is_present("watch_only");
  54. let open = matches.is_present("open");
  55. let include_drafts = matches.is_present("drafts");
  56. // Default one
  57. if port != 1111 && !watch_only && !port_is_available(port) {
  58. console::error("The requested port is not available");
  59. ::std::process::exit(1);
  60. }
  61. if !watch_only && !port_is_available(port) {
  62. port = if let Some(p) = get_available_port(1111) {
  63. p
  64. } else {
  65. console::error("No port available.");
  66. ::std::process::exit(1);
  67. }
  68. }
  69. let output_dir = matches.value_of("output_dir").unwrap();
  70. let base_url = matches.value_of("base_url").unwrap();
  71. console::info("Building site...");
  72. match cmd::serve(
  73. &root_dir,
  74. interface,
  75. port,
  76. output_dir,
  77. base_url,
  78. config_file,
  79. watch_only,
  80. open,
  81. include_drafts,
  82. ) {
  83. Ok(()) => (),
  84. Err(e) => {
  85. console::unravel_errors("", &e);
  86. ::std::process::exit(1);
  87. }
  88. };
  89. }
  90. ("check", Some(matches)) => {
  91. console::info("Checking site...");
  92. let start = Instant::now();
  93. match cmd::check(
  94. &root_dir,
  95. config_file,
  96. matches.value_of("base_path"),
  97. matches.value_of("base_url"),
  98. matches.is_present("drafts"),
  99. ) {
  100. Ok(()) => console::report_elapsed_time(start),
  101. Err(e) => {
  102. console::unravel_errors("Failed to check the site", &e);
  103. ::std::process::exit(1);
  104. }
  105. };
  106. }
  107. ("index", Some(matches)) => {
  108. console::info("Building search index...");
  109. let start = Instant::now();
  110. let output_dir = matches.value_of("output_dir").unwrap();
  111. let index_type = matches.value_of("index_type").unwrap();
  112. match cmd::index(
  113. &root_dir,
  114. config_file,
  115. matches.value_of("base_url"),
  116. output_dir,
  117. matches.is_present("drafts"),
  118. index_type,
  119. ) {
  120. Ok(()) => console::report_elapsed_time(start),
  121. Err(e) => {
  122. console::unravel_errors("Failed to build search index", &e);
  123. ::std::process::exit(1);
  124. }
  125. };
  126. }
  127. _ => unreachable!(),
  128. }
  129. }