5
5
6
6
use clap:: { Parser , Subcommand } ;
7
7
use color_eyre:: Result ;
8
+ use comfy_table:: { presets:: UTF8_FULL , Table } ;
9
+ use serde_json;
8
10
9
11
use super :: base_disks:: { list_base_disks, prune_base_disks} ;
12
+ use super :: OutputFormat ;
10
13
11
14
/// Options for base-disks command
12
15
#[ derive( Debug , Parser ) ]
@@ -19,11 +22,19 @@ pub struct LibvirtBaseDisksOpts {
19
22
#[ derive( Debug , Subcommand ) ]
20
23
pub enum BaseDisksSubcommand {
21
24
/// List all base disk images
22
- List ,
25
+ List ( ListOpts ) ,
23
26
/// Prune unreferenced base disk images
24
27
Prune ( PruneOpts ) ,
25
28
}
26
29
30
+ /// Options for list command
31
+ #[ derive( Debug , Parser ) ]
32
+ pub struct ListOpts {
33
+ /// Output format
34
+ #[ clap( long, value_enum, default_value_t = OutputFormat :: Table ) ]
35
+ pub format : OutputFormat ,
36
+ }
37
+
27
38
/// Options for prune command
28
39
#[ derive( Debug , Parser ) ]
29
40
pub struct PruneOpts {
@@ -37,59 +48,69 @@ pub fn run(global_opts: &crate::libvirt::LibvirtOptions, opts: LibvirtBaseDisksO
37
48
let connect_uri = global_opts. connect . as_ref ( ) ;
38
49
39
50
match opts. command {
40
- BaseDisksSubcommand :: List => run_list ( connect_uri) ,
51
+ BaseDisksSubcommand :: List ( list_opts ) => run_list ( connect_uri, list_opts ) ,
41
52
BaseDisksSubcommand :: Prune ( prune_opts) => run_prune ( connect_uri, prune_opts) ,
42
53
}
43
54
}
44
55
45
56
/// Execute the list subcommand
46
- fn run_list ( connect_uri : Option < & String > ) -> Result < ( ) > {
57
+ fn run_list ( connect_uri : Option < & String > , opts : ListOpts ) -> Result < ( ) > {
47
58
let base_disks = list_base_disks ( connect_uri) ?;
48
59
49
- if base_disks. is_empty ( ) {
50
- println ! ( "No base disk images found" ) ;
51
- return Ok ( ( ) ) ;
60
+ match opts. format {
61
+ OutputFormat :: Table => {
62
+ if base_disks. is_empty ( ) {
63
+ println ! ( "No base disk images found" ) ;
64
+ return Ok ( ( ) ) ;
65
+ }
66
+
67
+ let mut table = Table :: new ( ) ;
68
+ table. load_preset ( UTF8_FULL ) ;
69
+ table. set_header ( vec ! [ "NAME" , "SIZE" , "REFS" , "IMAGE DIGEST" ] ) ;
70
+
71
+ for disk in & base_disks {
72
+ let name = disk. path . file_name ( ) . unwrap_or ( "unknown" ) ;
73
+
74
+ let size = disk
75
+ . size
76
+ . map ( |bytes| indicatif:: BinaryBytes ( bytes) . to_string ( ) )
77
+ . unwrap_or_else ( || "unknown" . to_string ( ) ) ;
78
+
79
+ let refs = disk. ref_count . to_string ( ) ;
80
+
81
+ let digest = disk
82
+ . image_digest
83
+ . as_ref ( )
84
+ . map ( |d| {
85
+ // Truncate long digests for display
86
+ if d. len ( ) > 56 {
87
+ format ! ( "{}..." , & d[ ..53 ] )
88
+ } else {
89
+ d. clone ( )
90
+ }
91
+ } )
92
+ . unwrap_or_else ( || "<no metadata>" . to_string ( ) ) ;
93
+
94
+ table. add_row ( vec ! [ name, & size, & refs, & digest] ) ;
95
+ }
96
+
97
+ println ! ( "{}" , table) ;
98
+ println ! (
99
+ "\n Found {} base disk{}" ,
100
+ base_disks. len( ) ,
101
+ if base_disks. len( ) == 1 { "" } else { "s" }
102
+ ) ;
103
+ }
104
+ OutputFormat :: Json => {
105
+ println ! ( "{}" , serde_json:: to_string_pretty( & base_disks) ?) ;
106
+ }
107
+ OutputFormat :: Yaml => {
108
+ return Err ( color_eyre:: eyre:: eyre!(
109
+ "YAML format is not supported for base-disks list command"
110
+ ) )
111
+ }
52
112
}
53
113
54
- // Print table header
55
- println ! (
56
- "{:<40} {:<10} {:<10} {:<58}" ,
57
- "NAME" , "SIZE" , "REFS" , "IMAGE DIGEST"
58
- ) ;
59
- println ! ( "{}" , "=" . repeat( 118 ) ) ;
60
-
61
- for disk in & base_disks {
62
- let name = disk. path . file_name ( ) . unwrap_or ( "unknown" ) ;
63
-
64
- let size = disk
65
- . size
66
- . map ( |bytes| indicatif:: BinaryBytes ( bytes) . to_string ( ) )
67
- . unwrap_or_else ( || "unknown" . to_string ( ) ) ;
68
-
69
- let refs = disk. ref_count . to_string ( ) ;
70
-
71
- let digest = disk
72
- . image_digest
73
- . as_ref ( )
74
- . map ( |d| {
75
- // Truncate long digests for display
76
- if d. len ( ) > 56 {
77
- format ! ( "{}..." , & d[ ..53 ] )
78
- } else {
79
- d. clone ( )
80
- }
81
- } )
82
- . unwrap_or_else ( || "<no metadata>" . to_string ( ) ) ;
83
-
84
- println ! ( "{:<40} {:<10} {:<10} {:<58}" , name, size, refs, digest) ;
85
- }
86
-
87
- println ! (
88
- "\n Found {} base disk{}" ,
89
- base_disks. len( ) ,
90
- if base_disks. len( ) == 1 { "" } else { "s" }
91
- ) ;
92
-
93
114
Ok ( ( ) )
94
115
}
95
116
0 commit comments