Skip to content

Commit 3c541a6

Browse files
authored
Merge pull request #1151 from pkgxdev/query-mode
--query,-Q
2 parents 35f6bbe + 4ae6c22 commit 3c541a6

6 files changed

Lines changed: 79 additions & 23 deletions

File tree

.github/workflows/ci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,11 @@ jobs:
150150
if: ${{ matrix.os == 'ubuntu-latest' }}
151151
# ^^ only on one platform as wasteful otherwise
152152
153+
- run: |
154+
pkgx -Q hyperfine
155+
! test -d ~/.pkgx/crates.io/hyperfine
156+
! pkgx -Q flubber-flubber
157+
153158
- run: if [ $(find ~/.pkgx -name .tmp\* -type d | wc -l) -gt 0 ]; then
154159
exit 1;
155160
fi

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44

55
[![coverage][]][coveralls] [![teaRank][]](https://tea.xyz)
66

7+
> [!INFO]
8+
> You just want your shit to work and we want that too. We pride ourselves
9+
> on packaging things as well as possible because we want you to change the
10+
> world with what you build upon the *best* base we can give you.
11+
712
 
813

914
### Quickstart

crates/cli/src/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ pub enum Mode {
44
X,
55
Help,
66
Version,
7+
Query,
78
}
89

910
pub struct Flags {
@@ -59,6 +60,7 @@ pub fn parse() -> Args {
5960
"--help" => mode = Mode::Help,
6061
"--version" => mode = Mode::Version,
6162
"--quiet" => quiet = true,
63+
"--query" => mode = Mode::Query,
6264
"--shellcode" => {
6365
if !silent {
6466
eprintln!("{}", style("⨯ migration required").red());
@@ -96,6 +98,7 @@ pub fn parse() -> Args {
9698
'j' => json = true,
9799
'v' => version_n_continue = true,
98100
'!' => shebang = true,
101+
'Q' => mode = Mode::Query,
99102
_ => panic!("unknown argument: -{}", c),
100103
}
101104
}

crates/cli/src/main.rs

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
mod args;
22
mod execve;
33
mod help;
4+
mod query;
5+
mod setup;
46
#[cfg(test)]
57
mod tests;
68

@@ -9,7 +11,6 @@ use std::{collections::HashMap, error::Error, fmt::Write, sync::Arc, time::Durat
911
use execve::execve;
1012
use indicatif::{ProgressBar, ProgressState, ProgressStyle};
1113
use libpkgx::{
12-
config::Config,
1314
env::{self, construct_platform_case_aware_env_key},
1415
hydrate::hydrate,
1516
install_multi, pantry_db,
@@ -45,31 +46,14 @@ async fn main() -> Result<(), Box<dyn Error>> {
4546
println!("pkgx {}", env!("CARGO_PKG_VERSION"));
4647
return Ok(());
4748
}
49+
args::Mode::Query => {
50+
let (conn, _, _, _) = setup::setup(&flags).await?;
51+
return query::query(&args, flags.silent, &conn);
52+
}
4853
args::Mode::X => (),
4954
}
5055

51-
let config = Config::new()?;
52-
53-
std::fs::create_dir_all(config.pantry_db_file.parent().unwrap())?;
54-
let mut conn = Connection::open(&config.pantry_db_file)?;
55-
56-
let spinner = if flags.silent || flags.quiet {
57-
None
58-
} else {
59-
let spinner = indicatif::ProgressBar::new_spinner();
60-
spinner.enable_steady_tick(Duration::from_millis(100));
61-
Some(spinner)
62-
};
63-
64-
let did_sync = if sync::should(&config)? {
65-
if let Some(spinner) = &spinner {
66-
spinner.set_message("syncing pkg-db…");
67-
}
68-
sync::ensure(&config, &mut conn).await?;
69-
true
70-
} else {
71-
false
72-
};
56+
let (mut conn, did_sync, config, spinner) = setup::setup(&flags).await?;
7357

7458
if let Some(spinner) = &spinner {
7559
spinner.set_message("resolving pkg graph…");

crates/cli/src/query.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use std::error::Error;
2+
3+
use libpkgx::pantry_db;
4+
use rusqlite::Connection;
5+
6+
pub fn query(args: &Vec<String>, silent: bool, conn: &Connection) -> Result<(), Box<dyn Error>> {
7+
let mut fail = false;
8+
for arg in args {
9+
let projects = pantry_db::which(arg, conn)?;
10+
if projects.is_empty() && silent {
11+
std::process::exit(1);
12+
} else if projects.is_empty() {
13+
println!("{} not found", arg);
14+
fail = true;
15+
} else if !silent {
16+
println!("{}", projects.join(", "));
17+
}
18+
}
19+
if fail {
20+
std::process::exit(1);
21+
}
22+
Ok(())
23+
}

crates/cli/src/setup.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
use std::time::Duration;
2+
3+
use indicatif::ProgressBar;
4+
use libpkgx::{config::Config, sync};
5+
use rusqlite::Connection;
6+
7+
use crate::args::Flags;
8+
9+
pub async fn setup(
10+
flags: &Flags,
11+
) -> Result<(Connection, bool, Config, Option<ProgressBar>), Box<dyn std::error::Error>> {
12+
let config = Config::new()?;
13+
14+
std::fs::create_dir_all(config.pantry_db_file.parent().unwrap())?;
15+
let mut conn = Connection::open(&config.pantry_db_file)?;
16+
17+
let spinner = if flags.silent || flags.quiet {
18+
None
19+
} else {
20+
let spinner = indicatif::ProgressBar::new_spinner();
21+
spinner.enable_steady_tick(Duration::from_millis(100));
22+
Some(spinner)
23+
};
24+
25+
let did_sync = if sync::should(&config)? {
26+
if let Some(spinner) = &spinner {
27+
spinner.set_message("syncing pkg-db…");
28+
}
29+
sync::ensure(&config, &mut conn).await?;
30+
true
31+
} else {
32+
false
33+
};
34+
35+
Ok((conn, did_sync, config, spinner))
36+
}

0 commit comments

Comments
 (0)