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.

108 lines
3.6KB

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