-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
nargo backend ls
and nargo backend use
command to switc…
…h between backends (#2552)
- Loading branch information
1 parent
9fbd069
commit 7471147
Showing
6 changed files
with
147 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,28 @@ | ||
use std::path::PathBuf; | ||
|
||
use acvm_backend_barretenberg::backends_directory; | ||
pub(crate) use acvm_backend_barretenberg::Backend; | ||
|
||
fn active_backend_file_path() -> PathBuf { | ||
backends_directory().join(".selected_backend") | ||
} | ||
|
||
pub(crate) fn set_active_backend(backend_name: &str) { | ||
std::fs::create_dir_all( | ||
active_backend_file_path().parent().expect("active backend file should have parent"), | ||
) | ||
.unwrap(); | ||
std::fs::write(active_backend_file_path(), backend_name.as_bytes()).unwrap(); | ||
} | ||
|
||
pub(crate) fn get_active_backend() -> String { | ||
let active_backend_file = active_backend_file_path(); | ||
|
||
if !active_backend_file.is_file() { | ||
let barretenberg = "acvm-backend-barretenberg"; | ||
set_active_backend(barretenberg); | ||
return barretenberg.to_string(); | ||
} | ||
|
||
std::fs::read_to_string(active_backend_file).unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use acvm_backend_barretenberg::backends_directory; | ||
use clap::Args; | ||
|
||
use crate::errors::CliError; | ||
|
||
/// Prints the list of available backends | ||
#[derive(Debug, Clone, Args)] | ||
pub(crate) struct LsCommand; | ||
|
||
pub(crate) fn run(_args: LsCommand) -> Result<(), CliError> { | ||
for backend in get_available_backends() { | ||
println!("{}", backend); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(super) fn get_available_backends() -> Vec<String> { | ||
let backend_directory_contents = std::fs::read_dir(backends_directory()).unwrap(); | ||
|
||
backend_directory_contents | ||
.into_iter() | ||
.filter_map(|entry| { | ||
let path = entry.ok()?.path(); | ||
if path.is_dir() { | ||
path.file_name().map(|name| name.to_string_lossy().to_string()) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use clap::{Args, Subcommand}; | ||
|
||
use crate::errors::CliError; | ||
|
||
mod ls_cmd; | ||
mod use_cmd; | ||
|
||
#[non_exhaustive] | ||
#[derive(Args, Clone, Debug)] | ||
pub(crate) struct BackendCommand { | ||
#[command(subcommand)] | ||
command: BackendCommands, | ||
} | ||
|
||
#[non_exhaustive] | ||
#[derive(Subcommand, Clone, Debug)] | ||
pub(crate) enum BackendCommands { | ||
Ls(ls_cmd::LsCommand), | ||
Use(use_cmd::UseCommand), | ||
} | ||
|
||
pub(crate) fn run(cmd: BackendCommand) -> Result<(), CliError> { | ||
let BackendCommand { command } = cmd; | ||
|
||
match command { | ||
BackendCommands::Ls(args) => ls_cmd::run(args), | ||
BackendCommands::Use(args) => use_cmd::run(args), | ||
}?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use clap::Args; | ||
|
||
use crate::{backends::set_active_backend, errors::CliError}; | ||
|
||
use super::ls_cmd::get_available_backends; | ||
|
||
/// Select the currently active backend | ||
#[derive(Debug, Clone, Args)] | ||
pub(crate) struct UseCommand { | ||
backend: String, | ||
} | ||
|
||
pub(crate) fn run(args: UseCommand) -> Result<(), CliError> { | ||
let backends = get_available_backends(); | ||
|
||
assert!(backends.contains(&args.backend), "backend doesn't exist"); | ||
|
||
set_active_backend(&args.backend); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters