-
Notifications
You must be signed in to change notification settings - Fork 535
feat: add pixi global shortcut CLI
#3341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Hofer-Julian
merged 14 commits into
prefix-dev:main
from
ruben-arts:feature/add_shortcut_cli
Mar 18, 2025
Merged
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1649af0
feat: add shortcut to global cli
ruben-arts bb56080
feat: list shortcuts in global list and install
ruben-arts 628c9a5
Merge branch 'main' into feature/add_shortcut_cli
ruben-arts 3e69ceb
docs: autogenerate new iteration
ruben-arts 81f7975
Update src/cli/global/shortcut.rs
ruben-arts 8e77e8e
Update src/cli/global/shortcut.rs
ruben-arts d738d3a
Update src/cli/global/shortcut.rs
ruben-arts 5259f42
Merge branch 'main' into feature/add_shortcut_cli
ruben-arts da78d75
fix: slight improvements and docs update
ruben-arts 2f188cb
fix: use the full removal of the shortcuts function (needs rattler up…
ruben-arts 0af7be4
Merge branch 'main' into feature/add_shortcut_cli
Hofer-Julian 1186764
fix: update rattler
ruben-arts fe067d9
Merge branch 'main' into feature/add_shortcut_cli
ruben-arts 575c8c6
docs: autogenerated
ruben-arts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <!--- This file is autogenerated. Do not edit manually! --> | ||
| # <code>[pixi](../../pixi.md) [global](../global.md) shortcut</code> | ||
|
|
||
| ## About | ||
| Interact with the shortcuts on your machine | ||
|
|
||
| --8<-- "docs/reference/cli/pixi/global/shortcut_extender.md:description" | ||
|
|
||
| ## Usage | ||
| ``` | ||
| pixi global shortcut <COMMAND> | ||
| ``` | ||
|
|
||
| ## Subcommands | ||
| | Command | Description | | ||
| |---------|-------------| | ||
| | [`add`](shortcut/add.md) | Add a shortcut from an environment to your machine. | | ||
| | [`remove`](shortcut/remove.md) | Remove shortcuts from your machine | | ||
|
|
||
|
|
||
| --8<-- "docs/reference/cli/pixi/global/shortcut_extender.md:example" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <!--- This file is autogenerated. Do not edit manually! --> | ||
| # <code>[pixi](../../../pixi.md) [global](../../global.md) [shortcut](../shortcut.md) add</code> | ||
|
|
||
| ## About | ||
| Add a shortcut from an environment to your machine. | ||
|
|
||
| --8<-- "docs/reference/cli/pixi/global/shortcut/add_extender.md:description" | ||
|
|
||
| ## Usage | ||
| ``` | ||
| pixi global shortcut add [OPTIONS] --environment <ENVIRONMENT> [PACKAGE]... | ||
| ``` | ||
|
|
||
| ## Arguments | ||
| - <a id="arg-<PACKAGE>" href="#arg-<PACKAGE>">`<PACKAGE>`</a> | ||
| : The package name to add the shortcuts from | ||
|
|
||
| ## Options | ||
| - <a id="arg---environment" href="#arg---environment">`--environment (-e) <ENVIRONMENT>`</a> | ||
| : The environment from which the shortcut should be added | ||
| <br>**required**: `true` | ||
|
|
||
| ## Config Options | ||
| - <a id="arg---tls-no-verify" href="#arg---tls-no-verify">`--tls-no-verify`</a> | ||
| : Do not verify the TLS certificate of the server | ||
| - <a id="arg---auth-file" href="#arg---auth-file">`--auth-file <AUTH_FILE>`</a> | ||
| : Path to the file containing the authentication token | ||
| - <a id="arg---pypi-keyring-provider" href="#arg---pypi-keyring-provider">`--pypi-keyring-provider <PYPI_KEYRING_PROVIDER>`</a> | ||
| : Specifies whether to use the keyring to look up credentials for PyPI | ||
| <br>**options**: `disabled`, `subprocess` | ||
| - <a id="arg---concurrent-solves" href="#arg---concurrent-solves">`--concurrent-solves <CONCURRENT_SOLVES>`</a> | ||
| : Max concurrent solves, default is the number of CPUs | ||
| - <a id="arg---concurrent-downloads" href="#arg---concurrent-downloads">`--concurrent-downloads <CONCURRENT_DOWNLOADS>`</a> | ||
| : Max concurrent network requests, default is `50` | ||
|
|
||
| --8<-- "docs/reference/cli/pixi/global/shortcut/add_extender.md:example" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| <!--- This file is autogenerated. Do not edit manually! --> | ||
| # <code>[pixi](../../../pixi.md) [global](../../global.md) [shortcut](../shortcut.md) remove</code> | ||
|
|
||
| ## About | ||
| Remove shortcuts from your machine | ||
|
|
||
| --8<-- "docs/reference/cli/pixi/global/shortcut/remove_extender.md:description" | ||
|
|
||
| ## Usage | ||
| ``` | ||
| pixi global shortcut remove [OPTIONS] [SHORTCUTS]... | ||
| ``` | ||
|
|
||
| ## Arguments | ||
| - <a id="arg-<SHORTCUTS>" href="#arg-<SHORTCUTS>">`<SHORTCUTS>`</a> | ||
| : The shortcuts that should be removed | ||
|
|
||
| ## Config Options | ||
| - <a id="arg---tls-no-verify" href="#arg---tls-no-verify">`--tls-no-verify`</a> | ||
| : Do not verify the TLS certificate of the server | ||
| - <a id="arg---auth-file" href="#arg---auth-file">`--auth-file <AUTH_FILE>`</a> | ||
| : Path to the file containing the authentication token | ||
| - <a id="arg---pypi-keyring-provider" href="#arg---pypi-keyring-provider">`--pypi-keyring-provider <PYPI_KEYRING_PROVIDER>`</a> | ||
| : Specifies whether to use the keyring to look up credentials for PyPI | ||
| <br>**options**: `disabled`, `subprocess` | ||
| - <a id="arg---concurrent-solves" href="#arg---concurrent-solves">`--concurrent-solves <CONCURRENT_SOLVES>`</a> | ||
| : Max concurrent solves, default is the number of CPUs | ||
| - <a id="arg---concurrent-downloads" href="#arg---concurrent-downloads">`--concurrent-downloads <CONCURRENT_DOWNLOADS>`</a> | ||
| : Max concurrent network requests, default is `50` | ||
|
|
||
| --8<-- "docs/reference/cli/pixi/global/shortcut/remove_extender.md:example" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| use crate::global::Project; | ||
| use crate::{ | ||
| cli::global::revert_environment_after_error, | ||
| global::{EnvironmentName, StateChanges}, | ||
| }; | ||
| use clap::Parser; | ||
| use fancy_display::FancyDisplay; | ||
| use miette::Context; | ||
| use pixi_config::{Config, ConfigCli}; | ||
| use rattler_conda_types::PackageName; | ||
| use std::collections::HashMap; | ||
|
|
||
| /// Add a shortcut from an environment to your machine. | ||
| #[derive(Parser, Debug)] | ||
| #[clap(arg_required_else_help = true, verbatim_doc_comment)] | ||
| pub struct AddArgs { | ||
| /// The package name to add the shortcuts from. | ||
| #[arg(num_args = 1.., value_name = "PACKAGE")] | ||
| packages: Vec<PackageName>, | ||
|
|
||
| /// The environment from which the shortcut should be added | ||
| #[clap(short, long)] | ||
| environment: EnvironmentName, | ||
|
|
||
| #[clap(flatten)] | ||
| config: ConfigCli, | ||
| } | ||
|
|
||
| /// Remove shortcuts from your machine. | ||
| #[derive(Parser, Debug)] | ||
| pub struct RemoveArgs { | ||
| /// The shortcuts that should be removed | ||
| #[arg(num_args = 1..)] | ||
| shortcuts: Vec<PackageName>, | ||
|
ruben-arts marked this conversation as resolved.
Outdated
|
||
|
|
||
| #[clap(flatten)] | ||
| config: ConfigCli, | ||
| } | ||
|
|
||
| /// Interact with the shortcuts on your machine | ||
|
ruben-arts marked this conversation as resolved.
Outdated
|
||
| #[derive(Parser, Debug)] | ||
| #[clap(group(clap::ArgGroup::new("command")))] | ||
| pub enum SubCommand { | ||
| #[clap(name = "add")] | ||
| Add(AddArgs), | ||
| #[clap(name = "remove")] | ||
| Remove(RemoveArgs), | ||
| } | ||
|
|
||
| /// Add or remove shortcuts from your machine | ||
| pub async fn execute(args: SubCommand) -> miette::Result<()> { | ||
| match args { | ||
| SubCommand::Add(args) => add(args).await?, | ||
| SubCommand::Remove(args) => remove(args).await?, | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| pub async fn add(args: AddArgs) -> miette::Result<()> { | ||
| let config = Config::with_cli_config(&args.config); | ||
| let project_original = Project::discover_or_create() | ||
| .await? | ||
| .with_cli_config(config.clone()); | ||
|
|
||
| async fn apply_changes( | ||
| args: &AddArgs, | ||
| project: &mut Project, | ||
| ) -> Result<StateChanges, miette::Error> { | ||
| let env_name = &args.environment; | ||
| let mut state_changes = StateChanges::new_with_env(env_name.clone()); | ||
| for name in &args.packages { | ||
| project.manifest.add_shortcut(env_name, name)?; | ||
| } | ||
| state_changes |= project.sync_environment(env_name, None).await?; | ||
| project.manifest.save().await?; | ||
| Ok(state_changes) | ||
| } | ||
|
|
||
| let mut project_modified = project_original.clone(); | ||
| match apply_changes(&args, &mut project_modified).await { | ||
| Ok(state_changes) => { | ||
| project_modified.manifest.save().await?; | ||
| state_changes.report(); | ||
| Ok(()) | ||
| } | ||
| Err(err) => { | ||
| if let Err(revert_err) = | ||
| revert_environment_after_error(&args.environment, &project_original).await | ||
| { | ||
| tracing::warn!("Reverting of the operation failed"); | ||
| tracing::info!("Reversion error: {:?}", revert_err); | ||
| } | ||
| Err(err) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub async fn remove(args: RemoveArgs) -> miette::Result<()> { | ||
| let config = Config::with_cli_config(&args.config); | ||
| let project_original = Project::discover_or_create() | ||
| .await? | ||
| .with_cli_config(config.clone()); | ||
|
|
||
| async fn apply_changes( | ||
| shortcuts: Vec<PackageName>, | ||
| env_name: &EnvironmentName, | ||
| project: &mut Project, | ||
| ) -> Result<StateChanges, miette::Error> { | ||
| let mut state_changes = StateChanges::new_with_env(env_name.clone()); | ||
|
|
||
| for shortcut in shortcuts { | ||
| project | ||
| .manifest | ||
| .remove_shortcut(&shortcut, env_name) | ||
| .wrap_err_with(|| { | ||
| format!( | ||
| "Couldn't remove shortcut name '{}' from {} environment", | ||
| shortcut.as_normalized(), | ||
| env_name.fancy_display() | ||
| ) | ||
| })?; | ||
| } | ||
|
|
||
| state_changes |= project.sync_environment(env_name, None).await?; | ||
| project.manifest.save().await?; | ||
| Ok(state_changes) | ||
| } | ||
|
|
||
| let to_remove_shortcuts_map: HashMap<EnvironmentName, Vec<PackageName>> = project_original | ||
| .environments() | ||
| .iter() | ||
| .filter_map(|(env_name, env)| { | ||
| env.shortcuts.as_ref().map(|shortcuts| { | ||
| let to_remove = shortcuts | ||
| .iter() | ||
| .filter(|shortcut| args.shortcuts.contains(shortcut)) | ||
| .cloned() | ||
| .collect::<Vec<_>>(); | ||
| (!to_remove.is_empty()).then(|| (env_name.clone(), to_remove)) | ||
| })? | ||
| }) | ||
| .collect(); | ||
|
|
||
| if to_remove_shortcuts_map.is_empty() { | ||
| miette::bail!( | ||
| "No shortcuts found with name(s): {}", | ||
| console::style( | ||
| args.shortcuts | ||
| .iter() | ||
| .map(|s| s.as_normalized()) | ||
| .collect::<Vec<_>>() | ||
| .join(", ") | ||
| ) | ||
| .bold() | ||
| .yellow() | ||
| ); | ||
| } | ||
|
|
||
| let mut last_updated_project = project_original; | ||
| for (env_name, shortcuts) in to_remove_shortcuts_map { | ||
| let mut project = last_updated_project.clone(); | ||
| match apply_changes(shortcuts, &env_name, &mut project) | ||
| .await | ||
| .wrap_err_with(|| { | ||
| format!( | ||
| "Couldn't remove shortcuts from {}", | ||
| env_name.fancy_display() | ||
| ) | ||
| }) { | ||
| Ok(state_changes) => { | ||
| state_changes.report(); | ||
| } | ||
| Err(err) => { | ||
| if let Err(revert_err) = | ||
| revert_environment_after_error(&env_name, &last_updated_project).await | ||
| { | ||
| tracing::warn!("Reverting of the operation failed"); | ||
| tracing::info!("Reversion error: {:?}", revert_err); | ||
| } | ||
| return Err(err); | ||
| } | ||
| } | ||
| last_updated_project = project; | ||
| } | ||
| Ok(()) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.