snarkos_cli/commands/
update.rs

1// Copyright 2024 Aleo Network Foundation
2// This file is part of the snarkOS library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7
8// http://www.apache.org/licenses/LICENSE-2.0
9
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use crate::helpers::Updater;
17
18use anyhow::Result;
19use clap::Parser;
20
21/// Update snarkOS.
22#[derive(Debug, Parser)]
23pub struct Update {
24    /// Lists all available versions of snarkOS
25    #[clap(short = 'l', long)]
26    list: bool,
27    /// Suppress outputs to terminal
28    #[clap(short = 'q', long)]
29    quiet: bool,
30    /// Update to specified version
31    #[clap(short = 'v', long)]
32    version: Option<String>,
33}
34
35impl Update {
36    /// Update snarkOS.
37    pub fn parse(self) -> Result<String> {
38        match self.list {
39            true => match Updater::show_available_releases() {
40                Ok(output) => Ok(output),
41                Err(error) => Ok(format!("Failed to list the available versions of snarkOS\n{error}\n")),
42            },
43            false => {
44                let result = Updater::update_to_release(!self.quiet, self.version);
45                if !self.quiet {
46                    match result {
47                        Ok(status) => {
48                            if status.uptodate() {
49                                Ok("\nsnarkOS is already on the latest version".to_string())
50                            } else if status.updated() {
51                                Ok(format!("\nsnarkOS has updated to version {}", status.version()))
52                            } else {
53                                Ok(String::new())
54                            }
55                        }
56                        Err(e) => Ok(format!("\nFailed to update snarkOS to the latest version\n{e}\n")),
57                    }
58                } else {
59                    Ok(String::new())
60                }
61            }
62        }
63    }
64}