leo_lang/cli/commands/query/
program.rs

1// Copyright (C) 2019-2025 Provable Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use super::*;
18
19use clap::Parser;
20
21/// Query program source code and live mapping values.
22#[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 = "Get all mappings defined in the program",
29        default_value = "false",
30        conflicts_with = "mapping_value"
31    )]
32    pub(crate) mappings: bool,
33    #[arg(long, help = "Get the value corresponding to the specified mapping and key.", number_of_values = 2, value_names = &["MAPPING", "KEY"], conflicts_with = "mappings")]
34    pub(crate) mapping_value: Option<Vec<String>>,
35}
36
37impl Command for LeoProgram {
38    type Input = ();
39    type Output = String;
40
41    fn log_span(&self) -> Span {
42        tracing::span!(tracing::Level::INFO, "Leo")
43    }
44
45    fn prelude(&self, _context: Context) -> Result<Self::Input> {
46        Ok(())
47    }
48
49    fn apply(self, _context: Context, _: Self::Input) -> Result<Self::Output> {
50        // Check that the program name is valid.
51        let program = if self.name.ends_with(".aleo") { self.name.clone() } else { format!("{}.aleo", self.name) };
52        if !leo_package::is_valid_aleo_name(&program) {
53            return Err(CliError::invalid_program_name(program).into());
54        }
55        // Build custom url to fetch from based on the flags and user's input.
56        let url = if let Some(mapping_info) = self.mapping_value {
57            format!("program/{}/mapping/{}/{}", program, mapping_info[0], mapping_info[1])
58        } else if self.mappings {
59            format!("program/{}/mappings", program)
60        } else {
61            format!("program/{}", program)
62        };
63
64        Ok(url)
65    }
66}