[libvirt] [PATCHv2] virt-xml-validate: Allow input to be read from stdin

Johannes Holmberg posted 1 patch 4 years, 10 months ago
Test syntax-check passed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20190521083216.22488-1-johannes.holmberg@dataductus.se
tools/virt-xml-validate.in | 46 ++++++++++++++++++++++++++++----------
1 file changed, 34 insertions(+), 12 deletions(-)
[libvirt] [PATCHv2] virt-xml-validate: Allow input to be read from stdin
Posted by Johannes Holmberg 4 years, 10 months ago
Signed-off-by: Johannes Holmberg <johannes.holmberg@dataductus.se>
---

Changes from v1:

 - Quotes around $TMPFILE everywhere.
 - Explicit -n checks in if statements
 - Fixed one instance of incorrect indentation
 - Signed-off-by line in commit message
 
 tools/virt-xml-validate.in | 46 ++++++++++++++++++++++++++++----------
 1 file changed, 34 insertions(+), 12 deletions(-)

diff --git a/tools/virt-xml-validate.in b/tools/virt-xml-validate.in
index 64aeaaaa33..5cb7dcd276 100644
--- a/tools/virt-xml-validate.in
+++ b/tools/virt-xml-validate.in
@@ -16,7 +16,17 @@
 
 set -e
 
-case $1 in
+TMPFILE=
+
+cleanup() {
+  if [ -n "$TMPFILE" ]; then
+    rm -f "$TMPFILE"
+  fi
+}
+
+trap cleanup EXIT
+
+case "$1" in
   -h | --h | --he | --hel | --help)
     cat <<EOF
 Usage:
@@ -34,7 +44,7 @@ $0 (libvirt) @VERSION@
 EOF
     exit ;;
   --) shift ;;
-  -*)
+  -?*)
     echo "$0: unrecognized option '$1'" >&2
     exit 1 ;;
 esac
@@ -42,18 +52,27 @@ esac
 XMLFILE="$1"
 TYPE="$2"
 
-if [ -z "$XMLFILE" ]; then
-  echo "syntax: $0 XMLFILE [TYPE]" >&2
-  exit 1
-fi
+if [ "$XMLFILE" = "-" ]; then
+  TMPFILE=`mktemp --tmpdir virt-xml.XXXX`
+  cat > "$TMPFILE"
+else
+  if [ -z "$XMLFILE" ]; then
+    echo "syntax: $0 XMLFILE [TYPE]" >&2
+    exit 1
+  fi
 
-if [ ! -f "$XMLFILE" ]; then
-  echo "$0: document $XMLFILE does not exist" >&2
-  exit 2
+  if [ ! -f "$XMLFILE" ]; then
+    echo "$0: document $XMLFILE does not exist" >&2
+    exit 2
+  fi
 fi
 
 if [ -z "$TYPE" ]; then
-  ROOT=`xmllint --stream --debug "$XMLFILE" 2>/dev/null | grep "^0 1 " | awk '{ print $3 }'`
+  if [ -n "$TMPFILE" ]; then
+    ROOT=`xmllint --stream --debug - < "$TMPFILE" 2>/dev/null | grep "^0 1 " | awk '{ print $3 }'`
+  else
+    ROOT=`xmllint --stream --debug "$XMLFILE" 2>/dev/null | grep "^0 1 " | awk '{ print $3 }'`
+  fi
   case "$ROOT" in
      *domainsnapshot*) # Must come first, since *domain* is a substring
         TYPE="domainsnapshot"
@@ -101,6 +120,9 @@ if [ ! -f "$SCHEMA" ]; then
   exit 4
 fi
 
-xmllint --noout --relaxng "$SCHEMA" "$XMLFILE"
-
+if [ -n "$TMPFILE" ]; then
+  xmllint --noout --relaxng "$SCHEMA" - < "$TMPFILE"
+else
+  xmllint --noout --relaxng "$SCHEMA" "$XMLFILE"
+fi
 exit
-- 
2.17.1


--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCHv2] virt-xml-validate: Allow input to be read from stdin
Posted by Martin Kletzander 4 years, 9 months ago
On Tue, May 21, 2019 at 08:33:05AM +0000, Johannes Holmberg wrote:
>Signed-off-by: Johannes Holmberg <johannes.holmberg@dataductus.se>
>---
>
>Changes from v1:
>
> - Quotes around $TMPFILE everywhere.
> - Explicit -n checks in if statements
> - Fixed one instance of incorrect indentation
> - Signed-off-by line in commit message
>

Sorry for such late reply, it seems fine to me, so:

Reviewed-by: Martin Kletzander <mkletzan@redhat.com>

&& pushed.

Martin
--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list