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

This commit is contained in:
acheron 2023-11-09 21:58:08 +01:00 committed by GitHub
parent 8f3bb8a556
commit 9cb8d0355d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 39 deletions

View File

@ -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

View File

@ -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 {

View File

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

View File

@ -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()))
}
}
};
}