11package s10k .tool .nodes .meta .cmd ;
22
3+ import static com .github .freva .asciitable .HorizontalAlign .LEFT ;
4+ import static com .github .freva .asciitable .HorizontalAlign .RIGHT ;
35import static net .solarnetwork .util .StringUtils .commaDelimitedStringFromCollection ;
46import static s10k .tool .common .util .RestUtils .checkSuccess ;
5- import static s10k .tool .common .util .TableUtils .basicTable ;
67
8+ import java .io .IOException ;
79import java .util .ArrayList ;
810import java .util .Arrays ;
9- import java .util .LinkedHashMap ;
1011import java .util .List ;
11- import java .util .Map ;
1212import java .util .concurrent .Callable ;
1313
1414import org .springframework .http .MediaType ;
2020import com .fasterxml .jackson .databind .JsonNode ;
2121import com .fasterxml .jackson .databind .ObjectMapper ;
2222import com .fasterxml .jackson .databind .ObjectWriter ;
23+ import com .github .freva .asciitable .Column ;
2324
24- import net .solarnetwork .domain .datum .GeneralDatumMetadata ;
2525import picocli .CommandLine .Command ;
2626import picocli .CommandLine .Option ;
2727import s10k .tool .common .cmd .BaseSubCmd ;
28+ import s10k .tool .common .domain .ResultDisplayMode ;
29+ import s10k .tool .common .util .TableUtils ;
2830import s10k .tool .nodes .domain .NodeMetadata ;
2931
3032/**
3436@ Command (name = "list" )
3537public class ListNodeMetadataCmd extends BaseSubCmd <NodeMetadataCmd > implements Callable <Integer > {
3638
37- @ Option (names = { "-node" ,
38- "--node-id" }, description = "a node ID to return metadata for" , split = "\\ s*,\\ s*" , splitSynopsisLabel = "," , paramLabel = "nodeId" , required = true )
39+ // @formatter:off
40+
41+ @ Option (names = { "-node" , "--node-id" },
42+ description = "a node ID to return metadata for" ,
43+ split = "\\ s*,\\ s*" , splitSynopsisLabel = "," ,
44+ paramLabel = "nodeId" ,
45+ required = true )
3946 Long [] nodeIds ;
4047
41- @ Option (names = { "-filter" , "--filter" }, description = "a metadata filter" )
48+ @ Option (names = { "-filter" , "--filter" },
49+ description = "a metadata filter" )
4250 String filter ;
4351
52+ @ Option (names = { "-mode" , "--display-mode" },
53+ description = "how to display the CSV data" ,
54+ defaultValue = "PRETTY" )
55+ ResultDisplayMode displayMode = ResultDisplayMode .PRETTY ;
56+
57+ // @formatter:on
58+
4459 /**
4560 * Constructor.
4661 *
@@ -63,36 +78,46 @@ public Integer call() throws Exception {
6378 System .out .println ("No metadata matched your criteria." );
6479 return 0 ;
6580 }
66- boolean multi = false ;
67- for (NodeMetadata meta : metas ) {
68- if (multi ) {
69- System .out .println ("" );
70- } else {
71- multi = true ;
72- }
73- Map <String , Object > tableData = new LinkedHashMap <String , Object >(4 );
74- tableData .put ("nodeId" , meta .nodeId ());
75- if (meta .created () != null ) {
76- tableData .put ("created" , meta .created ());
77- }
78- if (meta .updated () != null ) {
79- tableData .put ("updated" , meta .updated ());
80- }
8181
82- GeneralDatumMetadata gdm = meta .metadata ();
82+ List <?> tableData = (displayMode == ResultDisplayMode .JSON
83+ ? metas .stream ().map (NodeMetadata ::metadata ).toList ()
84+ : metas .stream ().map (m -> metadataRow (m , pretty )).toList ());
85+ // @formatter:off
86+ TableUtils .renderTableData (new Column [] {
87+ new Column ().header ("Node ID" ).dataAlign (RIGHT ),
88+ new Column ().header ("Updated" ).dataAlign (LEFT ),
89+ new Column ().header ("Metadata" ).dataAlign (LEFT ),
90+ }, tableData , displayMode , objectMapper , TableUtils .TableDataJsonPrettyPrinter .INSTANCE , System .out );
91+ // @formatter:on
8392
84- System .out .print (basicTable (tableData , "Property" , "Value" , false ));
85- if (gdm != null ) {
86- System .out .println (pretty .writeValueAsString (gdm ));
87- }
88- }
8993 return 0 ;
9094 } catch (Exception e ) {
9195 System .err .println ("Error listing node metadata: %s" .formatted (e .getMessage ()));
9296 }
9397 return 1 ;
9498 }
9599
100+ /**
101+ * Convert datum stream metadata into a tabular structure.
102+ *
103+ * @param m the metadata to convert
104+ * @param jsonWriter the writer to use for JSON metadata
105+ * @return the metadata data
106+ */
107+ public static Object [] metadataRow (NodeMetadata m , ObjectWriter jsonWriter ) {
108+ try {
109+ // @formatter:off
110+ return new Object [] {
111+ m .nodeId (),
112+ m .updated (),
113+ jsonWriter .writeValueAsString (m .metadata ()),
114+ };
115+ // @formatter:on
116+ } catch (IOException e ) {
117+ throw new IllegalStateException (e );
118+ }
119+ }
120+
96121 /**
97122 * List node metadata matching a search filter.
98123 *
0 commit comments