Skip to content

Commit

Permalink
fix: Parse properly 'default' as environment Cli argument (#1247)
Browse files Browse the repository at this point in the history
Co-authored-by: Tim de Jager <tdejager89@gmail.com>
  • Loading branch information
olivier-lacroix and tdejager authored Apr 25, 2024
1 parent c4346eb commit 0879173
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 35 deletions.
6 changes: 1 addition & 5 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::config::ConfigCli;
use crate::environment::get_up_to_date_prefix;
use crate::project::manifest::EnvironmentName;
use crate::Project;
use clap::Parser;
use indexmap::IndexMap;
Expand All @@ -26,10 +25,7 @@ pub struct Args {
pub async fn execute(args: Args) -> miette::Result<()> {
let project =
Project::load_or_else_discover(args.manifest_path.as_deref())?.with_cli_config(args.config);
let environment_name = EnvironmentName::from_arg_or_env_var(args.environment);
let environment = project
.environment(&environment_name)
.ok_or_else(|| miette::miette!("unknown environment '{environment_name}'"))?;
let environment = project.environment_from_name_or_env_var(args.environment)?;

get_up_to_date_prefix(
&environment,
Expand Down
10 changes: 3 additions & 7 deletions src/cli/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use serde::Serialize;
use uv_distribution::RegistryWheelIndex;

use crate::lock_file::{UpdateLockFileOptions, UvResolutionContext};
use crate::project::manifest::EnvironmentName;
use crate::pypi_tags::{get_pypi_tags, is_python_record};
use crate::Project;

Expand Down Expand Up @@ -107,10 +106,7 @@ where

pub async fn execute(args: Args) -> miette::Result<()> {
let project = Project::load_or_else_discover(args.manifest_path.as_deref())?;
let environment_name = EnvironmentName::from_arg_or_env_var(args.environment);
let environment = project
.environment(&environment_name)
.ok_or_else(|| miette::miette!("unknown environment '{environment_name}'"))?;
let environment = project.environment_from_name_or_env_var(args.environment)?;

let lock_file = project
.up_to_date_lock_file(UpdateLockFileOptions {
Expand Down Expand Up @@ -209,8 +205,8 @@ pub async fn execute(args: Args) -> miette::Result<()> {
// print packages as json
json_packages(&packages_to_output, args.json_pretty);
} else {
if !environment_name.is_default() {
eprintln!("Environment: {}", environment_name.fancy_display());
if !environment.is_default() {
eprintln!("Environment: {}", environment.name().fancy_display());
}

// print packages as table
Expand Down
7 changes: 2 additions & 5 deletions src/cli/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,14 +203,11 @@ async fn start_nu_shell(
pub async fn execute(args: Args) -> miette::Result<()> {
let project =
Project::load_or_else_discover(args.manifest_path.as_deref())?.with_cli_config(args.config);
let environment_name = EnvironmentName::from_arg_or_env_var(args.environment);
let environment = project
.environment(&environment_name)
.ok_or_else(|| miette::miette!("unknown environment '{environment_name}'"))?;
let environment = project.environment_from_name_or_env_var(args.environment)?;

verify_current_platform_has_required_virtual_packages(&environment).into_diagnostic()?;

let prompt_name = match environment_name {
let prompt_name = match environment.name() {
EnvironmentName::Default => project.name().to_string(),
EnvironmentName::Named(name) => format!("{}:{}", project.name(), name),
};
Expand Down
9 changes: 3 additions & 6 deletions src/cli/task.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::project::manifest::{EnvironmentName, FeatureName};
use crate::project::manifest::FeatureName;
use crate::task::{quote, Alias, CmdArgs, Execute, Task, TaskName};
use crate::Project;
use clap::Parser;
use indexmap::IndexMap;
use itertools::Itertools;
use miette::miette;
use rattler_conda_types::Platform;
use std::error::Error;
use std::path::PathBuf;
Expand Down Expand Up @@ -274,10 +273,8 @@ pub fn execute(args: Args) -> miette::Result<()> {
);
}
Operation::List(args) => {
let env = EnvironmentName::from_arg_or_env_var(args.environment);
let tasks = project
.environment(&env)
.ok_or(miette!("Environment `{}` not found in project", env))?
let environment = project.environment_from_name_or_env_var(args.environment)?;
let tasks = environment
.tasks(Some(Platform::current()), true)?
.into_keys()
.collect_vec();
Expand Down
10 changes: 3 additions & 7 deletions src/cli/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use itertools::Itertools;
use rattler_conda_types::Platform;

use crate::lock_file::UpdateLockFileOptions;
use crate::project::manifest::EnvironmentName;
use crate::Project;

/// Show a tree of project dependencies
Expand Down Expand Up @@ -70,10 +69,7 @@ static UTF8_SYMBOLS: Symbols = Symbols {

pub async fn execute(args: Args) -> miette::Result<()> {
let project = Project::load_or_else_discover(args.manifest_path.as_deref())?;
let environment_name = EnvironmentName::from_arg_or_env_var(args.environment);
let environment = project
.environment(&environment_name)
.ok_or_else(|| miette::miette!("unknown environment '{environment_name}'"))?;
let environment = project.environment_from_name_or_env_var(args.environment)?;
let lock_file = project
.up_to_date_lock_file(UpdateLockFileOptions {
lock_file_usage: args.lock_file_usage.into(),
Expand All @@ -92,8 +88,8 @@ pub async fn execute(args: Args) -> miette::Result<()> {

let direct_deps = direct_dependencies(&environment, &platform, &dep_map);

if !environment_name.is_default() {
eprintln!("Environment: {}", environment_name.fancy_display());
if !environment.is_default() {
eprintln!("Environment: {}", environment.name().fancy_display());
}

if args.invert {
Expand Down
12 changes: 7 additions & 5 deletions src/project/manifest/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,20 @@ impl EnvironmentName {

/// Tries to read the environment name from an argument, then it will try
/// to read from an environment variable, otherwise it will fall back to default
pub fn from_arg_or_env_var(arg_name: Option<String>) -> Self {
pub fn from_arg_or_env_var(
arg_name: Option<String>,
) -> Result<Self, ParseEnvironmentNameError> {
if let Some(arg_name) = arg_name {
return EnvironmentName::Named(arg_name);
return EnvironmentName::from_str(&arg_name);
} else if std::env::var("PIXI_IN_SHELL").is_ok() {
if let Ok(env_var_name) = std::env::var("PIXI_ENVIRONMENT_NAME") {
if env_var_name == consts::DEFAULT_ENVIRONMENT_NAME {
return EnvironmentName::Default;
return Ok(EnvironmentName::Default);
}
return EnvironmentName::Named(env_var_name);
return Ok(EnvironmentName::Named(env_var_name));
}
}
EnvironmentName::Default
Ok(EnvironmentName::Default)
}
}

Expand Down
10 changes: 10 additions & 0 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,16 @@ impl Project {
.collect()
}

/// Returns an environment in this project based on a name or an environment variable.
pub fn environment_from_name_or_env_var(
&self,
name: Option<String>,
) -> miette::Result<Environment> {
let environment_name = EnvironmentName::from_arg_or_env_var(name).into_diagnostic()?;
self.environment(&environment_name)
.ok_or_else(|| miette::miette!("unknown environment '{environment_name}'"))
}

/// Returns all the solve groups in the project.
pub fn solve_groups(&self) -> Vec<SolveGroup> {
self.manifest
Expand Down

0 comments on commit 0879173

Please sign in to comment.