Skip to content
Snippets Groups Projects
Commit 2aec9bec authored by Hugo Trentesaux's avatar Hugo Trentesaux Committed by Hugo Trentesaux
Browse files

WIP move address to conf

parent 6ccda071
No related branches found
No related tags found
1 merge request!7Big refacto
......@@ -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
......
......@@ -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,
}
}
}
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment