]>
Commit | Line | Data |
---|---|---|
f53ad23a DM |
1 | package PVE::CLIFormatter; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | use JSON; | |
6 | ||
7 | sub data_to_text { | |
8 | my ($data) = @_; | |
9 | ||
10 | return '' if !defined($data); | |
11 | ||
12 | if (my $class = ref($data)) { | |
13 | return to_json($data, { utf8 => 1, canonical => 1 }); | |
14 | } else { | |
15 | return "$data"; | |
16 | } | |
17 | } | |
18 | ||
19 | # prints a formatted table with a title row. | |
20 | # $data - the data to print (array of objects) | |
21 | # $returnprops -json schema property description | |
22 | # $props_to_print - ordered list of properties to print | |
23 | # $sort_key can be used to sort after a column, if it isn't set we sort | |
24 | # after the leftmost column (with no undef value in $data) this can be | |
25 | # turned off by passing 0 as $sort_key | |
793ad69b | 26 | # $border - print with/without table header and asciiart border |
f53ad23a | 27 | sub print_text_table { |
793ad69b | 28 | my ($data, $returnprops, $props_to_print, $sort_key, $border) = @_; |
f53ad23a DM |
29 | |
30 | my $autosort = 1; | |
31 | if (defined($sort_key) && $sort_key eq 0) { | |
32 | $autosort = 0; | |
33 | $sort_key = undef; | |
34 | } | |
35 | ||
36 | my $colopts = {}; | |
793ad69b DM |
37 | |
38 | my $borderstring = ''; | |
f53ad23a DM |
39 | my $formatstring = ''; |
40 | ||
41 | my $column_count = scalar(@$props_to_print); | |
42 | ||
43 | for (my $i = 0; $i < $column_count; $i++) { | |
44 | my $prop = $props_to_print->[$i]; | |
45 | my $propinfo = $returnprops->{$prop} // {}; | |
46 | ||
47 | my $title = $propinfo->{title} // $prop; | |
48 | my $cutoff = $propinfo->{print_width} // $propinfo->{maxLength}; | |
49 | ||
50 | # calculate maximal print width and cutoff | |
51 | my $titlelen = length($title); | |
52 | ||
53 | my $longest = $titlelen; | |
54 | my $sortable = $autosort; | |
55 | foreach my $entry (@$data) { | |
56 | my $len = length(data_to_text($entry->{$prop})) // 0; | |
57 | $longest = $len if $len > $longest; | |
58 | $sortable = 0 if !defined($entry->{$prop}); | |
59 | } | |
60 | $cutoff = $longest if !defined($cutoff) || $cutoff > $longest; | |
61 | $sort_key //= $prop if $sortable; | |
62 | ||
63 | $colopts->{$prop} = { | |
64 | title => $title, | |
65 | default => $propinfo->{default} // '', | |
66 | cutoff => $cutoff, | |
67 | }; | |
68 | ||
793ad69b DM |
69 | if ($border) { |
70 | if ($i == ($column_count - 1)) { | |
71 | $formatstring .= "| %-${cutoff}s |\n"; | |
72 | $borderstring .= "+-" . ('-' x $cutoff) . "-+\n"; | |
73 | } else { | |
74 | $formatstring .= "| %-${cutoff}s "; | |
75 | $borderstring .= "+-" . ('-' x $cutoff) . '-'; | |
76 | } | |
77 | } else { | |
78 | # skip alignment and cutoff on last column | |
79 | $formatstring .= ($i == ($column_count - 1)) ? "%s\n" : "%-${cutoff}s "; | |
80 | } | |
f53ad23a DM |
81 | } |
82 | ||
f53ad23a DM |
83 | if (defined($sort_key)) { |
84 | my $type = $returnprops->{$sort_key}->{type} // 'string'; | |
85 | if ($type eq 'integer' || $type eq 'number') { | |
86 | @$data = sort { $a->{$sort_key} <=> $b->{$sort_key} } @$data; | |
87 | } else { | |
88 | @$data = sort { $a->{$sort_key} cmp $b->{$sort_key} } @$data; | |
89 | } | |
90 | } | |
91 | ||
793ad69b DM |
92 | print $borderstring if $border; |
93 | printf $formatstring, map { $colopts->{$_}->{title} } @$props_to_print; | |
94 | ||
f53ad23a | 95 | foreach my $entry (@$data) { |
793ad69b | 96 | print $borderstring if $border; |
f53ad23a DM |
97 | printf $formatstring, map { |
98 | substr(data_to_text($entry->{$_}) // $colopts->{$_}->{default}, | |
99 | 0, $colopts->{$_}->{cutoff}); | |
100 | } @$props_to_print; | |
101 | } | |
793ad69b | 102 | print $borderstring if $border; |
f53ad23a DM |
103 | } |
104 | ||
105 | # prints the result of an API GET call returning an array as a table. | |
106 | # takes formatting information from the results property of the call | |
107 | # if $props_to_print is provided, prints only those columns. otherwise | |
108 | # takes all fields of the results property, with a fallback | |
109 | # to all fields occuring in items of $data. | |
110 | sub print_api_list { | |
793ad69b | 111 | my ($data, $result_schema, $props_to_print, $sort_key, $border) = @_; |
f53ad23a DM |
112 | |
113 | die "can only print object lists\n" | |
114 | if !($result_schema->{type} eq 'array' && $result_schema->{items}->{type} eq 'object'); | |
115 | ||
116 | my $returnprops = $result_schema->{items}->{properties}; | |
117 | ||
118 | if (!defined($props_to_print)) { | |
119 | $props_to_print = [ sort keys %$returnprops ]; | |
120 | if (!scalar(@$props_to_print)) { | |
121 | my $all_props = {}; | |
122 | foreach my $obj (@{$data}) { | |
123 | foreach my $key (keys %{$obj}) { | |
124 | $all_props->{ $key } = 1; | |
125 | } | |
126 | } | |
127 | $props_to_print = [ sort keys %{$all_props} ]; | |
128 | } | |
129 | die "unable to detect list properties\n" if !scalar(@$props_to_print); | |
130 | } | |
131 | ||
793ad69b | 132 | print_text_table($data, $returnprops, $props_to_print, $sort_key, $border); |
f53ad23a DM |
133 | } |
134 | ||
135 | sub print_api_result { | |
136 | my ($format, $data, $result_schema, $props_to_print, $sort_key) = @_; | |
137 | ||
138 | return if $result_schema->{type} eq 'null'; | |
139 | ||
140 | if ($format eq 'json') { | |
141 | print to_json($data, {utf8 => 1, allow_nonref => 1, canonical => 1, pretty => 1 }); | |
793ad69b | 142 | } elsif ($format eq 'text' || $format eq 'plain') { |
f53ad23a DM |
143 | my $type = $result_schema->{type}; |
144 | if ($type eq 'object') { | |
145 | $props_to_print = [ sort keys %$data ] if !defined($props_to_print); | |
2a174d2a | 146 | my $kvstore = []; |
f53ad23a | 147 | foreach my $key (@$props_to_print) { |
2a174d2a | 148 | push @$kvstore, { key => $key, value => data_to_text($data->{$key}) }; |
f53ad23a | 149 | } |
2a174d2a DM |
150 | my $schema = { type => 'array', items => { type => 'object' }}; |
151 | print_api_list($kvstore, $schema, ['key', 'value'], 0, $format eq 'text'); | |
f53ad23a DM |
152 | } elsif ($type eq 'array') { |
153 | return if !scalar(@$data); | |
154 | my $item_type = $result_schema->{items}->{type}; | |
155 | if ($item_type eq 'object') { | |
793ad69b | 156 | print_api_list($data, $result_schema, $props_to_print, $sort_key, $format eq 'text'); |
f53ad23a DM |
157 | } else { |
158 | foreach my $entry (@$data) { | |
159 | print data_to_text($entry) . "\n"; | |
160 | } | |
161 | } | |
162 | } else { | |
163 | print "$data\n"; | |
164 | } | |
165 | } else { | |
166 | die "internal error: unknown output format"; # should not happen | |
167 | } | |
168 | } | |
169 | ||
170 | 1; |