diff --git a/jsonnet.cpp b/jsonnet.cpp index c8cc34a26..9fa5e694b 100644 --- a/jsonnet.cpp +++ b/jsonnet.cpp @@ -176,6 +176,7 @@ void usage(std::ostream &o) o << " -E / --env Bring in an environment var as an 'external' var\n"; o << " --code-var = As --var but value is Jsonnet code\n"; o << " --code-env As --env but env var contains Jsonnet code\n"; + o << " -o / --output-file Write to the output file rather than stdout\n"; o << " -m / --multi Write multiple files, list files on stdout\n"; o << " -S / --string Expect a string, manifest as plain text\n"; o << " -s / --max-stack Number of allowed stack frames\n"; @@ -204,6 +205,31 @@ long strtol_check(const std::string &str) return r; } +// Writes the output JSON to the specified output file. +static bool write_output_file(const char* output, + const std::string &output_file) { + if (output_file.empty()) { + std::cout << output; + std::cout.flush(); + return true; + } + std::ofstream f; + f.open(output_file.c_str()); + if (!f.good()) { + std::string msg = "Writing to output file: " + output_file; + perror(msg.c_str()); + return false; + } + f << output; + f.close(); + if (!f.good()) { + std::string msg = "Writing to output file: " + output_file; + perror(msg.c_str()); + return false; + } + return true; +} + int main(int argc, const char **argv) { try { @@ -218,6 +244,7 @@ int main(int argc, const char **argv) auto args = simplify_args(argc, argv); std::vector remaining_args; + std::string output_file = ""; for (unsigned i=0 ; i