Skip to content

Commit

Permalink
Merge 966e5e2 into 7114ebd
Browse files Browse the repository at this point in the history
  • Loading branch information
FroVolod authored Aug 12, 2021
2 parents 7114ebd + 966e5e2 commit 196b8b5
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Get the release version from the tag
# if: env.NEAR_CLI_VERSION == ''
run: |
echo "NEAR_CLI_VERSION=0.1.11" >> $GITHUB_ENV
echo "NEAR_CLI_VERSION=0.1.12" >> $GITHUB_ENV
echo "version is: ${{ env.NEAR_CLI_VERSION }}"
- name: Create GitHub release
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "near-cli"
version = "0.1.11"
version = "0.1.12"
authors = ["FroVolod <[email protected]>"]
edition = "2018"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,10 @@ pub struct NoInitialize {

impl CliNoInitialize {
pub fn to_cli_args(&self) -> std::collections::VecDeque<String> {
let args = self
.sign_option
self.sign_option
.as_ref()
.map(|subcommand| subcommand.to_cli_args())
.unwrap_or_default();
args
.unwrap_or_default()
}
}

Expand Down
13 changes: 3 additions & 10 deletions src/commands/add_command/contract_code/contract/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use dialoguer::{theme::ColorfulTheme, Input, Select};
use std::io::Read;
use strum::{EnumDiscriminants, EnumIter, EnumMessage, IntoEnumIterator};

mod initialize_mode;
Expand Down Expand Up @@ -170,22 +169,16 @@ impl ContractFile {
.with_prompt("What is a file location of the contract?")
.interact_text()
.unwrap();
let mut path = std::path::PathBuf::new();
path.push(input_file_path);
println!("path: {:?}", &path);
path
input_file_path.into()
}

pub async fn process(
self,
prepopulated_unsigned_transaction: near_primitives::transaction::Transaction,
network_connection_config: Option<crate::common::ConnectionConfig>,
) -> crate::CliResult {
let mut code = Vec::new();
std::fs::File::open(&self.file_path.clone())
.map_err(|err| color_eyre::Report::msg(format!("Failed to open file: {:?}", err)))?
.read_to_end(&mut code)
.map_err(|err| color_eyre::Report::msg(format!("Failed to read file: {:?}", err)))?;
let code = std::fs::read(&self.file_path.clone())
.map_err(|err| color_eyre::Report::msg(format!("Failed to open file: {:?}", err)))?;
let action = near_primitives::transaction::Action::DeployContract(
near_primitives::transaction::DeployContractAction { code },
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
use async_recursion::async_recursion;
use dialoguer::Input;

/// add contract file
#[derive(Debug, Default, Clone, clap::Clap)]
#[clap(
setting(clap::AppSettings::ColoredHelp),
setting(clap::AppSettings::DisableHelpSubcommand),
setting(clap::AppSettings::VersionlessSubcommands)
)]
pub struct CliContractFile {
file_path: Option<std::path::PathBuf>,
#[clap(subcommand)]
next_action: Option<super::CliSkipNextAction>,
}

#[derive(Debug, Clone)]
pub struct ContractFile {
pub file_path: std::path::PathBuf,
pub next_action: Box<super::NextAction>,
}

impl CliContractFile {
pub fn to_cli_args(&self) -> std::collections::VecDeque<String> {
let mut args = self
.next_action
.as_ref()
.map(|subcommand| subcommand.to_cli_args())
.unwrap_or_default();
if let Some(file_path) = &self.file_path {
args.push_front(file_path.as_path().display().to_string());
}
args
}
}

impl From<ContractFile> for CliContractFile {
fn from(contract_file: ContractFile) -> Self {
Self {
file_path: Some(contract_file.file_path),
next_action: Some(super::CliSkipNextAction::Skip(super::CliSkipAction {
sign_option: None,
})),
}
}
}

impl ContractFile {
pub fn from(
item: CliContractFile,
connection_config: Option<crate::common::ConnectionConfig>,
sender_account_id: near_primitives::types::AccountId,
) -> color_eyre::eyre::Result<Self> {
let file_path = match item.file_path {
Some(cli_file_path) => cli_file_path,
None => ContractFile::input_file_path(),
};
let skip_next_action: super::NextAction = match item.next_action {
Some(cli_skip_action) => super::NextAction::from_cli_skip_next_action(
cli_skip_action,
connection_config,
sender_account_id,
)?,
None => super::NextAction::input_next_action(connection_config, sender_account_id)?,
};
Ok(ContractFile {
file_path,
next_action: Box::new(skip_next_action),
})
}
}

impl ContractFile {
fn input_file_path() -> std::path::PathBuf {
println!();
let input_file_path: String = Input::new()
.with_prompt("What is a file location of the contract?")
.interact_text()
.unwrap();
input_file_path.into()
}

#[async_recursion(?Send)]
pub async fn process(
self,
prepopulated_unsigned_transaction: near_primitives::transaction::Transaction,
network_connection_config: Option<crate::common::ConnectionConfig>,
) -> crate::CliResult {
let code = std::fs::read(&self.file_path.clone())
.map_err(|err| color_eyre::Report::msg(format!("Failed to open file: {:?}", err)))?;
let action = near_primitives::transaction::Action::DeployContract(
near_primitives::transaction::DeployContractAction { code },
);
let mut actions = prepopulated_unsigned_transaction.actions.clone();
actions.push(action);
let unsigned_transaction = near_primitives::transaction::Transaction {
actions,
..prepopulated_unsigned_transaction
};
match *self.next_action {
super::NextAction::AddAction(select_action) => {
select_action
.process(unsigned_transaction, network_connection_config)
.await
}
super::NextAction::Skip(skip_action) => {
skip_action
.process(unsigned_transaction, network_connection_config)
.await
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use dialoguer::{theme::ColorfulTheme, Select};
use strum::{EnumDiscriminants, EnumIter, EnumMessage, IntoEnumIterator};

mod add_access_key_mode;
mod add_contract_code_type;
mod call_function_type;
mod create_account_type;
mod delete_access_key_type;
Expand Down Expand Up @@ -258,6 +259,8 @@ pub enum CliActionSubcommand {
AddAccessKey(self::add_access_key_mode::CliAddAccessKeyMode),
/// Предоставьте данные для удаления ключа доступа у пользователя
DeleteAccessKey(self::delete_access_key_type::CliDeleteAccessKeyAction),
/// Предоставьте данные для добавления контракта
AddContractCode(self::add_contract_code_type::CliContractFile),
}

#[derive(Debug, Clone, EnumDiscriminants)]
Expand All @@ -277,6 +280,8 @@ pub enum ActionSubcommand {
AddAccessKey(self::add_access_key_mode::AddAccessKeyMode),
#[strum_discriminants(strum(message = "Detete an Access Key"))]
DeleteAccessKey(self::delete_access_key_type::DeleteAccessKeyAction),
#[strum_discriminants(strum(message = "Add a contract code"))]
AddContractCode(self::add_contract_code_type::ContractFile),
}

impl CliActionSubcommand {
Expand Down Expand Up @@ -317,6 +322,11 @@ impl CliActionSubcommand {
args.push_front("delete-access-key".to_owned());
args
}
Self::AddContractCode(subcommand) => {
let mut args = subcommand.to_cli_args();
args.push_front("add-contract-code".to_owned());
args
}
}
}
}
Expand Down Expand Up @@ -345,6 +355,9 @@ impl From<ActionSubcommand> for CliActionSubcommand {
ActionSubcommand::DeleteAccessKey(delete_access_key_action) => {
Self::DeleteAccessKey(delete_access_key_action.into())
}
ActionSubcommand::AddContractCode(add_contract_code_action) => {
Self::AddContractCode(add_contract_code_action.into())
}
}
}
}
Expand Down Expand Up @@ -414,6 +427,14 @@ impl ActionSubcommand {
)
.unwrap(),
),
CliActionSubcommand::AddContractCode(cli_contract_file) => Self::AddContractCode(
self::add_contract_code_type::ContractFile::from(
cli_contract_file,
connection_config,
sender_account_id,
)
.unwrap(),
),
}
}
}
Expand Down Expand Up @@ -457,6 +478,9 @@ impl ActionSubcommand {
ActionSubcommandDiscriminants::DeleteAccessKey => {
CliActionSubcommand::DeleteAccessKey(Default::default())
}
ActionSubcommandDiscriminants::AddContractCode => {
CliActionSubcommand::AddContractCode(Default::default())
}
};
Self::from(cli_action_subcomand, connection_config, sender_account_id)
}
Expand Down Expand Up @@ -502,6 +526,11 @@ impl ActionSubcommand {
.process(prepopulated_unsigned_transaction, network_connection_config)
.await
}
ActionSubcommand::AddContractCode(args_contract_file) => {
args_contract_file
.process(prepopulated_unsigned_transaction, network_connection_config)
.await
}
}
}
}
Expand Down

0 comments on commit 196b8b5

Please sign in to comment.