leo_lang/cli/commands/
new.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 leo_package::{NetworkName, Package};
20
21/// Create new Leo project
22#[derive(Parser, Debug)]
23pub struct LeoNew {
24    #[clap(name = "NAME", help = "Set package name")]
25    pub(crate) name: String,
26    #[clap(short = 'n', long, help = "Name of the network to use", default_value = "testnet")]
27    pub(crate) network: String,
28    #[clap(
29        short = 'e',
30        long,
31        help = "Endpoint to retrieve network state from.",
32        default_value = "https://api.explorer.provable.com/v1"
33    )]
34    pub(crate) endpoint: String,
35}
36
37impl Command for LeoNew {
38    type Input = ();
39    type Output = ();
40
41    fn log_span(&self) -> Span {
42        tracing::span!(tracing::Level::INFO, "Leo")
43    }
44
45    fn prelude(&self, _: Context) -> Result<Self::Input> {
46        Ok(())
47    }
48
49    fn apply(self, context: Context, _: Self::Input) -> Result<Self::Output> {
50        // Parse the network.
51        let network: NetworkName = self.network.parse()?;
52
53        // Derive the location of the parent directory to the project.
54        let package_path = context.parent_dir()?;
55
56        // Change the cwd to the Leo package directory to initialize all files.
57        std::env::set_current_dir(&package_path)
58            .map_err(|err| PackageError::failed_to_set_cwd(package_path.display(), err))?;
59
60        let full_path = Package::initialize(&self.name, &package_path, network, &self.endpoint)?;
61
62        println!("Created program {} at `{}`.", self.name.bold(), full_path.display());
63
64        Ok(())
65    }
66}