11#include < sourcemeta/core/json.h>
22#include < sourcemeta/core/jsonschema.h>
33
4- #include < fstream> // std::ofstream
5- #include < iostream> // std::cerr
6- #include < sstream> // std::ostringstream
4+ #include < filesystem> // std::filesystem::path
5+ #include < fstream> // std::ofstream
6+ #include < functional> // std::reference_wrapper, std::cref
7+ #include < iostream> // std::cerr, std::cout
8+ #include < sstream> // std::ostringstream
9+ #include < vector> // std::vector
710
811#include " command.h"
912#include " error.h"
1417
1518auto sourcemeta::jsonschema::fmt (const sourcemeta::core::Options &options)
1619 -> void {
20+ const bool output_json{options.contains (" json" )};
21+ bool result{true };
22+ std::vector<std::reference_wrapper<const std::filesystem::path>> failed_files;
1723 const auto indentation{parse_indentation (options)};
1824 for (const auto &entry : for_each_json (options)) {
1925 if (entry.first .extension () == " .yaml" ||
@@ -51,13 +57,13 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
5157
5258 if (options.contains (" check" )) {
5359 if (current.str () == expected.str ()) {
54- LOG_VERBOSE (options) << " PASS : " << entry.first .string () << " \n " ;
60+ LOG_VERBOSE (options) << " ok : " << entry.first .string () << " \n " ;
5561 } 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} ;
62+ if (!output_json) {
63+ std::cerr << " fail: " << entry. first . string () << " \n " ;
64+ }
65+ failed_files. push_back ( std::cref (entry. first )) ;
66+ result = false ;
6167 }
6268 } else {
6369 if (current.str () != expected.str ()) {
@@ -66,4 +72,31 @@ auto sourcemeta::jsonschema::fmt(const sourcemeta::core::Options &options)
6672 }
6773 }
6874 }
75+
76+ if (options.contains (" check" ) && output_json) {
77+ auto output_json_object{sourcemeta::core::JSON::make_object ()};
78+ output_json_object.assign (" valid" , sourcemeta::core::JSON{result});
79+
80+ if (!result) {
81+ auto errors_array{sourcemeta::core::JSON::make_array ()};
82+ for (const auto &file_path : failed_files) {
83+ errors_array.push_back (sourcemeta::core::JSON{file_path.get ().string ()});
84+ }
85+
86+ output_json_object.assign (" errors" , sourcemeta::core::JSON{errors_array});
87+ }
88+
89+ sourcemeta::core::prettify (output_json_object, std::cout, indentation);
90+ std::cout << " \n " ;
91+ }
92+
93+ if (!result) {
94+ if (!output_json) {
95+ std::cerr << " \n Run the `fmt` command without `--check/-c` to fix the "
96+ " formatting"
97+ << " \n " ;
98+ }
99+
100+ throw Fail{EXIT_FAILURE};
101+ }
69102}
0 commit comments