提交 f46fb819 编写于 作者: J Ján Tomko

test-wrap-argv: add --in-place parameter

If --in-place is supplied as the first argument to the script,
replace the file in-place instead of printing to stdout.
上级 b0590a53
...@@ -22,7 +22,16 @@ ...@@ -22,7 +22,16 @@
# of the file. Parameter values that are longer than 80 chars will # of the file. Parameter values that are longer than 80 chars will
# also be split. # also be split.
# #
# If --in-place is supplied as the first parameter of this script,
# the files will be changed in place.
# Otherwise the rewrapped files are printed to the standard output.
$in_place = 0;
if (@ARGV[0] eq "--in-place") {
$in_place = 1;
shift @ARGV;
}
foreach my $file (@ARGV) { foreach my $file (@ARGV) {
&rewrap($file); &rewrap($file);
...@@ -57,10 +66,19 @@ sub rewrap { ...@@ -57,10 +66,19 @@ sub rewrap {
# Now each @lines represents a single command, we # Now each @lines represents a single command, we
# can process them # can process them
foreach my $line (@lines) { @lines = map { &rewrap_line($_) } @lines;
print &rewrap_line ($line);
}
if ($in_place) {
open FILE, ">", $file or die "cannot write $file: $!";
foreach my $line (@lines) {
print FILE $line;
}
close FILE;
} else {
foreach my $line (@lines) {
print $line;
}
}
} }
sub rewrap_line { sub rewrap_line {
......
...@@ -438,26 +438,20 @@ static int ...@@ -438,26 +438,20 @@ static int
virTestRewrapFile(const char *filename) virTestRewrapFile(const char *filename)
{ {
int ret = -1; int ret = -1;
char *outbuf = NULL;
char *script = NULL; char *script = NULL;
virCommandPtr cmd = NULL; virCommandPtr cmd = NULL;
if (virAsprintf(&script, "%s/test-wrap-argv.pl", abs_srcdir) < 0) if (virAsprintf(&script, "%s/test-wrap-argv.pl", abs_srcdir) < 0)
goto cleanup; goto cleanup;
cmd = virCommandNewArgList(script, filename, NULL); cmd = virCommandNewArgList(script, "--in-place", filename, NULL);
virCommandSetOutputBuffer(cmd, &outbuf);
if (virCommandRun(cmd, NULL) < 0) if (virCommandRun(cmd, NULL) < 0)
goto cleanup; goto cleanup;
if (virFileWriteStr(filename, outbuf, 0666) < 0)
goto cleanup;
ret = 0; ret = 0;
cleanup: cleanup:
VIR_FREE(script); VIR_FREE(script);
virCommandFree(cmd); virCommandFree(cmd);
VIR_FREE(outbuf);
return ret; return ret;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册
新手
引导
客服 返回
顶部