cli_config.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. use crate::serial::{deserialize, serialize, Decodable, Encodable};
  2. use crate::util::join_config_path;
  3. use crate::Result;
  4. use std::{fs::OpenOptions, io::prelude::*, path::PathBuf};
  5. pub trait ClientCliConfig: Encodable + Decodable + Default {
  6. fn load(path: PathBuf) -> Result<Self> {
  7. let path = join_config_path(&path)?;
  8. let mut file = OpenOptions::new()
  9. .read(true)
  10. .write(true)
  11. .create(true)
  12. .open(path)?;
  13. let mut buffer: Vec<u8> = vec![];
  14. file.read_to_end(&mut buffer)?;
  15. if !buffer.is_empty() {
  16. let config: Self = deserialize(&buffer)?;
  17. Ok(config)
  18. } else {
  19. Ok(Self::default())
  20. }
  21. }
  22. fn save(&self, path: PathBuf) -> Result<()> {
  23. let path = join_config_path(&path)?;
  24. let mut file = OpenOptions::new().write(true).create(true).open(&path)?;
  25. let serialized = serialize(self);
  26. file.write_all(&serialized)?;
  27. Ok(())
  28. }
  29. }
  30. impl ClientCliConfig for DarkfiCliConfig {}
  31. impl ClientCliConfig for DarkfidCliConfig {}
  32. pub struct DarkfiCliConfig {
  33. pub rpc_url: String,
  34. pub log_path: String,
  35. }
  36. pub struct DarkfidCliConfig {
  37. pub connect_url: String,
  38. pub subscriber_url: String,
  39. pub rpc_url: String,
  40. pub database_path: String,
  41. pub log_path: String,
  42. }
  43. impl Default for DarkfiCliConfig {
  44. fn default() -> Self {
  45. let rpc_url = String::from("http://127.0.0.1:8000");
  46. let log_path = String::from("/tmp/darkfi_cli.log");
  47. Self {
  48. rpc_url,
  49. log_path,
  50. }
  51. }
  52. }
  53. impl Encodable for DarkfidCliConfig {
  54. fn encode<S: std::io::Write>(&self, mut s: S) -> Result<usize> {
  55. let mut len = 0;
  56. len += self.connect_url.encode(&mut s)?;
  57. len += self.subscriber_url.encode(&mut s)?;
  58. len += self.rpc_url.encode(&mut s)?;
  59. len += self.database_path.encode(&mut s)?;
  60. len += self.log_path.encode(&mut s)?;
  61. Ok(len)
  62. }
  63. }
  64. impl Decodable for DarkfidCliConfig {
  65. fn decode<D: std::io::Read>(mut d: D) -> Result<Self> {
  66. Ok(Self {
  67. connect_url: Decodable::decode(&mut d)?,
  68. subscriber_url: Decodable::decode(&mut d)?,
  69. rpc_url: Decodable::decode(&mut d)?,
  70. database_path: Decodable::decode(&mut d)?,
  71. log_path: Decodable::decode(&mut d)?,
  72. })
  73. }
  74. }
  75. impl Default for DarkfidCliConfig {
  76. fn default() -> Self {
  77. let connect_url = String::from("127.0.0.1:3333");
  78. let subscriber_url = String::from("127.0.0.1:4444");
  79. let rpc_url = String::from("127.0.0.1:8000");
  80. let database_path = String::from("database_client.db");
  81. let database_path = join_config_path(&PathBuf::from(database_path))
  82. .expect("error during join database_path to config path");
  83. let database_path = String::from(
  84. database_path
  85. .to_str()
  86. .expect("error convert Path to String"),
  87. );
  88. let log_path = String::from("/tmp/darkfid_service_daemon.log");
  89. Self {
  90. connect_url,
  91. subscriber_url,
  92. rpc_url,
  93. database_path,
  94. log_path,
  95. }
  96. }
  97. }
  98. impl Encodable for DarkfiCliConfig {
  99. fn encode<S: std::io::Write>(&self, mut s: S) -> Result<usize> {
  100. let mut len = 0;
  101. len += self.rpc_url.encode(&mut s)?;
  102. len += self.log_path.encode(&mut s)?;
  103. Ok(len)
  104. }
  105. }
  106. impl Decodable for DarkfiCliConfig {
  107. fn decode<D: std::io::Read>(mut d: D) -> Result<Self> {
  108. Ok(Self {
  109. rpc_url: Decodable::decode(&mut d)?,
  110. log_path: Decodable::decode(&mut d)?,
  111. })
  112. }
  113. }