|
1 | 1 | use std::error::Error; |
2 | 2 |
|
3 | | -use libpkgx::pantry_db; |
| 3 | +use libpkgx::{config::Config, inventory, pantry_db}; |
4 | 4 | use rusqlite::{params, Connection}; |
| 5 | +use serde::Serialize; |
5 | 6 |
|
6 | | -pub fn query(args: &Vec<String>, silent: bool, conn: &Connection) -> Result<(), Box<dyn Error>> { |
| 7 | +use crate::{ |
| 8 | + args::Flags, |
| 9 | + resolve::{parse_pkgspec, Pkgspec}, |
| 10 | +}; |
| 11 | + |
| 12 | +#[derive(Serialize)] |
| 13 | +struct QueryResult { |
| 14 | + project: String, |
| 15 | + programs: Vec<String>, |
| 16 | +} |
| 17 | + |
| 18 | +pub async fn query( |
| 19 | + args: &Vec<String>, |
| 20 | + flags: &Flags, |
| 21 | + conn: &Connection, |
| 22 | + config: &Config, |
| 23 | +) -> Result<(), Box<dyn Error>> { |
| 24 | + let is_json = flags.json == Some(2); |
| 25 | + let silent = flags.silent; |
| 26 | + |
| 27 | + // print out the whole list if no args |
7 | 28 | if args.is_empty() { |
8 | | - let mut stmt = conn.prepare("SELECT program FROM provides")?; |
9 | | - let mut rows = stmt.query(params![])?; |
10 | | - while let Some(row) = rows.next()? { |
11 | | - let program: String = row.get(0)?; |
12 | | - println!("{}", program); |
| 29 | + // if they requested json, output full mapping of project -> programs |
| 30 | + if is_json { |
| 31 | + let mut stmt = |
| 32 | + conn.prepare("SELECT DISTINCT project FROM provides ORDER BY project")?; |
| 33 | + let mut rows = stmt.query(params![])?; |
| 34 | + let mut results = Vec::new(); |
| 35 | + while let Some(row) = rows.next()? { |
| 36 | + let project: String = row.get(0)?; |
| 37 | + let programs = get_programs(conn, &project)?; |
| 38 | + results.push(QueryResult { project, programs }); |
| 39 | + } |
| 40 | + println!("{}", serde_json::to_string_pretty(&results)?); |
| 41 | + // if not, just list all programs |
| 42 | + } else { |
| 43 | + let mut stmt = conn.prepare("SELECT program FROM provides")?; |
| 44 | + let mut rows = stmt.query(params![])?; |
| 45 | + while let Some(row) = rows.next()? { |
| 46 | + let program: String = row.get(0)?; |
| 47 | + println!("{}", program); |
| 48 | + } |
13 | 49 | } |
14 | | - } else { |
15 | | - let mut fail = false; |
16 | | - for arg in args { |
17 | | - let projects = pantry_db::which(arg, conn)?; |
18 | | - if projects.is_empty() && silent { |
| 50 | + return Ok(()); |
| 51 | + } |
| 52 | + |
| 53 | + let mut results = Vec::new(); |
| 54 | + let mut fail = false; |
| 55 | + |
| 56 | + for arg in args { |
| 57 | + let mut pkgspec = parse_pkgspec(arg)?; |
| 58 | + |
| 59 | + let projects = match &mut pkgspec { |
| 60 | + Pkgspec::Req(req) if !req.project.contains('.') && req.constraint.raw == "*" => { |
| 61 | + pantry_db::which(&req.project, conn)? |
| 62 | + } |
| 63 | + Pkgspec::Req(req) => match resolve_project(&req.project, conn) { |
| 64 | + Ok(project) => { |
| 65 | + req.project = project.clone(); |
| 66 | + vec![project] |
| 67 | + } |
| 68 | + Err(e) => { |
| 69 | + if silent { |
| 70 | + std::process::exit(1); |
| 71 | + } |
| 72 | + println!("{}", e); |
| 73 | + fail = true; |
| 74 | + continue; |
| 75 | + } |
| 76 | + }, |
| 77 | + Pkgspec::Latest(name) => match resolve_project(name, conn) { |
| 78 | + Ok(project) => vec![project], |
| 79 | + Err(e) => { |
| 80 | + if silent { |
| 81 | + std::process::exit(1); |
| 82 | + } |
| 83 | + println!("{}", e); |
| 84 | + fail = true; |
| 85 | + continue; |
| 86 | + } |
| 87 | + }, |
| 88 | + }; |
| 89 | + |
| 90 | + if projects.is_empty() { |
| 91 | + if silent { |
19 | 92 | std::process::exit(1); |
20 | | - } else if projects.is_empty() { |
21 | | - println!("{} not found", arg); |
22 | | - fail = true; |
23 | | - } else if !silent { |
24 | | - println!("{}", projects.join(", ")); |
| 93 | + } |
| 94 | + println!("{} not found", arg); |
| 95 | + fail = true; |
| 96 | + continue; |
| 97 | + } |
| 98 | + |
| 99 | + // validate version constraint if specified |
| 100 | + if let Pkgspec::Req(req) = &pkgspec { |
| 101 | + if req.constraint.raw != "*" { |
| 102 | + let versions = inventory::ls(&projects[0], config).await?; |
| 103 | + if !versions.iter().any(|v| req.constraint.satisfies(v)) { |
| 104 | + if silent { |
| 105 | + std::process::exit(1); |
| 106 | + } |
| 107 | + println!( |
| 108 | + "no versions matching {} found for {}", |
| 109 | + req.constraint.raw, projects[0] |
| 110 | + ); |
| 111 | + fail = true; |
| 112 | + continue; |
| 113 | + } |
25 | 114 | } |
26 | 115 | } |
27 | | - if fail { |
28 | | - std::process::exit(1); |
| 116 | + |
| 117 | + if is_json { |
| 118 | + for project in &projects { |
| 119 | + let programs = get_programs(conn, project)?; |
| 120 | + results.push(QueryResult { |
| 121 | + project: project.clone(), |
| 122 | + programs, |
| 123 | + }); |
| 124 | + } |
| 125 | + } else if !silent { |
| 126 | + println!("{}", projects.join(", ")); |
29 | 127 | } |
30 | 128 | } |
| 129 | + |
| 130 | + if is_json { |
| 131 | + println!("{}", serde_json::to_string_pretty(&results)?); |
| 132 | + } |
| 133 | + |
| 134 | + if fail { |
| 135 | + std::process::exit(1); |
| 136 | + } |
| 137 | + |
31 | 138 | Ok(()) |
32 | 139 | } |
| 140 | + |
| 141 | +fn resolve_project(input: &str, conn: &Connection) -> Result<String, Box<dyn Error>> { |
| 142 | + let projects = pantry_db::which(&input.to_string(), conn)?; |
| 143 | + match projects.len() { |
| 144 | + 1 => Ok(projects[0].clone()), |
| 145 | + 0 => { |
| 146 | + if input.contains('.') { |
| 147 | + let mut stmt = conn.prepare("SELECT COUNT(*) FROM provides WHERE project = ?")?; |
| 148 | + let count: i64 = stmt.query_row(params![input], |row| row.get(0))?; |
| 149 | + if count > 0 { |
| 150 | + return Ok(input.to_string()); |
| 151 | + } |
| 152 | + } |
| 153 | + Err(format!("{} not found", input).into()) |
| 154 | + } |
| 155 | + _ => Err(format!("{} is ambiguous: {}", input, projects.join(", ")).into()), |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +fn get_programs(conn: &Connection, project: &str) -> Result<Vec<String>, Box<dyn Error>> { |
| 160 | + let mut stmt = |
| 161 | + conn.prepare("SELECT program FROM provides WHERE project = ? ORDER BY program")?; |
| 162 | + let mut rows = stmt.query(params![project])?; |
| 163 | + let mut programs = Vec::new(); |
| 164 | + while let Some(row) = rows.next()? { |
| 165 | + programs.push(row.get(0)?); |
| 166 | + } |
| 167 | + Ok(programs) |
| 168 | +} |
0 commit comments