leo_lang/cli/commands/query/
program.rs1use super::*;
18
19use clap::Parser;
20
21#[derive(Parser, Debug)]
23pub struct LeoProgram {
24 #[clap(name = "NAME", help = "The name of the program to fetch")]
25 pub(crate) name: String,
26 #[arg(
27 long,
28 help = "An optional edition to use when fetching the program source. If not specified, the latest edition will be used."
29 )]
30 pub(crate) edition: Option<u16>,
31 #[arg(
32 long,
33 help = "Get all mappings defined in the latest edition of the program",
34 default_value = "false",
35 conflicts_with = "mapping_value"
36 )]
37 pub(crate) mappings: bool,
38 #[arg(
39 long,
40 help = "Get the value corresponding to the specified mapping and key.",
41 number_of_values = 2,
42 value_names = &["MAPPING", "KEY"],
43 conflicts_with = "mappings"
44 )]
45 pub(crate) mapping_value: Option<Vec<String>>,
46}
47
48impl Command for LeoProgram {
49 type Input = ();
50 type Output = String;
51
52 fn log_span(&self) -> Span {
53 tracing::span!(tracing::Level::INFO, "Leo")
54 }
55
56 fn prelude(&self, _context: Context) -> Result<Self::Input> {
57 Ok(())
58 }
59
60 fn apply(self, _context: Context, _: Self::Input) -> Result<Self::Output> {
61 let program = if self.name.ends_with(".aleo") { self.name.clone() } else { format!("{}.aleo", self.name) };
63 if !leo_package::is_valid_aleo_name(&program) {
64 return Err(CliError::invalid_program_name(program).into());
65 }
66 let edition = match self.edition {
68 Some(edition) => edition.to_string(),
69 None => "latest".to_string(),
70 };
71 let url = if let Some(mapping_info) = self.mapping_value {
73 format!("program/{program}/mapping/{}/{}", mapping_info[0], mapping_info[1])
74 } else if self.mappings {
75 format!("program/{program}/mappings")
76 } else {
77 format!("program/{program}/{edition}")
78 };
79
80 Ok(url)
81 }
82}