Skip to content

Commit

Permalink
add info sub-command (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv authored Jul 10, 2023
1 parent 86facd1 commit 6ddfced
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ rattler_virtual_packages = { default-features = false, git = "https://github.com
#rattler_networking = { default-features = false, path="../rattler/crates/rattler_networking" }
reqwest = { version = "0.11.16", default-features = false }
serde = "1.0.163"
serde_json = "1.0.96"
serde_spanned = "0.6.2"
serde_with = { version = "3.0.0", features = ["indexmap"] }
shlex = "1.1.0"
Expand Down
109 changes: 109 additions & 0 deletions src/cli/info.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
use std::{fmt::Display, path::PathBuf};

use clap::Parser;
use rattler_conda_types::{GenericVirtualPackage, Platform};
use rattler_virtual_packages::VirtualPackage;
use serde::Serialize;
use serde_with::serde_as;
use serde_with::DisplayFromStr;

use crate::Project;

#[derive(Parser, Debug)]
pub struct Args {
/// Wether to show the output as JSON or not
#[arg(long)]
json: bool,

/// The path to 'pixi.toml'
#[arg(long)]
pub manifest_path: Option<PathBuf>,
}

#[derive(Serialize)]
pub struct ProjectInfo {
tasks: Vec<String>,
manifest_path: PathBuf,
}

#[serde_as]
#[derive(Serialize)]
pub struct Info {
platform: String,
#[serde_as(as = "Vec<DisplayFromStr>")]
virtual_packages: Vec<GenericVirtualPackage>,
version: String,
cache_dir: Option<PathBuf>,
project_info: Option<ProjectInfo>,
}

impl Display for Info {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let cache_dir = match &self.cache_dir {
Some(path) => path.to_string_lossy().to_string(),
None => "None".to_string(),
};

writeln!(f, "pixi {}\n", self.version)?;
writeln!(f, "{:20}: {}", "Platform", self.platform)?;

for (i, p) in self.virtual_packages.iter().enumerate() {
if i == 0 {
writeln!(f, "{:20}: {}", "Virtual packages", p)?;
} else {
writeln!(f, "{:20}: {}", "", p)?;
}
}

writeln!(f, "{:20}: {}", "Cache dir", cache_dir)?;

if let Some(pi) = self.project_info.as_ref() {
writeln!(f, "\nProject\n------------\n")?;

writeln!(
f,
"{:20}: {}",
"Manifest file",
pi.manifest_path.to_string_lossy()
)?;

writeln!(f, "Tasks:")?;
for c in &pi.tasks {
writeln!(f, " - {}", c)?;
}
}

Ok(())
}
}

pub async fn execute(args: Args) -> anyhow::Result<()> {
let project = Project::load_or_else_discover(args.manifest_path.as_deref()).ok();

let project_info = project.map(|p| ProjectInfo {
manifest_path: p.root().to_path_buf().join("pixi.toml"),
tasks: p.manifest.tasks.keys().cloned().collect(),
});

let virtual_packages = VirtualPackage::current()?
.iter()
.cloned()
.map(GenericVirtualPackage::from)
.collect::<Vec<_>>();

let info = Info {
platform: Platform::current().to_string(),
virtual_packages,
version: env!("CARGO_PKG_VERSION").to_string(),
cache_dir: rattler::default_cache_dir().ok(),
project_info,
};

if args.json {
println!("{}", serde_json::to_string_pretty(&info)?);
Ok(())
} else {
println!("{}", info);
Ok(())
}
}
3 changes: 3 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use tracing_subscriber::{filter::LevelFilter, util::SubscriberInitExt, EnvFilter
pub mod add;
pub mod auth;
pub mod global;
pub mod info;
pub mod init;
pub mod install;
pub mod run;
Expand Down Expand Up @@ -53,6 +54,7 @@ pub enum Command {
#[clap(alias = "i")]
Install(install::Args),
Task(task::Args),
Info(info::Args),
}

fn completion(args: CompletionCommand) -> Result<(), Error> {
Expand Down Expand Up @@ -108,5 +110,6 @@ pub async fn execute_command(command: Command) -> Result<(), Error> {
Command::Install(cmd) => install::execute(cmd).await,
Command::Shell(cmd) => shell::execute(cmd).await,
Command::Task(cmd) => task::execute(cmd),
Command::Info(cmd) => info::execute(cmd).await,
}
}

0 comments on commit 6ddfced

Please sign in to comment.