Skip to main content

wapm_cli/commands/
search.rs

1//! Code pertaining to the `search` subcommand, which queries the server about
2//! the specified package.
3
4use crate::graphql::execute_query;
5
6use graphql_client::*;
7
8use prettytable::format;
9use prettytable::Table;
10use structopt::StructOpt;
11
12/// Options for the `search` subcommand
13#[derive(StructOpt, Debug)]
14pub struct SearchOpt {
15    #[structopt(parse(from_str))]
16    query: String,
17}
18
19type DateTime = String;
20
21#[derive(GraphQLQuery)]
22#[graphql(
23    schema_path = "graphql/schema.graphql",
24    query_path = "graphql/queries/search.graphql",
25    response_derives = "Debug"
26)]
27struct SearchQuery;
28
29/// Run the search command
30pub fn search(options: SearchOpt) -> anyhow::Result<()> {
31    let query = options.query;
32    let q = SearchQuery::build_query(search_query::Variables {
33        query: query.to_string(),
34    });
35    let response: search_query::ResponseData = execute_query(&q)?;
36
37    if response.search.edges.is_empty() {
38        println!("No packages found for \"{}\"", query);
39        return Ok(());
40    }
41    let mut table = Table::new();
42    table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
43
44    // Add a row per time
45    table.add_row(row!["NAME", "DESCRIPTION", "DATE", "VERSION"]);
46    for edge in response.search.edges.into_iter() {
47        let node = edge.unwrap().node;
48
49        if let Some(search_query::SearchQuerySearchEdgesNode::PackageVersion(version)) = node {
50            table.add_row(row![
51                version.package.display_name,
52                version.description,
53                version.created_at[..10],
54                version.version
55            ]);
56        }
57    }
58    table.printstd();
59
60    Ok(())
61}