This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Add config
command
#294
Open
abhi3700
wants to merge
21
commits into
main
Choose a base branch
from
add-config-cmd
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add config
command
#294
Changes from 8 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
95d3996
added boilerplate for 'config' command
abhi3700 4f08f9c
Added logic for config function
abhi3700 93fed0b
added individual options possibility in config command
abhi3700 15c4f9e
replaced bail w println because of better CLI interface that doesn no…
abhi3700 313b7bf
changed the logic from 'set one param only' to 'either or all params'
abhi3700 7f558ef
config command modified
abhi3700 b56e71a
Refactor utils and correct command order in interactive mode
abhi3700 b9f9bcb
Manual testing of create_or_move_data fn, doc update for parameters
abhi3700 68f413a
Merge branch 'main' of https://github.com/subspace/pulsar into add-co…
abhi3700 a9826fa
added missing pkg: serde_json
abhi3700 ef9072f
ensured check for already set chain, wipe after new chain set confirm…
abhi3700 8ea0009
fixed clippy errors
abhi3700 bb4a85f
fixed 'cargo doc' issues
abhi3700 a82110f
fixed build doc error
abhi3700 fd985c5
Remove show flag from config command
abhi3700 689b061
Correct rust doc for config
abhi3700 fb6c772
Add tests for fn used in config fn
abhi3700 208e297
Add tests
abhi3700 9b2539e
Updated tests for 'create_or_move_data' fn
abhi3700 96e408b
removed redundant check
abhi3700 3caa3c7
Refactor code with 'bail!'
abhi3700 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub(crate) mod config; | ||
pub(crate) mod farm; | ||
pub(crate) mod info; | ||
pub(crate) mod init; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
//! Config CLI command of pulsar is about setting either or all of the | ||
//! parameters: | ||
//! - chain | ||
//! - farm size | ||
//! - reward address | ||
//! - node directory | ||
//! - farm directory | ||
//! | ||
//! and showing the set config details. | ||
//! | ||
//! ## Usage | ||
//! | ||
//! ### Show | ||
//! ``` | ||
//! $ pulsar config -s | ||
//! Current Config set as: | ||
//! { | ||
//! "chain": "Gemini3g", | ||
//! "farmer": { | ||
//! "reward_address": "06fef8efdd808a95500e5baee2bcde4cf8d5e1191b2b3f93065f10f0e4648c09", | ||
//! "farm_directory": "/Users/abhi3700/Library/Application Support/pulsar/farms", | ||
//! "farm_size": "3.0 GB" | ||
//! }, | ||
//! "node": { | ||
//! "directory": "/Users/abhi3700/Library/Application Support/pulsar/node", | ||
//! "name": "abhi3700" | ||
//! } | ||
//! } | ||
//! in file: "/Users/abhi3700/Library/Application Support/pulsar/settings.toml" | ||
//! ``` | ||
//! | ||
//! ### Chain | ||
//! ``` | ||
//! $ pulsar config -c devnet | ||
//! ``` | ||
//! | ||
//! ### Farm size | ||
//! ``` | ||
//! $ pulsar config -f 3GB | ||
//! ``` | ||
//! | ||
//! ### Reward address | ||
//! | ||
//! ``` | ||
//! $ pulsar config -r 5CDstQSbxPrPAaRTuVR2n9PHuhGYnnQvXdbJSQmodD5i16x2 | ||
//! ``` | ||
//! | ||
//! ### Node directory | ||
//! ``` | ||
//! $ pulsar config -n "/Users/abhi3700/test/pulsar1/node" | ||
//! ``` | ||
//! | ||
//! ### Farm directory | ||
//! ``` | ||
//! $ pulsar config -d "/Users/abhi3700/test/pulsar1/farms" | ||
//! ``` | ||
//! | ||
//! ### All params | ||
//! ``` | ||
//! $ pulsar config \ | ||
//! --chain devnet \ | ||
//! --farm-size 5GB \ | ||
//! --reward-address 5DXRtoHJePQBEk44onMy5yG4T8CjpPaK4qKNmrwpxqxZALGY \ | ||
//! --node-directory "/Users/abhi3700/test/pulsar1/node" \ | ||
//! --farm-directory "/Users/abhi3700/test/pulsar1/farms" | ||
//! ``` | ||
|
||
use std::fs; | ||
use std::path::PathBuf; | ||
use std::str::FromStr; | ||
|
||
use color_eyre::eyre::{self, bail}; | ||
|
||
use crate::commands::wipe::wipe; | ||
use crate::config::{parse_config, parse_config_path, ChainConfig, Config}; | ||
use crate::utils::{create_or_move_data, reward_address_parser, size_parser}; | ||
|
||
// function for config cli command | ||
pub(crate) async fn config( | ||
show: bool, | ||
chain: Option<String>, | ||
farm_size: Option<String>, | ||
reward_address: Option<String>, | ||
node_dir: Option<String>, | ||
farm_dir: Option<String>, | ||
) -> eyre::Result<()> { | ||
// Define the path to your settings.toml file | ||
let config_path = parse_config_path()?; | ||
|
||
// if config file doesn't exist, then throw error | ||
if !config_path.exists() { | ||
bail!("Config file: \"settings.toml\" not found.\nPlease use `pulsar init` command first."); | ||
} | ||
|
||
// Load the current configuration | ||
let mut config: Config = parse_config()?; | ||
|
||
if show { | ||
// Display the current configuration as JSON | ||
// Serialize `config` to a pretty-printed JSON string | ||
let serialized = serde_json::to_string_pretty(&config)?; | ||
println!( | ||
"Current Config set as: \n{}\nin file: {:?}", | ||
serialized, | ||
config_path.to_str().expect("Expected stringified config path") | ||
); | ||
} else { | ||
// no options provided | ||
if chain.is_none() | ||
&& farm_size.is_none() | ||
&& reward_address.is_none() | ||
&& node_dir.is_none() | ||
&& farm_dir.is_none() | ||
{ | ||
println!("At least one option has to be provided.\nTry `pulsar config -h`"); | ||
return Ok(()); | ||
} | ||
|
||
// update (optional) the chain | ||
if let Some(c) = chain { | ||
config.chain = ChainConfig::from_str(&c)?; | ||
println!("Chain updated as {:?}", c); | ||
|
||
// wipe everything (farmer, node, summary) except config file | ||
wipe(true, true, true, false).await.expect("Error while wiping."); | ||
} | ||
|
||
// update (optional) the farm size | ||
if let Some(ref f) = farm_size { | ||
let farm_size = size_parser(&f)?; | ||
config.farmer.farm_size = farm_size; | ||
} | ||
|
||
// update (optional) the reward address | ||
if let Some(ref r) = reward_address { | ||
let reward_address = reward_address_parser(&r)?; | ||
config.farmer.reward_address = reward_address; | ||
} | ||
|
||
// update (optional) the node directory | ||
if let Some(ref n) = node_dir { | ||
let node_dir = PathBuf::from_str(&n).expect("Invalid node directory"); | ||
create_or_move_data(config.node.directory.clone(), node_dir.clone())?; | ||
config.node.directory = node_dir; | ||
} | ||
|
||
// update (optional) the farm directory | ||
if let Some(ref fd) = farm_dir { | ||
let farm_dir = PathBuf::from_str(&fd).expect("Invalid farm directory"); | ||
create_or_move_data(config.farmer.farm_directory.clone(), farm_dir.clone())?; | ||
if farm_dir.exists() { | ||
config.farmer.farm_directory = farm_dir; | ||
} | ||
} | ||
|
||
// Save the updated configuration back to the file | ||
fs::write(config_path, toml::to_string(&config)?)?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe check if the chain has changed before wiping it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although redundant.
Added here in this commit.
Additionally, added check for parsing already set chain.