summaryrefslogtreecommitdiffstats
path: root/src/db
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-11-03 18:57:52 +0100
committerMatthias Beyer <mail@beyermatthias.de>2020-11-03 18:57:52 +0100
commitce29de4d37e11cda5b0118cf005b7a38a70b479f (patch)
tree648e9aa4390a9499de7aea0738b5a8014a2cff36 /src/db
parent90e60f99167b8d05c2128b3045199542bbf76f56 (diff)
Add subcommand to list images from database
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
Diffstat (limited to 'src/db')
-rw-r--r--src/db/cli.rs11
-rw-r--r--src/db/interface.rs41
-rw-r--r--src/db/models/image.rs5
-rw-r--r--src/db/models/mod.rs3
4 files changed, 60 insertions, 0 deletions
diff --git a/src/db/cli.rs b/src/db/cli.rs
index 486b26d..d883bf7 100644
--- a/src/db/cli.rs
+++ b/src/db/cli.rs
@@ -53,5 +53,16 @@ pub fn cli<'a>() -> App<'a> {
)
)
+ .subcommand(App::new("images")
+ .about("List images from the DB")
+ .arg(Arg::with_name("csv")
+ .required(false)
+ .multiple(false)
+ .long("csv")
+ .takes_value(false)
+ .help("Format output as CSV")
+ )
+ )
+
}
diff --git a/src/db/interface.rs b/src/db/interface.rs
index 919cfe0..496f5b8 100644
--- a/src/db/interface.rs
+++ b/src/db/interface.rs
@@ -17,6 +17,7 @@ pub fn interface(db_connection_config: DbConnectionConfig, matches: &ArgMatches,
("cli", Some(matches)) => cli(db_connection_config, matches, config),
("artifacts", Some(matches)) => artifacts(db_connection_config, matches),
("envvars", Some(matches)) => envvars(db_connection_config, matches),
+ ("images", Some(matches)) => images(db_connection_config, matches),
(other, _) => return Err(anyhow!("Unknown subcommand: {}", other)),
}
}
@@ -199,6 +200,46 @@ fn envvars(conn_cfg: DbConnectionConfig, matches: &ArgMatches) -> Result<()> {
Ok(())
}
+fn images(conn_cfg: DbConnectionConfig, matches: &ArgMatches) -> Result<()> {
+ use diesel::RunQueryDsl;
+ use crate::schema::images::dsl;
+ use crate::db::models;
+
+ let csv = matches.is_present("csv");
+
+ let hdrs = vec![
+ {
+ let mut column = ascii_table::Column::default();
+ column.header = "Id".into();
+ column.align = ascii_table::Align::Left;
+ column
+ },
+ {
+ let mut column = ascii_table::Column::default();
+ column.header = "Name".into();
+ column.align = ascii_table::Align::Left;
+ column
+ }
+ ];
+
+ let connection = crate::db::establish_connection(conn_cfg)?;
+ let data = dsl::images
+ .load::<models::Image>(&connection)?
+ .into_iter()
+ .map(|image| {
+ vec![format!("{}", image.id), image.name]
+ })
+ .collect::<Vec<_>>();
+
+ if data.is_empty() {
+ info!("No images in database");
+ } else {
+ display_data(hdrs, data, csv)?;
+ }
+
+ Ok(())
+}
+
/// Display the passed data as nice ascii table,
/// or, if stdout is a pipe, print it nicely parseable
fn display_data<D: Display>(headers: Vec<ascii_table::Column>, data: Vec<Vec<D>>, csv: bool) -> Result<()> {
diff --git a/src/db/models/image.rs b/src/db/models/image.rs
new file mode 100644
index 0000000..b8dd552
--- /dev/null
+++ b/src/db/models/image.rs
@@ -0,0 +1,5 @@
+#[derive(Queryable)]
+pub struct Image {
+ pub id: i32,
+ pub name: String,
+}
diff --git a/src/db/models/mod.rs b/src/db/models/mod.rs
index 17b694b..8dbd7aa 100644
--- a/src/db/models/mod.rs
+++ b/src/db/models/mod.rs
@@ -4,3 +4,6 @@ pub use artifact::*;
mod envvar;
pub use envvar::*;
+mod image;
+pub use image::*;
+