Skip to content

Commit 55dd2a9

Browse files
committed
feat: Prepare cli config for column magic
Extend the cli config to include column constraints for improving the output. For now they are not used.
1 parent 23c49e4 commit 55dd2a9

File tree

3 files changed

+350
-27
lines changed

3 files changed

+350
-27
lines changed

openstack_cli/.config/config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ views:
2626
dns.zone/recordset:
2727
fields: [id, name, description, records, status, type, zone_name, created_at, updated_at]
2828
dns.zone:
29-
fields: [id, name, descripion, email, shared, status, ttl, type, created_at, updated_at]
29+
fields: [id, name, description, email, shared, status, ttl, type, created_at, updated_at]
3030
# identity
3131
identity.group:
3232
fields: [id, name, domain_id, description]

openstack_cli/src/config.rs

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ use std::{
3636
fmt,
3737
path::{Path, PathBuf},
3838
};
39-
use structable::OutputConfig;
4039
use thiserror::Error;
4140
use tracing::error;
4241

@@ -88,14 +87,43 @@ pub enum ConfigFileBuilderError {
8887
path: PathBuf,
8988
},
9089
}
90+
///
91+
/// Output configuration
92+
///
93+
/// This structure is controlling how the table table is being built for a structure.
94+
#[derive(Clone, Debug, Default, Deserialize)]
95+
pub struct ViewConfig {
96+
/// Limit fields (their titles) to be returned
97+
#[serde(default)]
98+
pub fields: Vec<FieldConfig>,
99+
}
100+
101+
/// Field output configuration
102+
#[derive(Clone, Debug, Deserialize, Eq, Ord, PartialOrd, PartialEq)]
103+
#[serde(untagged)]
104+
pub enum FieldConfig {
105+
/// Simple - only attribute name as string
106+
Simple(String),
107+
/// Extended - object with individual properties of the output column
108+
Extended {
109+
/// Attribute name
110+
name: String,
111+
/// Min length of the column
112+
#[serde(default)]
113+
min_len: Option<usize>,
114+
/// Max length of the column
115+
#[serde(default)]
116+
max_len: Option<usize>,
117+
},
118+
}
91119

92120
/// OpenStackClient configuration
93121
#[derive(Clone, Debug, Default, Deserialize)]
94122
pub struct Config {
95123
/// Map of views with the key being the resource key `<SERVICE_TYPE>.<RESOURCE>[/<SUBRESOURCE>]`)
96124
/// and the value being an `[OutputConfig]`
97125
#[serde(default)]
98-
pub views: HashMap<String, OutputConfig>,
126+
pub views: HashMap<String, ViewConfig>,
99127
}
100128

101129
/// A builder to create a [`ConfigFile`] by specifying which files to load.
@@ -224,3 +252,36 @@ impl fmt::Display for Config {
224252
write!(f, "")
225253
}
226254
}
255+
256+
#[cfg(test)]
257+
mod tests {
258+
use super::*;
259+
use std::io::Write;
260+
use tempfile::Builder;
261+
262+
#[test]
263+
fn test_parse_config() {
264+
let mut config_file = Builder::new().suffix(".yaml").tempfile().unwrap();
265+
266+
const CONFIG_DATA: &str = r#"
267+
views:
268+
foo:
269+
fields: ["a", "b", "c"]
270+
bar:
271+
fields:
272+
- "a"
273+
- name: "b"
274+
min_len: 1
275+
- "c"
276+
"#;
277+
278+
write!(config_file, "{}", CONFIG_DATA).unwrap();
279+
280+
let _cfg = ConfigFileBuilder {
281+
sources: Vec::new(),
282+
}
283+
.add_source(config_file.path())
284+
.unwrap()
285+
.build();
286+
}
287+
}

0 commit comments

Comments
 (0)