22#include < sourcemeta/core/jsonschema.h>
33
44#include < fstream> // std::ofstream
5- #include < iostream> // std::cerr
5+ #include < iostream> // std::cerr, std::cout
66#include < sstream> // std::ostringstream
7+ #include < utility> // std::move
8+ #include < vector> // std::vector
79
810#include " command.h"
911#include " error.h"
1416
1517auto sourcemeta::jsonschema::fmt (const sourcemeta::core::Options &options)
1618 -> void {
19+ const bool output_json{options.contains (" json" )};
20+ bool result{true };
21+ std::vector<std::string> failed_files;
1722 const auto indentation{parse_indentation (options)};
1823 for (const auto &entry : for_each_json (options)) {
1924 if (entry.first .extension () == " .yaml" ||
@@ -51,13 +56,13 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
5156
5257 if (options.contains (" check" )) {
5358 if (current.str () == expected.str ()) {
54- LOG_VERBOSE (options) << " PASS: " << entry.first .string () << " \n " ;
59+ LOG_VERBOSE (options) << " ok: " << entry.first .string () << " \n " ;
60+ } else if (output_json) {
61+ failed_files.push_back (entry.first .string ());
62+ result = false ;
5563 } else {
56- std::cerr << " FAIL: " << entry.first .string () << " \n " ;
57- std::cerr << " Got:\n "
58- << current.str () << " \n But expected:\n "
59- << expected.str () << " \n " ;
60- throw Fail{EXIT_FAILURE};
64+ std::cerr << " fail: " << entry.first .string () << " \n " ;
65+ result = false ;
6166 }
6267 } else {
6368 if (current.str () != expected.str ()) {
@@ -66,4 +71,31 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
6671 }
6772 }
6873 }
74+
75+ if (options.contains (" check" ) && output_json) {
76+ auto output_json_object{sourcemeta::core::JSON::make_object ()};
77+ output_json_object.assign (" valid" , sourcemeta::core::JSON{result});
78+
79+ if (!result) {
80+ auto errors_array{sourcemeta::core::JSON::make_array ()};
81+ for (auto &file_path : failed_files) {
82+ errors_array.push_back (sourcemeta::core::JSON{std::move (file_path)});
83+ }
84+
85+ output_json_object.assign (" errors" , sourcemeta::core::JSON{errors_array});
86+ }
87+
88+ sourcemeta::core::prettify (output_json_object, std::cout, indentation);
89+ std::cout << " \n " ;
90+ }
91+
92+ if (!result) {
93+ if (!output_json) {
94+ std::cerr << " \n Run the `fmt` command without `--check/-c` to fix the "
95+ " formatting"
96+ << " \n " ;
97+ }
98+
99+ throw Fail{EXIT_FAILURE};
100+ }
69101}
0 commit comments