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.

110 lines
3.5KB

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