@@ -110,7 +110,7 @@ void usage(std::ostream &o)
110
110
o << " -h / --help This message\n " ;
111
111
o << " -e / --exec Treat filename as code\n " ;
112
112
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 " ;
114
114
o << " --test Exit with failure if reformatting changed the file.\n " ;
115
115
o << " -n / --indent <n> Number of spaces to indent by (default 0, means no change)\n " ;
116
116
o << " --max-blank-lines <n> Max vertical spacing, 0 means no change (default 2)\n " ;
@@ -121,6 +121,7 @@ void usage(std::ostream &o)
121
121
o << " --[no-]pad-objects { x: 1, x: 2 } instead of {x: 1, y: 2} (on by default)\n " ;
122
122
o << " --debug-desugaring Unparse the desugared AST without executing it\n " ;
123
123
o << " --version Print version\n " ;
124
+ o << " Note: Multiple filenames can be provided at once when using -i or --test options.\n " ;
124
125
o << " \n " ;
125
126
o << " In all cases:\n " ;
126
127
o << " <filename> can be - (stdin)\n " ;
@@ -153,6 +154,7 @@ enum Command {
153
154
struct JsonnetConfig {
154
155
Command cmd;
155
156
std::string inputFile;
157
+ std::vector<std::string> inputFiles;
156
158
std::string outputFile;
157
159
bool filenameIsCode;
158
160
@@ -455,15 +457,19 @@ static bool process_args(int argc,
455
457
return false ;
456
458
}
457
459
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;
465
472
}
466
- config->inputFile = filename;
467
473
return true ;
468
474
}
469
475
@@ -618,18 +624,18 @@ int main(int argc, const char **argv)
618
624
return EXIT_FAILURE;
619
625
}
620
626
621
- // Read input files.
622
- std::string input;
623
- if (!read_input (&config, &input)) {
624
- jsonnet_destroy (vm);
625
- return EXIT_FAILURE;
626
- }
627
-
628
627
// Evaluate input Jsonnet and handle any errors from Jsonnet VM.
629
628
int error;
630
629
char *output;
631
630
switch (config.cmd ) {
632
631
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
+
633
639
if (config.evalMulti ) {
634
640
output = jsonnet_evaluate_snippet_multi (
635
641
vm, config.inputFile .c_str (), input.c_str (), &error);
@@ -684,27 +690,64 @@ int main(int argc, const char **argv)
684
690
jsonnet_destroy (vm);
685
691
return EXIT_FAILURE;
686
692
}
687
- output_file = config.inputFile ;
688
693
}
689
694
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
+ }
691
740
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);
699
742
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
+ }
706
750
707
- } else {
708
751
// Write output Jsonnet.
709
752
bool successful = write_output_file (output, output_file);
710
753
jsonnet_realloc (vm, output, 0 );
0 commit comments