[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH] edit: Add -e 'expr' option to non-interactively apply expression to the file.



-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines.  Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v
>From 9e193fd6b94f40b2204cb94e4f08c404b52eecd4 Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones redhat com>
Date: Sun, 11 Jul 2010 16:46:15 +0100
Subject: [PATCH] edit: Add -e 'expr' option to non-interactively apply expression to the file.

(Suggested by Justin Clift).
---
 tools/virt-edit |   67 ++++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 54 insertions(+), 13 deletions(-)

diff --git a/tools/virt-edit b/tools/virt-edit
index e00e4cf..6f66191 100755
--- a/tools/virt-edit
+++ b/tools/virt-edit
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 # virt-edit
-# Copyright (C) 2009 Red Hat Inc.
+# Copyright (C) 2009-2010 Red Hat Inc.
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -40,6 +40,8 @@ virt-edit - Edit a file in a virtual machine
 
  virt-edit [--options] disk.img [disk.img ...] file
 
+ virt-edit [domname|disk.img] file -e 'expr'
+
 =head1 WARNING
 
 You must I<not> use C<virt-edit> on live virtual machines.  If you do
@@ -56,10 +58,18 @@ cases you should look at the L<guestfish(1)> tool.
 
 =head1 EXAMPLES
 
+Edit the named files interactively:
+
  virt-edit mydomain /boot/grub/grub.conf
 
  virt-edit mydomain /etc/passwd
 
+=head2 NON-INTERACTIVE EDITING
+
+Change the init default level to 5:
+
+ virt-edit mydomain /etc/inittab -e 's/^id:.*/id:5:initdefault:/'
+
 =head1 OPTIONS
 
 =over 4
@@ -92,6 +102,20 @@ connect to the default libvirt hypervisor.
 If you specify guest block devices directly, then libvirt is not used
 at all.
 
+=cut
+
+my $expr;
+
+=item B<--expr EXPR> | B<-e EXPR>
+
+Instead of launching the external editor, non-interactively
+apply the Perl expression C<EXPR> to each line in the file.
+
+This is most useful for making substitutions like L<sed(1)>
+but with the full power of Perl and Perl regexps:
+
+ virt-edit domname filename -e 's/foo/bar/'
+
 =back
 
 =cut
@@ -99,6 +123,7 @@ at all.
 GetOptions ("help|?" => \$help,
             "version" => \$version,
             "connect|c=s" => \$uri,
+            "expr|e=s" => \$expr,
     ) or pod2usage (2);
 pod2usage (1) if $help;
 if ($version) {
@@ -139,24 +164,39 @@ my $root_dev = $roots[0];
 my $os = $oses->{$root_dev};
 mount_operating_system ($g, $os, 0);
 
-my ($fh, $tempname) = tempfile ();
+my ($fh_not_used, $tempname) = tempfile ();
 
 # Allow this to fail in case eg. the file does not exist.
 $g->download($filename, $tempname);
 
-my $oldctime = (stat ($tempname))[10];
+if (!defined $expr) {
+    # Interactively edit the file.
+    my $oldctime = (stat ($tempname))[10];
 
-my $editor = $ENV{EDITOR};
-$editor ||= "vi";
-system ("$editor $tempname") == 0
-    or die "edit failed: $editor: $?";
+    my $editor = $ENV{EDITOR};
+    $editor ||= "vi";
+    system ("$editor $tempname") == 0
+        or die "edit failed: $editor: $?";
 
-my $newctime = (stat ($tempname))[10];
+    my $newctime = (stat ($tempname))[10];
 
-if ($oldctime != $newctime) {
-    $g->upload ($tempname, $filename)
+    if ($oldctime != $newctime) {
+        $g->upload ($tempname, $filename);
+    } else {
+        print __"File not changed.\n";
+    }
 } else {
-    print __"File not changed.\n";
+    my ($fh, $tempout) = tempfile ();
+
+    # Apply a Perl expression to the lines of the file.
+    open IFILE, $tempname or die "$tempname: $!";
+    while (<IFILE>) {
+        eval $expr;
+        print $fh $_ or die "print: $!";
+    }
+    close $fh;
+
+    $g->upload ($tempout, $filename);
 }
 
 $g->sync ();
@@ -187,7 +227,8 @@ L<virt-cat(1)>,
 L<Sys::Guestfs(3)>,
 L<Sys::Guestfs::Lib(3)>,
 L<Sys::Virt(3)>,
-L<http://libguestfs.org/>.
+L<http://libguestfs.org/>,
+L<perlre(1)>.
 
 =head1 AUTHOR
 
@@ -195,7 +236,7 @@ Richard W.M. Jones L<http://people.redhat.com/~rjones/>
 
 =head1 COPYRIGHT
 
-Copyright (C) 2009 Red Hat Inc.
+Copyright (C) 2009-2010 Red Hat Inc.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-- 
1.7.1


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]