Skip to content
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

Merged

Conversation

Anyitechs
Copy link

@Anyitechs Anyitechs commented Apr 24, 2024

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:

  • An enum (ClnNetwork) with Mainnet, Testnet, Signet, and Regtest as variants
  • A ClnNetwork implementation (impl) with a from_str function that matches on the network (&str) argument and returns a ClnNetwork variant, and to_str function that matches on the ClnNetwork variant and returns the network String.
  • The 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.

ifeanyichukwu@IfeanyiukwusMBP coffee % coffee -n remote list
● Plugins installed
╭───────────────────────────────────────────╮
│ ●   Language   Name   Enabled   Exec path 
├───────────────────────────────────────────┤
╰───────────────────────────────────────────╯

How to test

  • cd into the coffee directory
  • Run make install
  • Run coffee -n remote list

Copy link

netlify bot commented Apr 24, 2024

Deploy Preview for coffee-docs canceled.

Name Link
🔨 Latest commit ddaa0dc
🔍 Latest deploy log https://app.netlify.com/sites/coffee-docs/deploys/663e32a72b1e1600078188cd

@@ -1,5 +1,6 @@
//! Coffee command line arguments definition.
use clap::{Parser, Subcommand};
use coffee_lib::{error, errors::CoffeeError};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
use coffee_lib::{error, errors::CoffeeError};
use coffee_lib::error;
use coffee_lib::errors::CoffeeError;

Comment on lines 148 to 153
match network {
"mainnet" => Some(Self::Mainnet),
"testnet" => Some(Self::Testnet),
"signet" => Some(Self::Signet),
"regtest" => Some(Self::Regtest),
_ => None,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about Liquid?

Comment on lines 157 to 173
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()),
}
}
Copy link
Contributor

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

Comment on lines 146 to 153
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,
Copy link
Contributor

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

Comment on lines 181 to 182
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()?;
Copy link
Contributor

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.

@Anyitechs
Copy link
Author

Anyitechs commented Apr 25, 2024

Thank you for the review. I'll work on the requested changes and update the PR.

@Anyitechs Anyitechs marked this pull request as ready for review May 9, 2024 22:12
Copy link
Contributor

@vincenzopalazzo vincenzopalazzo left a 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

coffee_cmd/src/cmd.rs Outdated Show resolved Hide resolved
coffee_cmd/src/cmd.rs Outdated Show resolved Hide resolved
coffee_cmd/src/cmd.rs Outdated Show resolved Hide resolved
coffee_cmd/src/cmd.rs Outdated Show resolved Hide resolved
@Anyitechs
Copy link
Author

Good code, some small minor changes

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.

Copy link
Contributor

@vincenzopalazzo vincenzopalazzo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK ddaa0dc

Comment on lines +3 to +5
use coffee_lib::error;
use coffee_lib::errors::CoffeeError;
use std::fmt::Display;
Copy link
Contributor

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

Copy link
Author

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!

Copy link
Author

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.

@vincenzopalazzo vincenzopalazzo merged commit fb966e7 into coffee-tools:master May 10, 2024
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Network Validation in Command Line Inputs
2 participants