From adddd2ade2b20b37ddbbb542e05c041f7da2756a Mon Sep 17 00:00:00 2001 From: Pranav Gaddamadugu <23022326+d0cd@users.noreply.github.com> Date: Mon, 30 Oct 2023 10:38:40 -0400 Subject: [PATCH] Update CLI to take files as input --- leo/cli/commands/execute.rs | 32 +++++++++++++++++++++++++++++--- leo/cli/commands/run.rs | 32 +++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/leo/cli/commands/execute.rs b/leo/cli/commands/execute.rs index 3f148b4221..b018d884db 100644 --- a/leo/cli/commands/execute.rs +++ b/leo/cli/commands/execute.rs @@ -16,7 +16,7 @@ use super::*; -use snarkvm::cli::Execute as SnarkVMExecute; +use snarkvm::{cli::Execute as SnarkVMExecute, prelude::Parser as SnarkVMParser}; /// Build, Prove and Run Leo program with inputs #[derive(Parser, Debug)] @@ -35,6 +35,9 @@ pub struct Execute { )] endpoint: String, + #[arg(short, long, help = "The inputs to the program, from a file. Overrides the INPUTS argument.")] + file: Option, + #[clap(flatten)] pub(crate) compiler_options: BuildOptions, } @@ -57,8 +60,31 @@ impl Command for Execute { // Compose the `execute` command. let mut arguments = vec![SNARKVM_COMMAND.to_string(), self.name]; - // Add the program inputs to the arguments. - arguments.append(&mut inputs); + // Add the inputs to the arguments. + match self.file { + Some(file) => { + // Get the contents from the file. + let path = context.dir()?.join(file); + let raw_content = std::fs::read_to_string(&path) + .map_err(|err| PackageError::failed_to_read_file(path.display(), err))?; + // Parse the values from the file. + let mut content = raw_content.as_str(); + let mut values = vec![]; + while let Ok((remaining, value)) = snarkvm::prelude::Value::::parse(content) { + content = remaining; + values.push(value); + } + // Check that the remaining content is empty. + if !content.trim().is_empty() { + return Err(PackageError::failed_to_read_input_file(path.display()).into()); + } + // Convert the values to strings. + let mut inputs_from_file = values.into_iter().map(|value| value.to_string()).collect::>(); + // Add the inputs from the file to the arguments. + arguments.append(&mut inputs_from_file); + } + None => arguments.append(&mut inputs), + } // Add the compiler options to the arguments. if self.compiler_options.offline { diff --git a/leo/cli/commands/run.rs b/leo/cli/commands/run.rs index 35eb6a90ec..60e5f3aae3 100644 --- a/leo/cli/commands/run.rs +++ b/leo/cli/commands/run.rs @@ -16,7 +16,7 @@ use super::*; -use snarkvm::cli::Run as SnarkVMRun; +use snarkvm::{cli::Run as SnarkVMRun, prelude::Parser as SnarkVMParser}; /// Build, Prove and Run Leo program with inputs #[derive(Parser, Debug)] @@ -27,6 +27,9 @@ pub struct Run { #[clap(name = "INPUTS", help = "The inputs to the program.")] pub(crate) inputs: Vec, + #[arg(short, long, help = "The inputs to the program, from a file. Overrides the INPUTS argument.")] + file: Option, + #[clap(flatten)] pub(crate) compiler_options: BuildOptions, } @@ -49,8 +52,31 @@ impl Command for Run { // Compose the `run` command. let mut arguments = vec![SNARKVM_COMMAND.to_string(), self.name]; - // Add the program inputs to the arguments. - arguments.append(&mut inputs); + // Add the inputs to the arguments. + match self.file { + Some(file) => { + // Get the contents from the file. + let path = context.dir()?.join(file); + let raw_content = std::fs::read_to_string(&path) + .map_err(|err| PackageError::failed_to_read_file(path.display(), err))?; + // Parse the values from the file. + let mut content = raw_content.as_str(); + let mut values = vec![]; + while let Ok((remaining, value)) = snarkvm::prelude::Value::::parse(content) { + content = remaining; + values.push(value); + } + // Check that the remaining content is empty. + if !content.trim().is_empty() { + return Err(PackageError::failed_to_read_input_file(path.display()).into()); + } + // Convert the values to strings. + let mut inputs_from_file = values.into_iter().map(|value| value.to_string()).collect::>(); + // Add the inputs from the file to the arguments. + arguments.append(&mut inputs_from_file); + } + None => arguments.append(&mut inputs), + } // Open the Leo build/ directory let path = context.dir()?;