A cli program to easily handle .gitignore files
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.

97 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. /*! Gitig is a small cli program to handle gitignore files
  2. It helps adding new entries to ignore and start a new project with templates from [Github](https://github.com/github/gitignore).
  3. This project used [rust-cli-boilerplate](https://github.com/ssokolow/rust-cli-boilerplate)
  4. */
  5. // Copyright 2017-2019, Stephan Sokolow
  6. // `error_chain` recursion adjustment
  7. #![recursion_limit = "1024"]
  8. // Make rustc's built-in lints more strict and set clippy into a whitelist-based configuration so
  9. // we see new lints as they get written (We'll opt back out selectively)
  10. #![warn(
  11. warnings,
  12. rust_2018_idioms,
  13. clippy::all,
  14. clippy::complexity,
  15. clippy::correctness,
  16. clippy::pedantic,
  17. clippy::perf,
  18. clippy::style,
  19. clippy::restriction
  20. )]
  21. // Opt out of the lints I've seen and don't want
  22. #![allow(
  23. clippy::float_arithmetic,
  24. clippy::implicit_return,
  25. clippy::filter_map,
  26. clippy::wildcard_imports,
  27. clippy::integer_arithmetic
  28. )]
  29. #[macro_use]
  30. extern crate error_chain;
  31. // stdlib imports
  32. use std::convert::TryInto;
  33. // 3rd-party imports
  34. mod errors;
  35. use log::trace;
  36. use structopt::StructOpt;
  37. // Local imports
  38. mod app;
  39. mod cache;
  40. mod gitignore;
  41. mod helpers;
  42. mod template;
  43. /// Boilerplate to parse command-line arguments, set up logging, and handle bubbled-up `Error`s.
  44. ///
  45. /// Based on the `StructOpt` example from stderrlog and the suggested error-chain harness from
  46. /// [quickstart.rs](https://github.com/brson/error-chain/blob/master/examples/quickstart.rs).
  47. ///
  48. /// See `app::main` for the application-specific logic.
  49. ///
  50. /// **TODO:** Consider switching to Failure and look into `impl Termination` as a way to avoid
  51. /// having to put the error message pretty-printing inside main()
  52. #[allow(clippy::result_expect_used, clippy::exit, clippy::use_debug)]
  53. fn main() {
  54. // Parse command-line arguments (exiting on parse error, --version, or --help)
  55. let opts = app::CliOpts::from_args();
  56. // Configure logging output so that -q is "decrease verbosity" rather than instant silence
  57. let verbosity = opts
  58. .boilerplate
  59. .verbose
  60. .saturating_add(app::DEFAULT_VERBOSITY)
  61. .saturating_sub(opts.boilerplate.quiet);
  62. stderrlog::new()
  63. .module(module_path!())
  64. .quiet(verbosity == 0)
  65. .verbosity(verbosity.saturating_sub(1).try_into().expect("should never even come close"))
  66. .timestamp(opts.boilerplate.timestamp.unwrap_or(stderrlog::Timestamp::Off))
  67. .init()
  68. .expect("initializing logging output");
  69. trace!("Initialized logging, {} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));
  70. trace!("Starting with application");
  71. if let Err(ref e) = app::main(opts) {
  72. use std::io::prelude::*;
  73. let stderr = std::io::stderr();
  74. let mut lock = stderr.lock();
  75. // Write the top-level error message, then chained errors, then backtrace if available
  76. writeln!(lock, "error: {}", e).expect("error while writing error");
  77. for e in e.iter().skip(1) {
  78. writeln!(lock, "caused by: {}", e).expect("error while writing error");
  79. }
  80. if let Some(backtrace) = e.backtrace() {
  81. writeln!(lock, "backtrace: {:?}", backtrace).expect("error while writing error");
  82. }
  83. std::process::exit(1);
  84. }
  85. }
  86. // vim: set sw=4 sts=4 expandtab :