20
20
#include " version.hh"
21
21
22
22
#include < boost/algorithm/string/replace.hpp>
23
+ #include < boost/filesystem.hpp>
23
24
#include < boost/foreach.hpp>
24
25
#include < boost/program_options.hpp>
25
26
#include < boost/regex.hpp>
26
27
#include < boost/tokenizer.hpp>
27
28
28
29
#include < cctype>
30
+ #include < fstream>
29
31
#include < iostream>
30
32
31
33
typedef std::vector<std::string> TStringList;
@@ -243,6 +245,30 @@ bool DockerFileTransformer::transform(std::istream &in, std::ostream &out)
243
245
return !anyError;
244
246
}
245
247
248
+ bool transformInPlace (DockerFileTransformer &dft, const std::string &fileName)
249
+ {
250
+ using namespace boost ::filesystem;
251
+
252
+ // open input file and temporary output file
253
+ std::fstream fin (fileName, std::ios::in);
254
+ path tmpFileName = unique_path ();
255
+ std::fstream fout (tmpFileName.native (), std::ios::out);
256
+
257
+ // transform fin -> fout and close the streams
258
+ const bool ok = dft.transform (fin, fout);
259
+ fin.close ();
260
+ fout.close ();
261
+
262
+ if (ok)
263
+ // rewrite input file by the temporary file
264
+ rename (tmpFileName, fileName);
265
+ else
266
+ // something failed, drop the temporary file
267
+ remove (tmpFileName);
268
+
269
+ return ok;
270
+ }
271
+
246
272
int main (int argc, char *argv[])
247
273
{
248
274
// used also in diagnostic messages
@@ -251,10 +277,12 @@ int main(int argc, char *argv[])
251
277
namespace po = boost::program_options;
252
278
po::variables_map vm;
253
279
po::options_description desc (std::string (" Usage: " ) + prog_name
254
- + " [--verbose] cmd [arg1 [arg2 [...]]]" );
280
+ + " [--in-place Dockerfile] [-- verbose] cmd [arg1 [arg2 [...]]]" );
255
281
256
282
try {
257
283
desc.add_options ()
284
+ (" in-place,i" , po::value<std::string>(),
285
+ " modify the specified file in-place" )
258
286
(" verbose" , " print transformations to standard error output" );
259
287
260
288
desc.add_options ()
@@ -308,6 +336,10 @@ int main(int argc, char *argv[])
308
336
// pass cmd-line args to DockerFileTransformer
309
337
DockerFileTransformer dft (prefixCmd, verbose);
310
338
339
+ if (vm.count (" in-place" ))
340
+ // transform Dockerfile in-place
341
+ return !transformInPlace (dft, vm[" in-place" ].as <std::string>());
342
+
311
343
// transform Dockerfile on stdin and write to stdout
312
344
return !dft.transform (std::cin, std::cout);
313
345
}
0 commit comments