mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-11-22 13:25:30 +03:00
Update CLI to take files as input
This commit is contained in:
parent
8a7f8394a9
commit
adddd2ade2
@ -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<String>,
|
||||
|
||||
#[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::<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 {
|
||||
|
@ -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<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,
|
||||
}
|
||||
@ -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()?;
|
||||
|
Loading…
Reference in New Issue
Block a user