Skip to content

Commit 9731855

Browse files
committed
Allow reformatting of multiple files
When using -i or --test. See google#210.
1 parent 12d0d1f commit 9731855

File tree

1 file changed

+75
-32
lines changed

1 file changed

+75
-32
lines changed

cmd/jsonnet.cpp

Lines changed: 75 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ void usage(std::ostream &o)
110110
o << " -h / --help This message\n";
111111
o << " -e / --exec Treat filename as code\n";
112112
o << " -o / --output-file <file> Write to the output file rather than stdout\n";
113-
o << " -i / --in-place Update the Jsonnet file in place. Same as -o <filename>\n";
113+
o << " -i / --in-place Update the Jsonnet file(s) in place. Same as -o <filename>\n";
114114
o << " --test Exit with failure if reformatting changed the file.\n";
115115
o << " -n / --indent <n> Number of spaces to indent by (default 0, means no change)\n";
116116
o << " --max-blank-lines <n> Max vertical spacing, 0 means no change (default 2)\n";
@@ -121,6 +121,7 @@ void usage(std::ostream &o)
121121
o << " --[no-]pad-objects { x: 1, x: 2 } instead of {x: 1, y: 2} (on by default)\n";
122122
o << " --debug-desugaring Unparse the desugared AST without executing it\n";
123123
o << " --version Print version\n";
124+
o << "Note: Multiple filenames can be provided at once when using -i or --test options.\n";
124125
o << "\n";
125126
o << "In all cases:\n";
126127
o << "<filename> can be - (stdin)\n";
@@ -153,6 +154,7 @@ enum Command {
153154
struct JsonnetConfig {
154155
Command cmd;
155156
std::string inputFile;
157+
std::vector<std::string> inputFiles;
156158
std::string outputFile;
157159
bool filenameIsCode;
158160

@@ -455,15 +457,19 @@ static bool process_args(int argc,
455457
return false;
456458
}
457459

458-
std::string filename = remaining_args[0];
459-
if (remaining_args.size() > 1) {
460-
std::cerr << "ERROR: Already specified " << want
461-
<< " as \"" << filename << "\"\n"
462-
<< std::endl;
463-
usage(std::cerr);
464-
return false;
460+
if (config->cmd == FMT && (config->fmtTest || config->fmtInPlace)) {
461+
config->inputFiles = remaining_args;
462+
} else {
463+
std::string filename = remaining_args[0];
464+
if (remaining_args.size() > 1) {
465+
std::cerr << "ERROR: Already specified " << want
466+
<< " as \"" << filename << "\"\n"
467+
<< std::endl;
468+
usage(std::cerr);
469+
return false;
470+
}
471+
config->inputFile = filename;
465472
}
466-
config->inputFile = filename;
467473
return true;
468474
}
469475

@@ -618,18 +624,18 @@ int main(int argc, const char **argv)
618624
return EXIT_FAILURE;
619625
}
620626

621-
// Read input files.
622-
std::string input;
623-
if (!read_input(&config, &input)) {
624-
jsonnet_destroy(vm);
625-
return EXIT_FAILURE;
626-
}
627-
628627
// Evaluate input Jsonnet and handle any errors from Jsonnet VM.
629628
int error;
630629
char *output;
631630
switch (config.cmd) {
632631
case EVAL: {
632+
// Read input files.
633+
std::string input;
634+
if (!read_input(&config, &input)) {
635+
jsonnet_destroy(vm);
636+
return EXIT_FAILURE;
637+
}
638+
633639
if (config.evalMulti) {
634640
output = jsonnet_evaluate_snippet_multi(
635641
vm, config.inputFile.c_str(), input.c_str(), &error);
@@ -684,27 +690,64 @@ int main(int argc, const char **argv)
684690
jsonnet_destroy(vm);
685691
return EXIT_FAILURE;
686692
}
687-
output_file = config.inputFile;
688693
}
689694

690-
output = jsonnet_fmt_snippet(vm, config.inputFile.c_str(), input.c_str(), &error);
695+
if (config.fmtInPlace || config.fmtTest) {
696+
for (std::string inputFile: config.inputFiles) {
697+
if (config.fmtInPlace) {
698+
output_file = inputFile;
699+
}
700+
config.inputFile = inputFile;
701+
std::string input;
702+
if (!read_input(&config, &input)) {
703+
jsonnet_destroy(vm);
704+
return EXIT_FAILURE;
705+
}
706+
707+
output = jsonnet_fmt_snippet(vm, config.inputFile.c_str(), input.c_str(), &error);
708+
709+
if (error) {
710+
std::cerr << output;
711+
std::cerr.flush();
712+
jsonnet_realloc(vm, output, 0);
713+
jsonnet_destroy(vm);
714+
return EXIT_FAILURE;
715+
}
716+
717+
if (config.fmtTest) {
718+
// Check the output matches the input.
719+
bool ok = output == input;
720+
jsonnet_realloc(vm, output, 0);
721+
jsonnet_destroy(vm);
722+
return ok ? EXIT_SUCCESS : 2;
723+
} else {
724+
// Write output Jsonnet.
725+
bool successful = write_output_file(output, output_file);
726+
jsonnet_realloc(vm, output, 0);
727+
if (!successful) {
728+
jsonnet_destroy(vm);
729+
return EXIT_FAILURE;
730+
}
731+
}
732+
}
733+
} else {
734+
// Read input files.
735+
std::string input;
736+
if (!read_input(&config, &input)) {
737+
jsonnet_destroy(vm);
738+
return EXIT_FAILURE;
739+
}
691740

692-
if (error) {
693-
std::cerr << output;
694-
std::cerr.flush();
695-
jsonnet_realloc(vm, output, 0);
696-
jsonnet_destroy(vm);
697-
return EXIT_FAILURE;
698-
}
741+
output = jsonnet_fmt_snippet(vm, config.inputFile.c_str(), input.c_str(), &error);
699742

700-
if (config.fmtTest) {
701-
// Check the output matches the input.
702-
bool ok = output == input;
703-
jsonnet_realloc(vm, output, 0);
704-
jsonnet_destroy(vm);
705-
return ok ? EXIT_SUCCESS : 2;
743+
if (error) {
744+
std::cerr << output;
745+
std::cerr.flush();
746+
jsonnet_realloc(vm, output, 0);
747+
jsonnet_destroy(vm);
748+
return EXIT_FAILURE;
749+
}
706750

707-
} else {
708751
// Write output Jsonnet.
709752
bool successful = write_output_file(output, output_file);
710753
jsonnet_realloc(vm, output, 0);

0 commit comments

Comments
 (0)