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