main.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // imag - the personal information management suite for the commandline
  3. // Copyright (C) 2015, 2016 Matthias Beyer <mail@beyermatthias.de> and contributors
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; version
  8. // 2.1 of the License.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. //
  19. #![deny(
  20. non_camel_case_types,
  21. non_snake_case,
  22. path_statements,
  23. trivial_numeric_casts,
  24. unstable_features,
  25. unused_allocation,
  26. unused_import_braces,
  27. unused_imports,
  28. unused_must_use,
  29. unused_mut,
  30. unused_qualifications,
  31. while_true,
  32. )]
  33. #[macro_use] extern crate log;
  34. #[macro_use] extern crate version;
  35. extern crate clap;
  36. extern crate libimagcounter;
  37. extern crate libimagrt;
  38. extern crate libimagerror;
  39. extern crate libimagutil;
  40. use std::process::exit;
  41. use std::str::FromStr;
  42. use libimagrt::setup::generate_runtime_setup;
  43. use libimagcounter::counter::Counter;
  44. use libimagerror::trace::MapErrTrace;
  45. use libimagutil::key_value_split::IntoKeyValue;
  46. use libimagutil::info_result::*;
  47. mod create;
  48. mod delete;
  49. mod interactive;
  50. mod list;
  51. mod ui;
  52. use ui::build_ui;
  53. use create::create;
  54. use delete::delete;
  55. use interactive::interactive;
  56. use list::list;
  57. enum Action {
  58. Inc,
  59. Dec,
  60. Reset,
  61. Set,
  62. }
  63. fn main() {
  64. let rt = generate_runtime_setup("imag-counter",
  65. &version!()[..],
  66. "Counter tool to count things",
  67. build_ui);
  68. rt.cli()
  69. .subcommand_name()
  70. .map_or_else(|| {
  71. let (action, name) = {
  72. if rt.cli().is_present("increment") {
  73. (Action::Inc, rt.cli().value_of("increment").unwrap())
  74. } else if rt.cli().is_present("decrement") {
  75. (Action::Dec, rt.cli().value_of("decrement").unwrap())
  76. } else if rt.cli().is_present("reset") {
  77. (Action::Reset, rt.cli().value_of("reset").unwrap())
  78. } else /* rt.cli().is_present("set") */ {
  79. (Action::Set, rt.cli().value_of("set").unwrap())
  80. }
  81. };
  82. match action {
  83. Action::Inc => {
  84. Counter::load(String::from(name), rt.store())
  85. .map(|mut c| c.inc().map_err_trace_exit(1).map_info_str("Ok"))
  86. },
  87. Action::Dec => {
  88. Counter::load(String::from(name), rt.store())
  89. .map(|mut c| c.dec().map_err_trace_exit(1).map_info_str("Ok"))
  90. },
  91. Action::Reset => {
  92. Counter::load(String::from(name), rt.store())
  93. .map(|mut c| c.reset().map_err_trace_exit(1).map_info_str("Ok"))
  94. },
  95. Action::Set => {
  96. let kv = String::from(name).into_kv();
  97. if kv.is_none() {
  98. warn!("Not a key-value pair: '{}'", name);
  99. exit(1);
  100. }
  101. let (key, value) = kv.unwrap().into();
  102. let value = FromStr::from_str(&value[..]);
  103. if value.is_err() {
  104. warn!("Not a integer: '{:?}'", value);
  105. exit(1);
  106. }
  107. let value : i64 = value.unwrap();
  108. Counter::load(String::from(key), rt.store())
  109. .map(|mut c| c.set(value).map_err_trace_exit(1).map_info_str("Ok"))
  110. },
  111. }
  112. .map_err_trace()
  113. .ok();
  114. },
  115. |name| {
  116. debug!("Call: {}", name);
  117. match name {
  118. "create" => create(&rt),
  119. "delete" => delete(&rt),
  120. "interactive" => interactive(&rt),
  121. "list" => list(&rt),
  122. _ => {
  123. debug!("Unknown command"); // More error handling
  124. },
  125. };
  126. })
  127. }