Skip to content

Commit 40d6ac6

Browse files
committed
refactor(context): Abstract lookup for locked flag
1 parent c576f68 commit 40d6ac6

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

src/cargo/ops/lockfile.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,14 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
5050
}
5151
}
5252

53-
if !ws.gctx().lock_update_allowed() {
54-
let flag = if ws.gctx().locked() {
55-
"--locked"
56-
} else {
57-
"--frozen"
58-
};
53+
if let Some(locked_flag) = ws.gctx().locked_flag() {
5954
anyhow::bail!(
6055
"the lock file {} needs to be updated but {} was passed to prevent this\n\
6156
If you want to try to generate the lock file without accessing the network, \
6257
remove the {} flag and use --offline instead.",
6358
lock_root.as_path_unlocked().join(LOCKFILE_NAME).display(),
64-
flag,
65-
flag
59+
locked_flag,
60+
locked_flag
6661
);
6762
}
6863

src/cargo/ops/registry/info/mod.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,8 @@ fn validate_locked_and_frozen_options(
198198
) -> Result<(), anyhow::Error> {
199199
// Only in workspace, we can use --frozen or --locked.
200200
if !in_workspace {
201-
if gctx.locked() {
202-
bail!("the option `--locked` can only be used within a workspace");
203-
}
204-
205-
if gctx.frozen() {
206-
bail!("the option `--frozen` can only be used within a workspace");
201+
if let Some(locked_flag) = gctx.locked_flag() {
202+
bail!("the option `{locked_flag}` can only be used within a workspace");
207203
}
208204
}
209205
Ok(())

src/cargo/util/context/mod.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1191,7 +1191,17 @@ impl GlobalContext {
11911191
}
11921192

11931193
pub fn lock_update_allowed(&self) -> bool {
1194-
!self.frozen && !self.locked
1194+
!self.locked_flag().is_some()
1195+
}
1196+
1197+
pub fn locked_flag(&self) -> Option<&'static str> {
1198+
if self.frozen {
1199+
Some("--frozen")
1200+
} else if self.locked {
1201+
Some("--locked")
1202+
} else {
1203+
None
1204+
}
11951205
}
11961206

11971207
/// Loads configuration from the filesystem.

0 commit comments

Comments
 (0)