Browse Source

client: Cluster parses custom urls from str (#369)

Armani Ferrante 4 years ago
parent
commit
6ed71d7e68
3 changed files with 39 additions and 1 deletions
  1. 1 0
      CHANGELOG.md
  2. 1 0
      client/Cargo.toml
  3. 37 1
      client/src/cluster.rs

+ 1 - 0
CHANGELOG.md

@@ -14,6 +14,7 @@ incremented for features.
 ### Features
 ### Features
 
 
 * cli: Add `--program-name` option for build command to build a single program at a time ([#362](https://github.com/project-serum/anchor/pull/362)).
 * cli: Add `--program-name` option for build command to build a single program at a time ([#362](https://github.com/project-serum/anchor/pull/362)).
+* cli, client: Parse custom cluster urls from str ([#369](https://github.com/project-serum/anchor/pull/369)).
 
 
 ### Fixes
 ### Fixes
 
 

+ 1 - 0
client/Cargo.toml

@@ -14,3 +14,4 @@ serde = { version = "1.0.122", features = ["derive"] }
 solana-client = "1.6.6"
 solana-client = "1.6.6"
 solana-sdk = "1.6.6"
 solana-sdk = "1.6.6"
 thiserror = "1.0.20"
 thiserror = "1.0.20"
+url = "2.2.2"

+ 37 - 1
client/src/cluster.rs

@@ -1,6 +1,7 @@
-use anyhow::Result;
+use anyhow::{anyhow, Result};
 use serde::{Deserialize, Serialize};
 use serde::{Deserialize, Serialize};
 use std::str::FromStr;
 use std::str::FromStr;
+use url::Url;
 
 
 #[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
 #[derive(Serialize, Deserialize, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
 pub enum Cluster {
 pub enum Cluster {
@@ -27,6 +28,21 @@ impl FromStr for Cluster {
             "d" | "devnet" => Ok(Cluster::Devnet),
             "d" | "devnet" => Ok(Cluster::Devnet),
             "l" | "localnet" => Ok(Cluster::Localnet),
             "l" | "localnet" => Ok(Cluster::Localnet),
             "g" | "debug" => Ok(Cluster::Debug),
             "g" | "debug" => Ok(Cluster::Debug),
+            url if url.contains("http") => {
+                let http_url = url;
+
+                // Websocket port is always +1 the http port.
+                let mut ws_url = Url::parse(http_url)?;
+                if let Some(port) = ws_url.port() {
+                    ws_url.set_port(Some(port + 1))
+                        .map_err(|_| anyhow!("Unable to set port"))?;
+                } else {
+                    ws_url.set_port(Some(8900))
+                        .map_err(|_| anyhow!("Unable to set port"))?;
+                }
+
+                Ok(Cluster::Custom(http_url.to_string(), ws_url.to_string()))
+            }
             _ => Err(anyhow::Error::msg(
             _ => Err(anyhow::Error::msg(
                 "Cluster must be one of [localnet, testnet, mainnet, devnet] or be an http or https url\n",
                 "Cluster must be one of [localnet, testnet, mainnet, devnet] or be an http or https url\n",
             )),
             )),
@@ -94,4 +110,24 @@ mod tests {
         let bad_url = "httq://my_custom_url.test.net";
         let bad_url = "httq://my_custom_url.test.net";
         Cluster::from_str(bad_url).unwrap();
         Cluster::from_str(bad_url).unwrap();
     }
     }
+
+    #[test]
+    fn test_http_port() {
+        let url = "http://my-url.com:7000/";
+        let cluster = Cluster::from_str(url).unwrap();
+        assert_eq!(
+            Cluster::Custom(url.to_string(), "http://my-url.com:7001/".to_string()),
+            cluster
+        );
+    }
+
+    #[test]
+    fn test_http_no_port() {
+        let url = "http://my-url.com/";
+        let cluster = Cluster::from_str(url).unwrap();
+        assert_eq!(
+            Cluster::Custom(url.to_string(), "http://my-url.com:8900/".to_string()),
+            cluster
+        );
+    }
 }
 }