1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
#include "format.h"
#include "json.h"
#include "getpost.h"
#if defined(_MSC_VER)
#include <io.h>
#else
#include <unistd.h>
#endif
#include <cstdlib>
#include <fstream>
#include <map>
#include <sstream>
#include <string>
#if defined(_WIN32)
#include <windows.h>
static void erase_char( std::string &s, const char &c )
{
s.erase( std::remove( s.begin(), s.end(), c ), s.end() );
}
#endif
static bool enable_stdout_ansi_colors()
{
#if defined(_WIN32)
// enable ANSI colors on windows consoles https://superuser.com/a/1529908
DWORD dwMode;
GetConsoleMode( GetStdHandle( STD_OUTPUT_HANDLE ), &dwMode );
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
// may fail on Windows 10 earlier than 1511
return SetConsoleMode( GetStdHandle( STD_OUTPUT_HANDLE ), dwMode );
#else
return true;
#endif
}
int main( int argc, char *argv[] )
{
#if defined(_MSC_VER)
bool supports_color = _isatty( _fileno( stdout ) );
#else
bool supports_color = isatty( STDOUT_FILENO );
#endif
// formatter stdout in github actions is redirected but still able to handle ANSI colors
supports_color |= std::getenv( "CI" ) != nullptr;
// NOLINTNEXTLINE(cata-tests-must-restore-global-state)
json_error_output_colors = supports_color
? json_error_output_colors_t::ansi_escapes
: json_error_output_colors_t::no_colors;
if( !enable_stdout_ansi_colors() ) {
json_error_output_colors = json_error_output_colors_t::no_colors;
}
std::stringstream in;
std::stringstream out;
std::string filename;
std::string header;
char *gateway_var = getenv( "GATEWAY_INTERFACE" );
if( gateway_var == nullptr ) {
// Expect a single filename for now.
if( argc == 2 ) {
filename = argv[1];
} else if( argc != 1 ) {
std::cout << "Supply a filename to style or no arguments." << std::endl;
exit( EXIT_FAILURE );
}
if( filename.empty() ) {
in << std::cin.rdbuf();
} else {
std::ifstream fin( filename, std::ios::binary );
if( !fin.good() ) {
std::cout << "Failed to open " << filename << std::endl;
exit( EXIT_FAILURE );
}
in << fin.rdbuf();
fin.close();
}
} else {
std::map<std::string, std::string> params;
initializePost( params );
std::string data = params[ "data" ];
if( data.empty() ) {
exit( -255 );
}
in.str( data );
header = "Content-type: application/json\n\n";
}
if( in.str().empty() ) {
std::cout << "Error, " << ( filename.empty() ? "input" : filename ) << " is empty." << std::endl;
exit( EXIT_FAILURE );
}
JsonOut jsout( out, true );
TextJsonIn jsin( in, filename.empty() ? "<STDIN>" : filename );
try {
formatter::format( jsin, jsout );
} catch( const JsonError &e ) {
std::cout << e.what() << std::endl;
exit( EXIT_FAILURE );
}
out << std::endl;
if( filename.empty() ) {
std::cout << header;
std::cout << out.str();
} else {
std::string in_str = in.str();
#if defined(_WIN32)
erase_char( in_str, '\r' );
#endif
std::string color_bad = supports_color ? "\x1b[31m" : std::string();
std::string color_end = supports_color ? "\x1b[0m" : std::string();
if( in_str == out.str() ) {
exit( EXIT_SUCCESS );
} else {
std::ofstream fout( filename, std::ios::binary | std::ios::trunc );
fout << out.str();
fout.close();
std::cout << color_bad << "Has been linted : " << color_end << filename << std::endl;
std::cout << "Please read doc/JSON_STYLE.md" << std::endl;
exit( EXIT_FAILURE );
}
}
}
|