Browse Source

cli: Fix using user specific path for `provider.wallet` (#2696)

acheron 1 year ago
parent
commit
9cb8d0355d
4 changed files with 39 additions and 39 deletions
  1. 1 0
      CHANGELOG.md
  2. 38 2
      cli/src/config.rs
  3. 0 1
      cli/src/lib.rs
  4. 0 36
      cli/src/path.rs

+ 1 - 0
CHANGELOG.md

@@ -16,6 +16,7 @@ The minor version will be incremented upon a breaking change and the patch versi
 
 - syn: Add missing `new_from_array` method to `Hash` ([#2682](https://github.com/coral-xyz/anchor/pull/2682)).
 - cli: Switch to Cargo feature resolver(`resolver = "2"`) ([#2676](https://github.com/coral-xyz/anchor/pull/2676)).
+- cli: Fix using user specific path for `provider.wallet` in `Anchor.toml` ([#2696](https://github.com/coral-xyz/anchor/pull/2696)).
 
 ### Breaking
 

+ 38 - 2
cli/src/config.rs

@@ -3,6 +3,7 @@ use anchor_client::Cluster;
 use anchor_syn::idl::types::Idl;
 use anyhow::{anyhow, bail, Context, Error, Result};
 use clap::{Parser, ValueEnum};
+use dirs::home_dir;
 use heck::ToSnakeCase;
 use reqwest::Url;
 use serde::de::{self, MapAccess, Visitor};
@@ -593,7 +594,7 @@ impl ToString for Config {
             registry: Some(self.registry.clone()),
             provider: Provider {
                 cluster: self.provider.cluster.clone(),
-                wallet: self.provider.wallet.to_string(),
+                wallet: self.provider.wallet.stringify_with_tilde(),
             },
             test: self.test_validator.clone().map(Into::into),
             scripts: match self.scripts.is_empty() {
@@ -1329,7 +1330,42 @@ impl AnchorPackage {
     }
 }
 
-crate::home_path!(WalletPath, ".config/solana/id.json");
+#[macro_export]
+macro_rules! home_path {
+    ($my_struct:ident, $path:literal) => {
+        #[derive(Clone, Debug)]
+        pub struct $my_struct(String);
+
+        impl Default for $my_struct {
+            fn default() -> Self {
+                $my_struct(home_dir().unwrap().join($path).display().to_string())
+            }
+        }
+
+        impl $my_struct {
+            fn stringify_with_tilde(&self) -> String {
+                self.0
+                    .replacen(home_dir().unwrap().to_str().unwrap(), "~", 1)
+            }
+        }
+
+        impl FromStr for $my_struct {
+            type Err = anyhow::Error;
+
+            fn from_str(s: &str) -> Result<Self, Self::Err> {
+                Ok(Self(s.to_owned()))
+            }
+        }
+
+        impl ToString for $my_struct {
+            fn to_string(&self) -> String {
+                self.0.clone()
+            }
+        }
+    };
+}
+
+home_path!(WalletPath, ".config/solana/id.json");
 
 #[cfg(test)]
 mod tests {

+ 0 - 1
cli/src/lib.rs

@@ -51,7 +51,6 @@ use std::string::ToString;
 use tar::Archive;
 
 pub mod config;
-mod path;
 pub mod rust_template;
 pub mod solidity_template;
 

+ 0 - 36
cli/src/path.rs

@@ -1,36 +0,0 @@
-#[macro_export]
-macro_rules! home_path {
-    ($my_struct:ident, $path:literal) => {
-        #[derive(Clone, Debug)]
-        pub struct $my_struct(String);
-
-        impl Default for $my_struct {
-            fn default() -> Self {
-                match dirs::home_dir() {
-                    None => {
-                        println!("$HOME doesn't exist. This probably won't do what you want.");
-                        $my_struct(".".to_string())
-                    }
-                    Some(mut path) => {
-                        path.push($path);
-                        $my_struct(path.as_path().display().to_string())
-                    }
-                }
-            }
-        }
-
-        impl ToString for $my_struct {
-            fn to_string(&self) -> String {
-                self.0.clone()
-            }
-        }
-
-        impl FromStr for $my_struct {
-            type Err = anyhow::Error;
-
-            fn from_str(s: &str) -> Result<Self, Self::Err> {
-                Ok(Self(s.to_string()))
-            }
-        }
-    };
-}