Skip to content

Commit

Permalink
fix: tilde in path not expanded
Browse files Browse the repository at this point in the history
Fixes an issue that when providing option values using `=`, the tilde does not get expanded by the shell and thus causes file not found errors.
  • Loading branch information
xJonathanLEI committed Jul 18, 2023
1 parent ba83680 commit ebcf7d5
Show file tree
Hide file tree
Showing 14 changed files with 117 additions and 8 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ rpassword = "7.2.0"
serde = { version = "1.0.164", features = ["derive"] }
serde_json = { version = "1.0.99", features = ["preserve_order"] }
serde_with = "2.3.3"
shellexpand = "3.1.0"
starknet = "0.5.0"
thiserror = "1.0.40"
tokio = { version = "1.28.2", default-features = false, features = ["macros", "rt-multi-thread"] }
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod compiler;
mod decode;
mod fee;
mod network;
mod path;
mod provider;
mod signer;
mod subcommands;
Expand Down
28 changes: 28 additions & 0 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
use std::path::PathBuf;

use clap::{builder::TypedValueParser, error::ErrorKind, Arg, Command, Error};

#[derive(Clone)]
pub struct ExpandedPathbufParser;

impl TypedValueParser for ExpandedPathbufParser {
type Value = PathBuf;

fn parse_ref(
&self,
cmd: &Command,
_arg: Option<&Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, Error> {
if value.is_empty() {
Err(cmd.clone().error(ErrorKind::InvalidValue, "empty path"))
} else {
let path = match value.to_str() {
Some(value) => PathBuf::from(shellexpand::tilde(value).into_owned()),
None => PathBuf::from(value),
};

Ok(path)
}
}
}
6 changes: 5 additions & 1 deletion src/subcommands/account/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use starknet::{
use crate::{
account::{AccountConfig, AccountVariant, DeployedStatus, DeploymentStatus},
fee::{FeeArgs, FeeSetting},
path::ExpandedPathbufParser,
signer::SignerArgs,
utils::watch_tx,
verbosity::VerbosityArgs,
Expand All @@ -27,7 +28,10 @@ pub struct Deploy {
signer: SignerArgs,
#[clap(flatten)]
fee: FeeArgs,
#[clap(help = "Path to the account config file")]
#[clap(
value_parser = ExpandedPathbufParser,
help = "Path to the account config file"
)]
file: PathBuf,
#[clap(flatten)]
verbosity: VerbosityArgs,
Expand Down
6 changes: 5 additions & 1 deletion src/subcommands/account/oz/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use starknet::{

use crate::{
account::{AccountConfig, AccountVariant, DeploymentStatus, OzAccountConfig, UndeployedStatus},
path::ExpandedPathbufParser,
signer::SignerArgs,
};

Expand All @@ -29,7 +30,10 @@ pub struct Init {
help = "Overwrite the account config file if it already exists"
)]
force: bool,
#[clap(help = "Path to save the account config file")]
#[clap(
value_parser = ExpandedPathbufParser,
help = "Path to save the account config file"
)]
output: PathBuf,
}

Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/class_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ use anyhow::Result;
use clap::Parser;
use starknet::core::types::contract::{legacy::LegacyContractClass, CompiledClass, SierraClass};

use crate::path::ExpandedPathbufParser;

#[derive(Debug, Parser)]
pub struct ClassHash {
#[clap(help = "Path to contract artifact file")]
#[clap(
value_parser = ExpandedPathbufParser,
help = "Path to contract artifact file"
)]
file: PathBuf,
}

Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::{
account::{AccountConfig, DeploymentStatus},
casm::{CasmArgs, CasmHashSource},
fee::{FeeArgs, FeeSetting},
path::ExpandedPathbufParser,
signer::SignerArgs,
utils::watch_tx,
verbosity::VerbosityArgs,
Expand All @@ -33,14 +34,18 @@ pub struct Declare {
#[clap(
long,
env = "STARKNET_ACCOUNT",
value_parser = ExpandedPathbufParser,
help = "Path to account config JSON file"
)]
account: PathBuf,
#[clap(flatten)]
fee: FeeArgs,
#[clap(long, help = "Wait for the transaction to confirm")]
watch: bool,
#[clap(help = "Path to contract artifact file")]
#[clap(
value_parser = ExpandedPathbufParser,
help = "Path to contract artifact file"
)]
file: PathBuf,
#[clap(flatten)]
verbosity: VerbosityArgs,
Expand Down
2 changes: 2 additions & 0 deletions src/subcommands/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use crate::{
address_book::AddressBookResolver,
decode::FeltDecoder,
fee::{FeeArgs, FeeSetting},
path::ExpandedPathbufParser,
signer::SignerArgs,
utils::watch_tx,
verbosity::VerbosityArgs,
Expand All @@ -44,6 +45,7 @@ pub struct Deploy {
#[clap(
long,
env = "STARKNET_ACCOUNT",
value_parser = ExpandedPathbufParser,
help = "Path to account config JSON file"
)]
account: PathBuf,
Expand Down
2 changes: 2 additions & 0 deletions src/subcommands/invoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::{
address_book::AddressBookResolver,
decode::FeltDecoder,
fee::{FeeArgs, FeeSetting},
path::ExpandedPathbufParser,
signer::SignerArgs,
utils::watch_tx,
verbosity::VerbosityArgs,
Expand All @@ -32,6 +33,7 @@ pub struct Invoke {
#[clap(
long,
env = "STARKNET_ACCOUNT",
value_parser = ExpandedPathbufParser,
help = "Path to account config JSON file"
)]
account: PathBuf,
Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/signer/keystore/from_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use clap::Parser;
use colored::Colorize;
use starknet::{core::types::FieldElement, signers::SigningKey};

use crate::path::ExpandedPathbufParser;

#[derive(Debug, Parser)]
pub struct FromKey {
#[clap(long, help = "Overwrite the file if it already exists")]
Expand All @@ -16,7 +18,10 @@ pub struct FromKey {
help = "Supply password from command line option instead of prompt"
)]
password: Option<String>,
#[clap(help = "Path to save the JSON keystore")]
#[clap(
value_parser = ExpandedPathbufParser,
help = "Path to save the JSON keystore"
)]
file: PathBuf,
}

Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/signer/keystore/inspect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use clap::Parser;
use colored::Colorize;
use starknet::signers::SigningKey;

use crate::path::ExpandedPathbufParser;

#[derive(Debug, Parser)]
pub struct Inspect {
#[clap(
Expand All @@ -14,7 +16,10 @@ pub struct Inspect {
password: Option<String>,
#[clap(long, help = "Print the public key only")]
raw: bool,
#[clap(help = "Path to the JSON keystore")]
#[clap(
value_parser = ExpandedPathbufParser,
help = "Path to the JSON keystore"
)]
file: PathBuf,
}

Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/signer/keystore/inspect_private.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use clap::Parser;
use colored::Colorize;
use starknet::signers::SigningKey;

use crate::path::ExpandedPathbufParser;

#[derive(Debug, Parser)]
pub struct InspectPrivate {
#[clap(
Expand All @@ -14,7 +16,10 @@ pub struct InspectPrivate {
password: Option<String>,
#[clap(long, help = "Print the private key only")]
raw: bool,
#[clap(help = "Path to the JSON keystore")]
#[clap(
value_parser = ExpandedPathbufParser,
help = "Path to the JSON keystore"
)]
file: PathBuf,
}

Expand Down
7 changes: 6 additions & 1 deletion src/subcommands/signer/keystore/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use clap::Parser;
use colored::Colorize;
use starknet::signers::SigningKey;

use crate::path::ExpandedPathbufParser;

#[derive(Debug, Parser)]
pub struct New {
#[clap(
Expand All @@ -14,7 +16,10 @@ pub struct New {
password: Option<String>,
#[clap(long, help = "Overwrite the file if it already exists")]
force: bool,
#[clap(help = "Path to save the JSON keystore")]
#[clap(
value_parser = ExpandedPathbufParser,
help = "Path to save the JSON keystore"
)]
file: PathBuf,
}

Expand Down

0 comments on commit ebcf7d5

Please sign in to comment.