1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use crate::{load_component_metadata, load_metadata, Config};
use anyhow::Result;
use cargo_component_core::command::CommonOptions;
use clap::Args;
use std::path::PathBuf;

/// Update dependencies as recorded in the component lock file
#[derive(Args)]
#[clap(disable_version_flag = true)]
pub struct UpdateCommand {
    /// The common command options.
    #[clap(flatten)]
    pub common: CommonOptions,

    /// Don't actually write the lockfile
    #[clap(long = "dry-run")]
    pub dry_run: bool,

    /// Require lock file and cache are up to date
    #[clap(long = "frozen")]
    pub frozen: bool,

    /// Path to Cargo.toml
    #[clap(long = "manifest-path", value_name = "PATH")]
    pub manifest_path: Option<PathBuf>,

    /// Require lock file is up to date
    #[clap(long = "locked")]
    pub locked: bool,

    /// Run without accessing the network
    #[clap(long = "offline")]
    pub offline: bool,
}

impl UpdateCommand {
    /// Executes the command.
    pub async fn exec(self) -> Result<()> {
        log::debug!("executing update command");
        let config = Config::new(self.common.new_terminal())?;
        let metadata = load_metadata(self.manifest_path.as_deref())?;
        let packages = load_component_metadata(&metadata, [].iter(), true)?;

        let network_allowed = !self.frozen && !self.offline;
        let lock_update_allowed = !self.frozen && !self.locked;
        crate::update_lockfile(
            &config,
            &metadata,
            &packages,
            network_allowed,
            lock_update_allowed,
            self.locked,
            self.dry_run,
        )
        .await
    }
}