Skip to content

Commit

Permalink
Update CLI to take files as input
Browse files Browse the repository at this point in the history
  • Loading branch information
d0cd committed Oct 30, 2023
1 parent 9a5a016 commit ca88194
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
32 changes: 29 additions & 3 deletions leo/cli/commands/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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<String>,

#[clap(flatten)]
pub(crate) compiler_options: BuildOptions,
}
Expand All @@ -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::<CurrentNetwork>::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::<Vec<String>>();
// 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 {
Expand Down
32 changes: 29 additions & 3 deletions leo/cli/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -27,6 +27,9 @@ pub struct Run {
#[clap(name = "INPUTS", help = "The inputs to the program.")]
inputs: Vec<String>,

#[arg(short, long, help = "The inputs to the program, from a file. Overrides the INPUTS argument.")]
file: Option<String>,

#[clap(flatten)]
pub(crate) compiler_options: BuildOptions,
}
Expand All @@ -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::<CurrentNetwork>::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::<Vec<String>>();
// 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()?;
Expand Down

0 comments on commit ca88194

Please sign in to comment.