leo_lang/cli/commands/
update.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::*;
18use crate::cli::helpers::updater::Updater;
19
20/// Update Leo to the latest version
21#[derive(Debug, Parser)]
22pub struct LeoUpdate {
23    /// Lists all available versions of Leo
24    #[clap(short = 'l', long, help = "List all available releases.")]
25    list: bool,
26    /// Update to a specific named release
27    #[clap(short = 'n', long, help = "An optional release name.")]
28    name: Option<String>,
29    /// Suppress outputs to terminal
30    #[clap(short = 'q', long, help = "Suppress download logs.")]
31    quiet: bool,
32}
33
34impl Command for LeoUpdate {
35    type Input = ();
36    type Output = ();
37
38    fn log_span(&self) -> Span {
39        tracing::span!(tracing::Level::INFO, "Leo")
40    }
41
42    fn prelude(&self, _: Context) -> Result<Self::Input> {
43        Ok(())
44    }
45
46    fn apply(self, _: Context, _: Self::Input) -> Result<Self::Output>
47    where
48        Self: Sized,
49    {
50        match self.list {
51            true => match Updater::show_available_releases() {
52                Ok(output) => tracing::info!("{output}"),
53                Err(error) => tracing::info!("Failed to list the available versions of Leo\n{error}\n"),
54            },
55            false => {
56                let result = Updater::update(!self.quiet, self.name);
57                if !self.quiet {
58                    match result {
59                        Ok(status) => {
60                            if status.uptodate() {
61                                tracing::info!("\nLeo is already on the latest version")
62                            } else if status.updated() {
63                                tracing::info!("\nLeo has updated to version {}", status.version())
64                            }
65                        }
66                        Err(e) => tracing::info!("\nFailed to update Leo to the latest version\n{e}\n"),
67                    }
68                }
69            }
70        }
71        Ok(())
72    }
73}