below_dump/btrfs.rs
1// Copyright (c) Facebook, Inc. and its affiliates.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use model::BtrfsModelFieldId;
16
17use super::*;
18
19pub struct Btrfs {
20 opts: GeneralOpt,
21 select: Option<BtrfsModelFieldId>,
22 fields: Vec<BtrfsField>,
23}
24
25impl Btrfs {
26 pub fn new(
27 opts: &GeneralOpt,
28 select: Option<BtrfsModelFieldId>,
29 fields: Vec<BtrfsField>,
30 ) -> Self {
31 Self {
32 opts: opts.to_owned(),
33 select,
34 fields,
35 }
36 }
37}
38
39impl Dumper for Btrfs {
40 fn dump_model(
41 &self,
42 ctx: &CommonFieldContext,
43 model: &model::Model,
44 output: &mut dyn Write,
45 round: &mut usize,
46 comma_flag: bool,
47 ) -> Result<IterExecResult> {
48 match model.system.btrfs.as_ref() {
49 Some(btrfs_items_ref) => {
50 let mut btrfs_items: Vec<_> = btrfs_items_ref
51 .iter()
52 .filter_map(|(_, model)| {
53 match (self.select.as_ref(), self.opts.filter.as_ref()) {
54 (Some(field_id), Some(filter))
55 if !filter.is_match(
56 &model
57 .query(field_id)
58 .map_or("?".to_owned(), |v| v.to_string()),
59 ) =>
60 {
61 None
62 }
63 _ => Some(model),
64 }
65 })
66 .collect();
67
68 if let Some(field_id) = &self.select {
69 if self.opts.sort {
70 model::sort_queriables(&mut btrfs_items, field_id, false);
71 }
72
73 if self.opts.rsort {
74 model::sort_queriables(&mut btrfs_items, field_id, true);
75 }
76
77 if (self.opts.sort || self.opts.rsort) && self.opts.top != 0 {
78 btrfs_items.truncate(self.opts.top as usize);
79 }
80 }
81 let mut json_output = json!([]);
82
83 btrfs_items
84 .into_iter()
85 .map(|model| {
86 match self.opts.output_format {
87 Some(OutputFormat::Raw) | None => write!(
88 output,
89 "{}",
90 print::dump_raw(
91 &self.fields,
92 ctx,
93 model,
94 *round,
95 self.opts.repeat_title,
96 self.opts.disable_title,
97 self.opts.raw
98 )
99 )?,
100 Some(OutputFormat::Csv) => write!(
101 output,
102 "{}",
103 print::dump_csv(
104 &self.fields,
105 ctx,
106 model,
107 *round,
108 self.opts.disable_title,
109 self.opts.raw
110 )
111 )?,
112 Some(OutputFormat::KeyVal) => write!(
113 output,
114 "{}",
115 print::dump_kv(&self.fields, ctx, model, self.opts.raw)
116 )?,
117 Some(OutputFormat::Json) => {
118 let par = print::dump_json(&self.fields, ctx, model, self.opts.raw);
119 json_output.as_array_mut().unwrap().push(par);
120 }
121 Some(OutputFormat::Tsv) => write!(
122 output,
123 "{}",
124 print::dump_tsv(
125 &self.fields,
126 ctx,
127 model,
128 *round,
129 self.opts.disable_title,
130 self.opts.raw
131 )
132 )?,
133 Some(OutputFormat::OpenMetrics) => write!(
134 output,
135 "{}",
136 print::dump_openmetrics(&self.fields, ctx, model)
137 )?,
138 }
139 *round += 1;
140 Ok(())
141 })
142 .collect::<Result<Vec<_>>>()?;
143
144 match (self.opts.output_format, comma_flag) {
145 (Some(OutputFormat::Json), true) => write!(output, ",{}", json_output)?,
146 (Some(OutputFormat::Json), false) => write!(output, "{}", json_output)?,
147 (Some(OutputFormat::OpenMetrics), _) => (),
148 _ => writeln!(output)?,
149 };
150
151 Ok(IterExecResult::Success)
152 }
153 None => Ok(IterExecResult::Skip),
154 }
155 }
156}