program_options.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. use clap::{Arg, ArgMatches, Command};
  2. use std::net::SocketAddr;
  3. use darkfi::{net, Result};
  4. pub struct ProgramOptions {
  5. pub network_settings: net::Settings,
  6. pub log_path: Box<std::path::PathBuf>,
  7. pub irc_accept_addr: SocketAddr,
  8. pub rpc_listen_addr: SocketAddr,
  9. pub app: ArgMatches,
  10. }
  11. impl ProgramOptions {
  12. pub fn load() -> Result<ProgramOptions> {
  13. let app = Command::new("dfi")
  14. .version("0.1.0")
  15. .author("Amir Taaki <amir@dyne.org>")
  16. .about("Dark node")
  17. .arg(
  18. Arg::new("ACCEPT")
  19. .short('a')
  20. .long("accept")
  21. .value_name("ACCEPT")
  22. .help("Accept address")
  23. .takes_value(true),
  24. )
  25. .arg(
  26. Arg::new("SEED_NODES")
  27. .short('s')
  28. .long("seeds")
  29. .value_name("SEED_NODES")
  30. .help("Seed nodes")
  31. .takes_value(true),
  32. )
  33. .arg(
  34. Arg::new("CONNECTS")
  35. .short('c')
  36. .long("connect")
  37. .value_name("CONNECTS")
  38. .help("Manual connections")
  39. .takes_value(true),
  40. )
  41. .arg(
  42. Arg::new("CONNECT_SLOTS")
  43. .long("slots")
  44. .value_name("CONNECT_SLOTS")
  45. .help("Connection slots")
  46. .takes_value(true),
  47. )
  48. .arg(
  49. Arg::new("EXTERNAL_ADDR")
  50. .short('e')
  51. .long("external")
  52. .value_name("EXTERNAL_ADDR")
  53. .help("External address")
  54. .takes_value(true),
  55. )
  56. .arg(
  57. Arg::new("LOG_PATH")
  58. .long("log")
  59. .value_name("LOG_PATH")
  60. .help("Logfile path")
  61. .takes_value(true),
  62. )
  63. .arg(
  64. Arg::new("IRC_ACCEPT")
  65. .short('r')
  66. .long("irc")
  67. .value_name("IRC_ACCEPT")
  68. .help("IRC accept address")
  69. .takes_value(true),
  70. )
  71. .arg(
  72. Arg::new("RPC_LISTEN")
  73. .long("rpc")
  74. .value_name("RPC_LISTEN")
  75. .help("RPC listen address")
  76. .takes_value(true),
  77. )
  78. .arg(
  79. Arg::new("verbose")
  80. .short('v')
  81. .long("verbose")
  82. .multiple_occurrences(true)
  83. .help("Sets the level of verbosity"),
  84. )
  85. .get_matches();
  86. let accept_addr = if let Some(accept_addr) = app.value_of("ACCEPT") {
  87. Some(accept_addr.parse()?)
  88. } else {
  89. None
  90. };
  91. let mut seed_addrs: Vec<SocketAddr> = vec![];
  92. if let Some(seeds) = app.values_of("SEED_NODES") {
  93. for seed in seeds {
  94. seed_addrs.push(seed.parse()?);
  95. }
  96. }
  97. let mut manual_connects: Vec<SocketAddr> = vec![];
  98. if let Some(connections) = app.values_of("CONNECTS") {
  99. for connect in connections {
  100. manual_connects.push(connect.parse()?);
  101. }
  102. }
  103. let connection_slots = if let Some(connection_slots) = app.value_of("CONNECT_SLOTS") {
  104. connection_slots.parse()?
  105. } else {
  106. 0
  107. };
  108. let external_addr = if let Some(external_addr) = app.value_of("EXTERNAL_ADDR") {
  109. Some(external_addr.parse()?)
  110. } else {
  111. None
  112. };
  113. let log_path = Box::new(
  114. if let Some(log_path) = app.value_of("LOG_PATH") {
  115. std::path::Path::new(log_path)
  116. } else {
  117. std::path::Path::new("/tmp/darkfid.log")
  118. }
  119. .to_path_buf(),
  120. );
  121. let irc_accept_addr = if let Some(accept_addr) = app.value_of("IRC_ACCEPT") {
  122. accept_addr.parse()?
  123. } else {
  124. ([127, 0, 0, 1], 6667).into()
  125. };
  126. let rpc_listen_addr = if let Some(rpc_addr) = app.value_of("RPC_LISTEN") {
  127. rpc_addr.parse()?
  128. } else {
  129. ([127, 0, 0, 1], 8000).into()
  130. };
  131. Ok(ProgramOptions {
  132. network_settings: net::Settings {
  133. inbound: accept_addr,
  134. outbound_connections: connection_slots,
  135. external_addr,
  136. peers: manual_connects,
  137. seeds: seed_addrs,
  138. ..Default::default()
  139. },
  140. log_path,
  141. irc_accept_addr,
  142. rpc_listen_addr,
  143. app,
  144. })
  145. }
  146. }