feat(cli): env add and env remove; env list shows added interpreters
The CLI half of Add interpreter…: env add validates, probes and stores an interpreter or environment directory, env remove takes it off the list (matched by identity, or by the stored path when the file is gone), and env list appends the added ones after discovery. --env and env use go through the same validator. Two new help goldens; help-env and help-env-list re-blessed for the new lines. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c9128e479e
commit
9f9efbba79
7 files changed
+231
-28
No files matched your search
@@ -275,13 +275,23 @@ enum Command {
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
enum EnvCommand {
|
||||
/// List discovered environments, their sources and PEP 668 flags.
|
||||
/// List discovered environments and the ones added with `env add`, their sources and PEP 668 flags.
|
||||
List,
|
||||
/// Set the default environment.
|
||||
Use {
|
||||
/// Interpreter or environment directory.
|
||||
path: PathBuf,
|
||||
},
|
||||
/// Add an interpreter discovery does not find to the list (the GUI's Add interpreter…).
|
||||
Add {
|
||||
/// Interpreter or environment directory.
|
||||
path: PathBuf,
|
||||
},
|
||||
/// Take an interpreter added with `env add` off the list. Nothing on disk is touched.
|
||||
Remove {
|
||||
/// Interpreter or environment directory, as it was added.
|
||||
path: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Subcommand)]
|
||||
@@ -395,6 +405,8 @@ async fn main() -> ExitCode {
|
||||
}
|
||||
Command::Schema { type_name } => run::schema(type_name),
|
||||
Command::Env(EnvCommand::Use { path }) => run::env_use(&cli.global, path).await,
|
||||
Command::Env(EnvCommand::Add { path }) => run::env_add(&cli.global, path).await,
|
||||
Command::Env(EnvCommand::Remove { path }) => run::env_remove(&cli.global, path),
|
||||
Command::Audit => run::audit(&cli.global).await,
|
||||
Command::PipUpgrade => run::pip_upgrade(&cli.global).await,
|
||||
Command::Snapshot(SnapshotCommand::Rollback { id }) => {
|
||||
|
||||
+116
-23
@@ -228,7 +228,8 @@ pub async fn select_env(opts: &GlobalOpts) -> Result<PyEnv> {
|
||||
/// default that mutates the wrong environment.
|
||||
pub async fn select(opts: &GlobalOpts) -> Result<envs::ProbeResult> {
|
||||
if let Some(path) = &opts.env {
|
||||
return envs::probe(&interpreter_in(path), EnvSource::Manual).await;
|
||||
let interpreter = envs::validate_interpreter(&envs::interpreter_in(path), &app_data_dir())?;
|
||||
return envs::probe(&interpreter, EnvSource::Manual).await;
|
||||
}
|
||||
|
||||
let stored = Store::open(&app_data_dir())
|
||||
@@ -257,31 +258,21 @@ pub async fn select(opts: &GlobalOpts) -> Result<envs::ProbeResult> {
|
||||
))
|
||||
}
|
||||
|
||||
/// Accept either an interpreter path or an environment directory, as CLI-SPEC §2 promises.
|
||||
fn interpreter_in(path: &Path) -> PathBuf {
|
||||
if path.is_file() {
|
||||
return path.to_path_buf();
|
||||
}
|
||||
let exe: PathBuf = if cfg!(windows) {
|
||||
["Scripts", "python.exe"].iter().collect()
|
||||
} else {
|
||||
["bin", "python"].iter().collect()
|
||||
};
|
||||
let candidate = path.join(&exe);
|
||||
if candidate.is_file() {
|
||||
candidate
|
||||
} else {
|
||||
path.to_path_buf()
|
||||
}
|
||||
}
|
||||
|
||||
/// `pipdock env list`
|
||||
///
|
||||
/// # Errors
|
||||
/// Never fails as a whole: an interpreter that cannot be probed is reported in place rather than
|
||||
/// aborting the listing, because one broken environment must not hide the rest.
|
||||
pub async fn env_list(opts: &GlobalOpts) -> Result<Exit> {
|
||||
let candidates = envs::scan().await;
|
||||
// The interpreters `env add` (or the GUI's Add interpreter…) put on the list, after discovery's
|
||||
// own. A store that cannot be read costs those rows, not the listing.
|
||||
let manual: Vec<PathBuf> = Store::open(&app_data_dir())
|
||||
.and_then(|store| store.manual_envs())
|
||||
.unwrap_or_default()
|
||||
.into_iter()
|
||||
.map(|m| PathBuf::from(m.interpreter))
|
||||
.collect();
|
||||
let candidates = envs::scan_with(&manual).await;
|
||||
|
||||
let mut rows = Vec::new();
|
||||
for Candidate { path, source } in candidates {
|
||||
@@ -1020,7 +1011,7 @@ pub fn schema(type_name: &str) -> Result<Exit> {
|
||||
/// `PD-ENV-001` when the path is not a usable interpreter — chosen deliberately over accepting it
|
||||
/// silently, because a default that does not work fails on every later command instead of this one.
|
||||
pub async fn env_use(opts: &GlobalOpts, path: &Path) -> Result<Exit> {
|
||||
let interpreter = interpreter_in(path);
|
||||
let interpreter = envs::validate_interpreter(&envs::interpreter_in(path), &app_data_dir())?;
|
||||
let probed = envs::probe(&interpreter, EnvSource::Manual).await?;
|
||||
let store = Store::open(&app_data_dir())?;
|
||||
|
||||
@@ -1043,6 +1034,96 @@ pub async fn env_use(opts: &GlobalOpts, path: &Path) -> Result<Exit> {
|
||||
Ok(Exit::Success)
|
||||
}
|
||||
|
||||
/// `pipdock env add <path>` — the GUI's *Add interpreter…* (PRD P0-1).
|
||||
///
|
||||
/// Validated, probed, then stored, so the list never gains an entry that was refused or broken on
|
||||
/// arrival. `--json` prints the entry exactly as `env list --json` will.
|
||||
///
|
||||
/// # Errors
|
||||
/// `PD-ENV-001` when the path is missing, not named like a Python interpreter, or one of PipDock's
|
||||
/// own environments; `PD-ENV-003` when the probe fails; `PD-INT-001` when the store cannot be
|
||||
/// written.
|
||||
pub async fn env_add(opts: &GlobalOpts, path: &Path) -> Result<Exit> {
|
||||
let interpreter = envs::validate_interpreter(&envs::interpreter_in(path), &app_data_dir())?;
|
||||
let probed = envs::probe(&interpreter, EnvSource::Manual).await?;
|
||||
let env_hash = envs::env_hash(&interpreter);
|
||||
Store::open(&app_data_dir())?.add_manual_env(
|
||||
&env_hash,
|
||||
&interpreter.display().to_string(),
|
||||
&jiff::Timestamp::now().to_string(),
|
||||
)?;
|
||||
|
||||
if opts.json {
|
||||
let pip = probed
|
||||
.dists
|
||||
.iter()
|
||||
.find(|d| d.name.as_str() == "pip")
|
||||
.map(|d| d.version.0.clone());
|
||||
print_json(&serde_json::json!({
|
||||
"interpreter": interpreter,
|
||||
"source": EnvSource::Manual,
|
||||
"python": probed.env.python_version,
|
||||
"externallyManaged": probed.env.externally_managed,
|
||||
"hiddenUserSite": probed.env.hidden_user_site,
|
||||
"packages": probed.dists.len(),
|
||||
"envHash": env_hash,
|
||||
"pipVersion": pip,
|
||||
}));
|
||||
} else if !opts.quiet {
|
||||
println!(
|
||||
"added: {} (Python {})",
|
||||
interpreter.display(),
|
||||
probed.env.python_version
|
||||
);
|
||||
}
|
||||
Ok(Exit::Success)
|
||||
}
|
||||
|
||||
/// `pipdock env remove <path>`
|
||||
///
|
||||
/// Matched by identity, and by the path as stored when the file is gone — a deleted venv cannot be
|
||||
/// canonicalized into the identity it was added under, and it is exactly the entry someone wants
|
||||
/// gone. Not being on the list is not an error: the list is what was asked about, and it is right.
|
||||
///
|
||||
/// # Errors
|
||||
/// `PD-INT-001` when the store cannot be read or written.
|
||||
pub fn env_remove(opts: &GlobalOpts, path: &Path) -> Result<Exit> {
|
||||
let store = Store::open(&app_data_dir())?;
|
||||
let interpreter = envs::interpreter_in(path);
|
||||
let wanted = envs::env_hash(&interpreter);
|
||||
let entry = store
|
||||
.manual_envs()?
|
||||
.into_iter()
|
||||
.find(|m| m.env_hash == wanted || same_path(&m.interpreter, &interpreter));
|
||||
let removed = match &entry {
|
||||
Some(m) => store.remove_manual_env(&m.env_hash)?,
|
||||
None => false,
|
||||
};
|
||||
let shown = entry.map_or_else(|| interpreter.display().to_string(), |m| m.interpreter);
|
||||
|
||||
if opts.json {
|
||||
print_json(&serde_json::json!({ "interpreter": shown, "removed": removed }));
|
||||
} else if !opts.quiet {
|
||||
if removed {
|
||||
println!("removed from the list: {shown} (nothing on disk was changed)");
|
||||
} else {
|
||||
println!("not on the list: {shown}");
|
||||
}
|
||||
}
|
||||
Ok(Exit::Success)
|
||||
}
|
||||
|
||||
/// Whether a stored path and a typed one name the same file, without asking the file system —
|
||||
/// which, for a deleted venv, has nothing to say. Case-insensitive on Windows (invariant 9).
|
||||
fn same_path(stored: &str, typed: &Path) -> bool {
|
||||
let stored = Path::new(stored);
|
||||
if cfg!(windows) {
|
||||
stored.as_os_str().eq_ignore_ascii_case(typed.as_os_str())
|
||||
} else {
|
||||
stored == typed
|
||||
}
|
||||
}
|
||||
|
||||
/// `pipdock tools sync`
|
||||
///
|
||||
/// The re-sync `PD-HLT-001`'s shipped copy already tells users to run — until now there was no way
|
||||
@@ -2270,8 +2351,20 @@ mod tests {
|
||||
#[test]
|
||||
fn an_env_directory_resolves_to_its_interpreter() {
|
||||
// CLI-SPEC §2 accepts "interpreter or env dir"; a non-existent path is passed through so
|
||||
// the probe reports PD-ENV-001 against what the user actually typed.
|
||||
// the validator reports PD-ENV-001 against what the user actually typed.
|
||||
let missing = Path::new("definitely-not-here");
|
||||
assert_eq!(interpreter_in(missing), missing);
|
||||
assert_eq!(envs::interpreter_in(missing), missing);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stored_path_matches_what_was_typed_in_any_case_on_windows() {
|
||||
let stored = r"D:\work\proj\myenv\Scripts\python.exe";
|
||||
assert!(same_path(stored, Path::new(stored)));
|
||||
assert_eq!(
|
||||
same_path(stored, Path::new(&stored.to_lowercase())),
|
||||
cfg!(windows),
|
||||
"case-folded on Windows only"
|
||||
);
|
||||
assert!(!same_path(stored, Path::new(r"D:\work\other\python.exe")));
|
||||
}
|
||||
}
|
||||
@@ -154,6 +154,8 @@ fn subcommand_help_is_stable() {
|
||||
for (parent, child) in [
|
||||
("env", "list"),
|
||||
("env", "use"),
|
||||
("env", "add"),
|
||||
("env", "remove"),
|
||||
("pin", "add"),
|
||||
("pin", "remove"),
|
||||
("pin", "list"),
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
---
|
||||
source: crates/pipdock-cli/tests/golden.rs
|
||||
expression: "stdout_of(&[parent, child, \"--help\"])"
|
||||
---
|
||||
Add an interpreter discovery does not find to the list (the GUI's Add interpreter…)
|
||||
|
||||
Usage: pipdock env add [OPTIONS] <PATH>
|
||||
|
||||
Arguments:
|
||||
<PATH>
|
||||
Interpreter or environment directory
|
||||
|
||||
Options:
|
||||
--env <PATH>
|
||||
Interpreter or environment directory. Defaults to the one `env use` set, else an auto-detected `.venv` in the working directory
|
||||
|
||||
--engine <ENGINE>
|
||||
Override the configured engine for this invocation
|
||||
|
||||
Possible values:
|
||||
- pip: `<python> -m pip`
|
||||
- uv: `uv pip --python <python>`
|
||||
|
||||
--json
|
||||
Machine-readable output: exactly one JSON document on stdout (CLI-SPEC §6).
|
||||
|
||||
Prompts, warnings and engine output go to stderr. This said "NDJSON for streaming commands" until [VERSION], when several commands printed several documents.
|
||||
|
||||
-y, --yes
|
||||
Assume the default at every prompt: a held-back package keeps its compatible version and an impossible one is skipped. Only `--strategy latest` forces
|
||||
|
||||
--quiet
|
||||
Do not stream engine output to stderr. (The log file is written regardless.)
|
||||
|
||||
--verbose
|
||||
Also write the log to stderr, at debug level
|
||||
|
||||
--log-file <PATH>
|
||||
Also write the log to this file. The daily log in `data\logs\` is always kept
|
||||
|
||||
--no-snapshot
|
||||
DANGEROUS: skip the pre-batch snapshot. CI images only; prints a warning.
|
||||
|
||||
This is the sole documented escape from DATA-FLOW §9.2, and it exists because a throwaway CI container has nothing to roll back to.
|
||||
|
||||
-h, --help
|
||||
Print help (see a summary with '-h')
|
||||
@@ -2,7 +2,7 @@
|
||||
source: crates/pipdock-cli/tests/golden.rs
|
||||
expression: "stdout_of(&[parent, child, \"--help\"])"
|
||||
---
|
||||
List discovered environments, their sources and PEP 668 flags
|
||||
List discovered environments and the ones added with `env add`, their sources and PEP 668 flags
|
||||
|
||||
Usage: pipdock env list [OPTIONS]
|
||||
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
---
|
||||
source: crates/pipdock-cli/tests/golden.rs
|
||||
expression: "stdout_of(&[parent, child, \"--help\"])"
|
||||
---
|
||||
Take an interpreter added with `env add` off the list. Nothing on disk is touched
|
||||
|
||||
Usage: pipdock env remove [OPTIONS] <PATH>
|
||||
|
||||
Arguments:
|
||||
<PATH>
|
||||
Interpreter or environment directory, as it was added
|
||||
|
||||
Options:
|
||||
--env <PATH>
|
||||
Interpreter or environment directory. Defaults to the one `env use` set, else an auto-detected `.venv` in the working directory
|
||||
|
||||
--engine <ENGINE>
|
||||
Override the configured engine for this invocation
|
||||
|
||||
Possible values:
|
||||
- pip: `<python> -m pip`
|
||||
- uv: `uv pip --python <python>`
|
||||
|
||||
--json
|
||||
Machine-readable output: exactly one JSON document on stdout (CLI-SPEC §6).
|
||||
|
||||
Prompts, warnings and engine output go to stderr. This said "NDJSON for streaming commands" until [VERSION], when several commands printed several documents.
|
||||
|
||||
-y, --yes
|
||||
Assume the default at every prompt: a held-back package keeps its compatible version and an impossible one is skipped. Only `--strategy latest` forces
|
||||
|
||||
--quiet
|
||||
Do not stream engine output to stderr. (The log file is written regardless.)
|
||||
|
||||
--verbose
|
||||
Also write the log to stderr, at debug level
|
||||
|
||||
--log-file <PATH>
|
||||
Also write the log to this file. The daily log in `data\logs\` is always kept
|
||||
|
||||
--no-snapshot
|
||||
DANGEROUS: skip the pre-batch snapshot. CI images only; prints a warning.
|
||||
|
||||
This is the sole documented escape from DATA-FLOW §9.2, and it exists because a throwaway CI container has nothing to roll back to.
|
||||
|
||||
-h, --help
|
||||
Print help (see a summary with '-h')
|
||||
@@ -7,9 +7,11 @@ Manage environments
|
||||
Usage: pipdock env [OPTIONS] <COMMAND>
|
||||
|
||||
Commands:
|
||||
list List discovered environments, their sources and PEP 668 flags
|
||||
use Set the default environment
|
||||
help Print this message or the help of the given subcommand(s)
|
||||
list List discovered environments and the ones added with `env add`, their sources and PEP 668 flags
|
||||
use Set the default environment
|
||||
add Add an interpreter discovery does not find to the list (the GUI's Add interpreter…)
|
||||
remove Take an interpreter added with `env add` off the list. Nothing on disk is touched
|
||||
help Print this message or the help of the given subcommand(s)
|
||||
|
||||
Options:
|
||||
--env <PATH>
|
||||
|
||||
Reference in new issue
Block a user