program_options.rs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. use std::net::SocketAddr;
  2. use darkfi::{net, Result};
  3. pub struct ProgramOptions {
  4. pub network_settings: net::Settings,
  5. pub log_path: Box<std::path::PathBuf>,
  6. pub irc_accept_addr: SocketAddr,
  7. }
  8. impl ProgramOptions {
  9. pub fn load() -> Result<ProgramOptions> {
  10. let app = clap_app!(dfi =>
  11. (version: "0.1.0")
  12. (author: "Amir Taaki <amir@dyne.org>")
  13. (about: "Dark node")
  14. (@arg ACCEPT: -a --accept +takes_value "Accept address")
  15. (@arg SEED_NODES: -s --seeds +takes_value ... "Seed nodes")
  16. (@arg CONNECTS: -c --connect +takes_value ... "Manual connections")
  17. (@arg CONNECT_SLOTS: --slots +takes_value "Connection slots")
  18. (@arg EXTERNAL_ADDR: -e --external +takes_value "External address")
  19. (@arg LOG_PATH: --log +takes_value "Logfile path")
  20. (@arg IRC_ACCEPT: -r --irc +takes_value "IRC accept address")
  21. )
  22. .get_matches();
  23. let accept_addr = if let Some(accept_addr) = app.value_of("ACCEPT") {
  24. Some(accept_addr.parse()?)
  25. } else {
  26. None
  27. };
  28. let mut seed_addrs: Vec<SocketAddr> = vec![];
  29. if let Some(seeds) = app.values_of("SEED_NODES") {
  30. for seed in seeds {
  31. seed_addrs.push(seed.parse()?);
  32. }
  33. }
  34. let mut manual_connects: Vec<SocketAddr> = vec![];
  35. if let Some(connections) = app.values_of("CONNECTS") {
  36. for connect in connections {
  37. manual_connects.push(connect.parse()?);
  38. }
  39. }
  40. let connection_slots = if let Some(connection_slots) = app.value_of("CONNECT_SLOTS") {
  41. connection_slots.parse()?
  42. } else {
  43. 0
  44. };
  45. let external_addr = if let Some(external_addr) = app.value_of("EXTERNAL_ADDR") {
  46. Some(external_addr.parse()?)
  47. } else {
  48. None
  49. };
  50. let log_path = Box::new(
  51. if let Some(log_path) = app.value_of("LOG_PATH") {
  52. std::path::Path::new(log_path)
  53. } else {
  54. std::path::Path::new("/tmp/darkfid.log")
  55. }
  56. .to_path_buf(),
  57. );
  58. let irc_accept_addr = if let Some(accept_addr) = app.value_of("IRC_ACCEPT") {
  59. accept_addr.parse()?
  60. } else {
  61. ([127, 0, 0, 1], 6667).into()
  62. };
  63. Ok(ProgramOptions {
  64. network_settings: net::Settings {
  65. inbound: accept_addr,
  66. outbound_connections: connection_slots,
  67. external_addr,
  68. peers: manual_connects,
  69. seeds: seed_addrs,
  70. ..Default::default()
  71. },
  72. log_path,
  73. irc_accept_addr,
  74. })
  75. }
  76. }