-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(cmd): add network validation in coffee cmd inputs #274
fix(cmd): add network validation in coffee cmd inputs #274
Conversation
✅ Deploy Preview for coffee-docs canceled.
|
coffee_cmd/src/cmd.rs
Outdated
@@ -1,5 +1,6 @@ | |||
//! Coffee command line arguments definition. | |||
use clap::{Parser, Subcommand}; | |||
use coffee_lib::{error, errors::CoffeeError}; |
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.
use coffee_lib::{error, errors::CoffeeError}; | |
use coffee_lib::error; | |
use coffee_lib::errors::CoffeeError; |
coffee_cmd/src/cmd.rs
Outdated
match network { | ||
"mainnet" => Some(Self::Mainnet), | ||
"testnet" => Some(Self::Testnet), | ||
"signet" => Some(Self::Signet), | ||
"regtest" => Some(Self::Regtest), | ||
_ => None, |
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.
What about Liquid?
coffee_cmd/src/cmd.rs
Outdated
fn to_str(network: Self) -> Option<String> { | ||
match network { | ||
Self::Mainnet => Some("mainnet".to_string()), | ||
Self::Testnet => Some("testnet".to_string()), | ||
Self::Signet => Some("signet".to_string()), | ||
Self::Regtest => Some("regtest".to_string()), | ||
} | ||
} |
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.
implement the std::fmt::Display
trait not use this an hack
coffee_cmd/src/cmd.rs
Outdated
fn from_str(network: &str) -> Option<Self> { | ||
|
||
match network { | ||
"mainnet" => Some(Self::Mainnet), | ||
"testnet" => Some(Self::Testnet), | ||
"signet" => Some(Self::Signet), | ||
"regtest" => Some(Self::Regtest), | ||
_ => None, |
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.
implement the From<&str>
trait, do not reivent rust
coffee_cmd/src/cmd.rs
Outdated
let network = self.network.clone().ok_or_else(|| error!("Network is not set")).ok()?; | ||
let validated_network = ClnNetwork::from_str(&network.to_lowercase()).ok_or_else(|| error!("Invalid network name")).ok()?; |
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.
I think you do not run make
command, this code looks like unformatted.
Thank you for the review. I'll work on the requested changes and update the PR. |
ece6c69
to
71a76dc
Compare
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.
Good code, some small minor changes
71a76dc
to
ddaa0dc
Compare
Thank you for the review. I've addressed all the requested changes and updated the PR. Let me know if there's anything I'm missing. |
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.
ACK ddaa0dc
use coffee_lib::error; | ||
use coffee_lib::errors::CoffeeError; | ||
use std::fmt::Display; |
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.
look at how we organize the import, we follow the following rules
std import
external dependencies import
coffee importa
crate import
But this can be addressed in a followup PR
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.
Thank you for pointing that out. I'll address that asap!
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.
I've addressed this in #276.
This PR validates the argument passed into the
-n
flag, and ensures only supported CLN networks are created in the/Users/User/.coffee
directory, and closes #270 .Implementation
The current implementation consists of:
ClnNetwork
) withMainnet
,Testnet
,Signet
, andRegtest
as variantsClnNetwork
implementation (impl
) with afrom_str
function thatmatch
es on thenetwork
(&str
) argument and returns aClnNetwork
variant, andto_str
function thatmatch
es on theClnNetwork
variant and returns the networkString
.network
function validates the-n
argument.Current Behaviour
The validation works as expected, so passing in a random name as the network name will not create the directory but the command returns the same output as below regardless of the network name. Also, passing in a random network name and running
coffee -n remote list
for the first time ignores the invalid network name and creates a/Users/ifeanyichukwu/.coffee/bitcoin
directory for example, but running the command again doesn't create a../.coffee/bitcoin
directory again.How to test
coffee
directorymake install
coffee -n remote list