cargo_component/commands/
update.rs

1use anyhow::Result;
2use cargo_component_core::command::CommonOptions;
3use clap::Args;
4use std::path::PathBuf;
5
6use crate::{load_component_metadata, load_metadata, Config};
7
8/// Update dependencies as recorded in the component lock file
9#[derive(Args)]
10#[clap(disable_version_flag = true)]
11pub struct UpdateCommand {
12    /// The common command options.
13    #[clap(flatten)]
14    pub common: CommonOptions,
15
16    /// Don't actually write the lockfile
17    #[clap(long = "dry-run")]
18    pub dry_run: bool,
19
20    /// Require lock file and cache are up to date
21    #[clap(long = "frozen")]
22    pub frozen: bool,
23
24    /// Path to Cargo.toml
25    #[clap(long = "manifest-path", value_name = "PATH")]
26    pub manifest_path: Option<PathBuf>,
27
28    /// Require lock file is up to date
29    #[clap(long = "locked")]
30    pub locked: bool,
31
32    /// Run without accessing the network
33    #[clap(long = "offline")]
34    pub offline: bool,
35}
36
37impl UpdateCommand {
38    /// Executes the command.
39    pub async fn exec(self) -> Result<()> {
40        log::debug!("executing update command");
41        let config = Config::new(self.common.new_terminal(), self.common.config).await?;
42        let metadata = load_metadata(self.manifest_path.as_deref())?;
43        let packages = load_component_metadata(&metadata, [].iter(), true)?;
44
45        let lock_update_allowed = !self.frozen && !self.locked;
46        let client = config.client(self.common.cache_dir, false).await?;
47        crate::update_lockfile(
48            client,
49            &config,
50            &metadata,
51            &packages,
52            lock_update_allowed,
53            self.locked,
54            self.dry_run,
55        )
56        .await
57    }
58}