| 1 |
//! Data types for output format and format version choice. |
| 2 |
//! |
| 3 |
//! These data types express the values of the `--output-format` and |
| 4 |
//! `--output-version` global options to `sq`. |
| 5 |
|
| 6 |
use std::io::Write; |
| 7 |
|
| 8 |
use anyhow::{anyhow, Result}; |
| 9 |
|
| 10 |
pub mod hint; |
| 11 |
pub mod import; |
| 12 |
pub mod pluralize; |
| 13 |
pub mod sanitize; |
| 14 |
pub mod wrapping; |
| 15 |
|
| 16 |
pub use keyring::ListItem as KeyringListItem; |
| 17 |
|
| 18 |
use crate::cli::types::Version; |
| 19 |
|
| 20 |
pub const DEFAULT_OUTPUT_VERSION: Version = Version::new(0, 0, 0); |
| 21 |
pub const OUTPUT_VERSIONS: &[Version] = &[Version::new(0, 0, 0)]; |
| 22 |
|
| 23 |
/// A model for the output of `sq` subcommands. |
| 24 |
/// |
| 25 |
/// This is for adding machine-readable output (such as JSON) to |
| 26 |
/// subcommand. Every subcommand is represented as a variant, for each |
| 27 |
/// version of the output. Versioning is global. We keep the latest |
| 28 |
/// subversion of each major version. |
| 29 |
/// |
| 30 |
/// Each variant is created by a dedicated function. |
| 31 |
pub enum Model { |
| 32 |
KeyringListV0(keyring::ListV0), |
| 33 |
} |
| 34 |
|
| 35 |
impl Model { |
| 36 |
|
| 37 |
fn version(v: Option<Version>) -> Version { |
| 38 |
v.unwrap_or(DEFAULT_OUTPUT_VERSION) |
| 39 |
} |
| 40 |
|
| 41 |
/// Create a model for the output of the `sq keyring list` |
| 42 |
/// subcommand. |
| 43 |
pub fn keyring_list(certs: Vec<keyring::ListItem>, all_uids: bool) -> Result<Self> { |
| 44 |
let version = Self::version(None); |
| 45 |
let result = match version { |
| 46 |
keyring::ListV0::V => Self::KeyringListV0(keyring::ListV0::new(certs, all_uids)), |
| 47 |
_ => return Err(anyhow!("unknown output version {:?}", version)), |
| 48 |
}; |
| 49 |
Ok(result) |
| 50 |
} |
| 51 |
|
| 52 |
/// Write the output of a model to an open write handle in the |
| 53 |
/// format requested by the user. |
| 54 |
pub fn write(&self, w: &mut dyn Write) -> Result<()> { |
| 55 |
match self { |
| 56 |
Self::KeyringListV0(x) => x.human_readable(w)? |
| 57 |
} |
| 58 |
Ok(()) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// Model output as a data type that can be serialized. |
| 63 |
mod keyring { |
| 64 |
use sequoia_openpgp as openpgp; |
| 65 |
use openpgp::{ |
| 66 |
Result, |
| 67 |
cert::Cert, |
| 68 |
}; |
| 69 |
use crate::Sq; |
| 70 |
use crate::common::ui; |
| 71 |
use super::{Version, Write}; |
| 72 |
use serde::Serialize; |
| 73 |
|
| 74 |
#[derive(Debug, Serialize)] |
| 75 |
pub struct ListV0 { |
| 76 |
#[serde(skip)] |
| 77 |
all_uids: bool, |
| 78 |
sq_output_version: Version, |
| 79 |
keys: Vec<ListItem>, |
| 80 |
} |
| 81 |
|
| 82 |
impl ListV0 { |
| 83 |
pub const V: Version = Version::new(0, 0, 0); |
| 84 |
|
| 85 |
pub fn new(keys: Vec<ListItem>, all_uids: bool) -> Self { |
| 86 |
Self { |
| 87 |
all_uids, |
| 88 |
sq_output_version: Self::V, |
| 89 |
keys, |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
pub fn human_readable(&self, w: &mut dyn Write) -> Result<()> { |
| 94 |
for (i, item) in self.keys.iter().enumerate() { |
| 95 |
match item { |
| 96 |
ListItem::Error(e) => { |
| 97 |
writeln!(w, "{}. {}", i, e)?; |
| 98 |
}, |
| 99 |
ListItem::Cert(cert) => { |
| 100 |
let line = format!("{}. {}", i, cert.fingerprint); |
| 101 |
let indent = line.chars().map(|_| ' ').collect::<String>(); |
| 102 |
write!(w, "{}", line)?; |
| 103 |
match &cert.primary_userid { |
| 104 |
Some(uid) => writeln!(w, " {}", uid)?, |
| 105 |
None => writeln!(w)?, |
| 106 |
} |
| 107 |
if self.all_uids { |
| 108 |
for uid in &cert.userids { |
| 109 |
writeln!(w, "{} {}", indent, uid)?; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
Ok(()) |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
#[derive(Debug, Serialize)] |
| 120 |
#[serde(untagged)] |
| 121 |
pub enum ListItem { |
| 122 |
Error(String), |
| 123 |
Cert(OutputCert), |
| 124 |
} |
| 125 |
|
| 126 |
impl ListItem { |
| 127 |
pub fn from_cert_with_sq(item: Result<Cert>, sq: &Sq) -> Self { |
| 128 |
match item { |
| 129 |
Ok(cert) => ListItem::Cert(OutputCert::from_cert_with_sq(cert, sq)), |
| 130 |
Err(e) => ListItem::Error(format!("{}", e)), |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
#[derive(Debug, Serialize)] |
| 136 |
pub struct OutputCert { |
| 137 |
fingerprint: String, |
| 138 |
primary_userid: Option<String>, |
| 139 |
userids: Vec<String>, |
| 140 |
} |
| 141 |
|
| 142 |
impl OutputCert { |
| 143 |
fn from_cert_with_sq(cert: Cert, sq: &Sq) -> Self { |
| 144 |
// Try to be more helpful by including a User ID in the |
| 145 |
// listing. We'd like it to be the primary one. Use |
| 146 |
// decreasingly strict policies. |
| 147 |
let mut primary_uid: Option<Vec<u8>> = None; |
| 148 |
|
| 149 |
// First, apply our policy. |
| 150 |
if let Ok(vcert) = cert.with_policy(sq.policy, None) { |
| 151 |
if let Ok(primary) = vcert.primary_userid() { |
| 152 |
primary_uid = Some(primary.userid().value().to_vec()); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
// Second, apply the null policy. |
| 157 |
if primary_uid.is_none() { |
| 158 |
let null = unsafe { openpgp::policy::NullPolicy::new() }; |
| 159 |
if let Ok(vcert) = cert.with_policy(&null, None) { |
| 160 |
if let Ok(primary) = vcert.primary_userid() { |
| 161 |
primary_uid = Some(primary.userid().value().to_vec()); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
// As a last resort, pick the first user id. |
| 167 |
if primary_uid.is_none() { |
| 168 |
if let Some(primary) = cert.userids().next() { |
| 169 |
primary_uid = Some(primary.userid().value().to_vec()); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
// List all user ids independently of their validity. |
| 174 |
let mut userids = vec![]; |
| 175 |
for u in cert.userids() { |
| 176 |
if primary_uid.as_ref() |
| 177 |
.map(|p| &p[..] == u.userid().value()).unwrap_or(false) |
| 178 |
{ |
| 179 |
// Skip the user id we already handled. |
| 180 |
continue; |
| 181 |
} |
| 182 |
|
| 183 |
userids.push(Self::userid(u.userid().value())); |
| 184 |
} |
| 185 |
|
| 186 |
Self { |
| 187 |
fingerprint: format!("{:X}", cert.fingerprint()), |
| 188 |
primary_userid: primary_uid.map(|id| Self::userid(&id)), |
| 189 |
userids, |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
fn userid(bytes: &[u8]) -> String { |
| 194 |
ui::Safe(bytes).to_string() |
| 195 |
} |
| 196 |
} |
| 197 |
} |