From 2aec9bec1953d51f5efeb79cd3ee83f768884305 Mon Sep 17 00:00:00 2001
From: Hugo Trentesaux <hugo@trentesaux.fr>
Date: Wed, 7 Jun 2023 12:37:31 +0200
Subject: [PATCH] WIP move address to conf

---
 doc/example.md | 13 +++++++++++
 src/conf.rs    |  5 +++++
 src/data.rs    | 61 ++++++++++++++++++++++++++++++--------------------
 3 files changed, 55 insertions(+), 24 deletions(-)

diff --git a/doc/example.md b/doc/example.md
index a046b3d..369d19b 100644
--- a/doc/example.md
+++ b/doc/example.md
@@ -23,6 +23,19 @@ with derivations:
 - `//Charlie`
 - ...
 
+## Configuration
+
+It can be handful to use Gcli with a configuration file to avoid passing arguments on every command.
+
+```sh
+# show config commands
+gcli config
+# show where config file is stored
+gcli config where
+# save config to use gdev network for next commands
+gcli --network gdev config save
+```
+
 ## Commands
 
 ```sh
diff --git a/src/conf.rs b/src/conf.rs
index 89a4c93..7b5afba 100644
--- a/src/conf.rs
+++ b/src/conf.rs
@@ -5,8 +5,12 @@ const APP_NAME: &str = "gcli";
 
 #[derive(Serialize, Deserialize, Debug)]
 pub struct Config {
+	// duniter endpoint
 	pub duniter_endpoint: String,
+	// indexer endpoint
 	pub indexer_endpoint: String,
+	// user address
+	pub address: Option<AccountId>,
 }
 
 impl std::default::Default for Config {
@@ -14,6 +18,7 @@ impl std::default::Default for Config {
 		Self {
 			duniter_endpoint: String::from(data::LOCAL_DUNITER_ENDPOINT),
 			indexer_endpoint: String::from(data::LOCAL_INDEXER_ENDPOINT),
+			address: None,
 		}
 	}
 }
diff --git a/src/data.rs b/src/data.rs
index cb30916..be67bec 100644
--- a/src/data.rs
+++ b/src/data.rs
@@ -33,8 +33,6 @@ pub struct Data {
 	pub client: Option<Client>,
 	// graphql to duniter-indexer
 	pub indexer: Option<Indexer>,
-	// user address
-	pub address: Option<AccountId>,
 	// user keypair
 	pub keypair: Option<Pair>,
 	// user identity index
@@ -79,7 +77,7 @@ impl Data {
 		self.indexer.as_ref().expect("indexer is not set up")
 	}
 	pub fn address(&self) -> AccountId {
-		self.address.clone().expect("an address is needed")
+		self.cfg.address.clone().expect("an address is needed")
 	}
 	pub fn keypair(&self) -> Pair {
 		self.keypair.clone().expect("a keypair is needed")
@@ -99,6 +97,7 @@ impl Data {
 	// --- mutators ---
 	/// use arguments to overwrite config
 	pub fn overwrite_from_args(mut self) -> Self {
+		// network
 		if let Some(network) = self.args.network.clone() {
 			// a network was provided as arugment
 			match &network[..] {
@@ -131,40 +130,54 @@ impl Data {
 				}
 			}
 		}
+		// duniter endpoint
 		if let Some(duniter_endpoint) = self.args.url.clone() {
 			self.cfg.duniter_endpoint = duniter_endpoint;
 		}
+		// indexer endpoint
 		if let Some(indexer_endpoint) = self.args.indexer.clone() {
 			self.cfg.indexer_endpoint = indexer_endpoint
 		}
+		// secret
+		if self.args.secret.is_some() {
+			self = self.build_keypair();
+		}
+		// address
+		if self.args.address.is_some() {
+			self = self.build_address();
+		}
 		self
 	}
-	/// force an address if needed
+	/// ask user to input an address if needed
 	pub fn build_address(mut self) -> Self {
-		self.address = Some(
-			get_keys(
+		if self.cfg.address.is_none() {
+			self.cfg.address = Some(
+				get_keys(
+					self.args.secret_format,
+					&self.args.address,
+					&self.args.secret,
+					NeededKeys::Public,
+				)
+				.expect("needed")
+				.0
+				.expect("needed"),
+			);
+		}
+		self
+	}
+	/// ask user to input a keypair if needed
+	pub fn build_keypair(mut self) -> Self {
+		if self.keypair.is_none() {
+			let (address, keypair) = get_keys(
 				self.args.secret_format,
 				&self.args.address,
 				&self.args.secret,
-				NeededKeys::Public,
+				NeededKeys::Secret,
 			)
-			.expect("needed")
-			.0
-			.expect("needed"),
-		);
-		self
-	}
-	/// force a keypair if needed
-	pub fn build_keypair(mut self) -> Self {
-		let (address, keypair) = get_keys(
-			self.args.secret_format,
-			&self.args.address,
-			&self.args.secret,
-			NeededKeys::Secret,
-		)
-		.expect("needed");
-		self.address = address;
-		self.keypair = keypair;
+			.expect("needed");
+			self.cfg.address = address;
+			self.keypair = keypair;
+		}
 		self
 	}
 	/// build a client from url
-- 
GitLab