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)]
25    list: bool,
26    /// Suppress outputs to terminal
27    #[clap(short = 'q', long)]
28    quiet: bool,
29}
30
31impl Command for LeoUpdate {
32    type Input = ();
33    type Output = ();
34
35    fn log_span(&self) -> Span {
36        tracing::span!(tracing::Level::INFO, "Leo")
37    }
38
39    fn prelude(&self, _: Context) -> Result<Self::Input> {
40        Ok(())
41    }
42
43    fn apply(self, _: Context, _: Self::Input) -> Result<Self::Output>
44    where
45        Self: Sized,
46    {
47        match self.list {
48            true => match Updater::show_available_releases() {
49                Ok(output) => tracing::info!("{output}"),
50                Err(error) => tracing::info!("Failed to list the available versions of Leo\n{error}\n"),
51            },
52            false => {
53                let result = Updater::update_to_latest_release(!self.quiet);
54                if !self.quiet {
55                    match result {
56                        Ok(status) => {
57                            if status.uptodate() {
58                                tracing::info!("\nLeo is already on the latest version")
59                            } else if status.updated() {
60                                tracing::info!("\nLeo has updated to version {}", status.version())
61                            }
62                        }
63                        Err(e) => tracing::info!("\nFailed to update Leo to the latest version\n{e}\n"),
64                    }
65                }
66            }
67        }
68        Ok(())
69    }
70}