virt-xml-validate: Allow input to be read from stdin

Signed-off-by: Johannes Holmberg <johannes.holmberg@dataductus.se>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
This commit is contained in:
Johannes Holmberg 2019-05-21 08:33:05 +00:00 committed by Martin Kletzander
parent 143c2de113
commit 1134fd6289

View File

@ -16,7 +16,17 @@
set -e 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) -h | --h | --he | --hel | --help)
cat <<EOF cat <<EOF
Usage: Usage:
@ -34,7 +44,7 @@ $0 (libvirt) @VERSION@
EOF EOF
exit ;; exit ;;
--) shift ;; --) shift ;;
-*) -?*)
echo "$0: unrecognized option '$1'" >&2 echo "$0: unrecognized option '$1'" >&2
exit 1 ;; exit 1 ;;
esac esac
@ -42,18 +52,27 @@ esac
XMLFILE="$1" XMLFILE="$1"
TYPE="$2" TYPE="$2"
if [ -z "$XMLFILE" ]; then if [ "$XMLFILE" = "-" ]; then
echo "syntax: $0 XMLFILE [TYPE]" >&2 TMPFILE=`mktemp --tmpdir virt-xml.XXXX`
exit 1 cat > "$TMPFILE"
fi else
if [ -z "$XMLFILE" ]; then
echo "syntax: $0 XMLFILE [TYPE]" >&2
exit 1
fi
if [ ! -f "$XMLFILE" ]; then if [ ! -f "$XMLFILE" ]; then
echo "$0: document $XMLFILE does not exist" >&2 echo "$0: document $XMLFILE does not exist" >&2
exit 2 exit 2
fi
fi fi
if [ -z "$TYPE" ]; then 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 case "$ROOT" in
*domainsnapshot*) # Must come first, since *domain* is a substring *domainsnapshot*) # Must come first, since *domain* is a substring
TYPE="domainsnapshot" TYPE="domainsnapshot"
@ -101,6 +120,9 @@ if [ ! -f "$SCHEMA" ]; then
exit 4 exit 4
fi fi
xmllint --noout --relaxng "$SCHEMA" "$XMLFILE" if [ -n "$TMPFILE" ]; then
xmllint --noout --relaxng "$SCHEMA" - < "$TMPFILE"
else
xmllint --noout --relaxng "$SCHEMA" "$XMLFILE"
fi
exit exit