Skip to content

Commit 2cb301b

Browse files
KixironJoshua Nelson
authored andcommitted
Exposed priority commands to the binary
1 parent 5fdec26 commit 2cb301b

File tree

3 files changed

+72
-28
lines changed

3 files changed

+72
-28
lines changed

src/bin/cratesfyi.rs

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::path::{Path, PathBuf};
33

44
use clap::{App, AppSettings, Arg, SubCommand};
55
use cratesfyi::db::{add_path_into_database, connect_db};
6-
use cratesfyi::utils::add_crate_to_queue;
6+
use cratesfyi::utils::{add_crate_to_queue, remove_crate_priority, set_crate_priority};
77
use cratesfyi::Server;
88
use cratesfyi::{db, DocBuilder, DocBuilderOptions, RustwideBuilder};
99

@@ -140,7 +140,26 @@ pub fn main() {
140140
.short("p")
141141
.long("priority")
142142
.help("Priority of build (default: 5) (new crate builds get priority 0)")
143-
.takes_value(true))))
143+
.takes_value(true)))
144+
.subcommand(SubCommand::with_name("priority")
145+
.about("Interactions with build queue priorities")
146+
.setting(AppSettings::ArgRequiredElseHelp)
147+
.subcommand(SubCommand::with_name("set")
148+
.about("Set all crates matching the given pattern to a priority level")
149+
.arg(Arg::with_name("PATTERN")
150+
.index(1)
151+
.required(true)
152+
.help("See https://www.postgresql.org/docs/current/functions-matching.html"))
153+
.arg(Arg::with_name("PRIORITY")
154+
.index(2)
155+
.required(true)
156+
.help("The priority to give crates matching PATTERN"))
157+
.subcommand(SubCommand::with_name("remove")
158+
.about("Remove the prioritization of crates by the given pattern")
159+
.arg(Arg::with_name("PATTERN")
160+
.index(1)
161+
.required(true)
162+
.help("See https://www.postgresql.org/docs/current/functions-matching.html"))))))
144163
.get_matches();
145164

146165
if let Some(matches) = matches.subcommand_matches("build") {
@@ -299,6 +318,29 @@ pub fn main() {
299318
priority,
300319
)
301320
.expect("Could not add crate to queue");
321+
} else if let Some(matches) = matches.subcommand_matches("set") {
322+
let pattern = matches
323+
.value_of("PATTERN")
324+
.expect("You must give a pattern to match with");
325+
let priority = clap::value_t!(matches.value_of("PRIORITY"), i32)
326+
.expect("You must give a priority for a crate");
327+
let conn = connect_db().expect("Could not connect to the database");
328+
329+
set_crate_priority(&conn, pattern, priority).expect("Could not set pattern's priority");
330+
} else if let Some(matches) = matches.subcommand_matches("remove") {
331+
let pattern = matches
332+
.value_of("PATTERN")
333+
.expect("You must give a pattern to remove");
334+
let conn = connect_db().expect("Could not connect to the database");
335+
336+
if remove_crate_priority(&conn, pattern)
337+
.expect("Could not remove pattern's priority")
338+
.is_some()
339+
{
340+
println!("Removed pattern");
341+
} else {
342+
println!("Pattern did not exist and so was not removed");
343+
}
302344
}
303345
}
304346
}

src/utils/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ pub(crate) use self::copy::copy_doc_dir;
55
pub use self::daemon::start_daemon;
66
pub use self::github_updater::github_updater;
77
pub use self::html::extract_head_and_body;
8-
pub use self::queue::{add_crate_to_queue, get_crate_priority};
8+
pub use self::queue::{
9+
add_crate_to_queue, get_crate_priority, remove_crate_priority, set_crate_priority,
10+
};
911
pub use self::release_activity_updater::update_release_activity;
1012
pub(crate) use self::rustc_version::parse_rustc_version;
1113

src/utils/queue.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@ pub fn get_crate_priority(conn: &Connection, name: &str) -> Result<i32> {
2121
}
2222
}
2323

24+
/// Set all crates that match [`pattern`] to have a certain priority
25+
///
26+
/// Note: `pattern` is used in a `LIKE` statement, so it must follow the postgres like syntax
27+
///
28+
/// [`pattern`]: https://www.postgresql.org/docs/8.3/functions-matching.html
29+
pub fn set_crate_priority(conn: &Connection, pattern: &str, priority: i32) -> Result<()> {
30+
conn.query(
31+
"INSERT INTO crate_priorities (pattern, priority) VALUES ($1, $2)",
32+
&[&pattern, &priority],
33+
)?;
34+
35+
Ok(())
36+
}
37+
38+
/// Remove a pattern from the priority table, returning the priority that it was associated with or `None`
39+
/// if nothing was removed
40+
pub fn remove_crate_priority(conn: &Connection, pattern: &str) -> Result<Option<i32>> {
41+
let query = conn.query(
42+
"DELETE FROM crate_priorities WHERE pattern = $1 RETURNING priority",
43+
&[&pattern],
44+
)?;
45+
46+
Ok(query.iter().next().map(|row| row.get(0)))
47+
}
48+
2449
/// Adds a crate to the build queue to be built by rustdoc. `priority` should be gotten from `get_crate_priority`
2550
pub fn add_crate_to_queue(
2651
conn: &Connection,
@@ -41,31 +66,6 @@ mod tests {
4166
use super::*;
4267
use crate::test::wrapper;
4368

44-
/// Set all crates that match [`pattern`] to have a certain priority
45-
///
46-
/// Note: `pattern` is used in a `LIKE` statement, so it must follow the postgres like syntax
47-
///
48-
/// [`pattern`]: https://www.postgresql.org/docs/8.3/functions-matching.html
49-
pub fn set_crate_priority(conn: &Connection, pattern: &str, priority: i32) -> Result<()> {
50-
conn.query(
51-
"INSERT INTO crate_priorities (pattern, priority) VALUES ($1, $2)",
52-
&[&pattern, &priority],
53-
)?;
54-
55-
Ok(())
56-
}
57-
58-
/// Remove a pattern from the priority table, returning the priority that it was associated with or `None`
59-
/// if nothing was removed
60-
pub fn remove_crate_priority(conn: &Connection, pattern: &str) -> Result<Option<i32>> {
61-
let query = conn.query(
62-
"DELETE FROM crate_priorities WHERE pattern = $1 RETURNING priority",
63-
&[&pattern],
64-
)?;
65-
66-
Ok(query.iter().next().map(|row| row.get(0)))
67-
}
68-
6969
#[test]
7070
fn set_priority() {
7171
wrapper(|env| {

0 commit comments

Comments
 (0)