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 = "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        // Check that the program name is valid.
62        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        // Get the edition, defaulting to the latest if not specified.
67        let edition = match self.edition {
68            Some(edition) => edition.to_string(),
69            None => "latest".to_string(),
70        };
71        // Build custom url to fetch from based on the flags and user's input.
72        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}