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.

main.rs 4.0KB

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