From 76a8d3704b448d4e36af1c2661920d2774edab64 Mon Sep 17 00:00:00 2001
From: Colleen Murphy <colleen@gazlene.net>
Date: Mon, 8 Feb 2016 17:24:52 -0800
Subject: [PATCH] Add --version option to disk-image-create

Being able to discover DIB's version from the command itself is
convenient. This patch adds a --version option to the disk-image-create
command, failing gracefully if diskimage-builder is not installed.

This adds an explicit dependency on pbr to the requirements since this
is required to run diskimage_builder/version.py outside of a test
environment.

This patch consciously chooses to only provide the long-form option
and no '-v' to allow for the future possibility that a '-v' might
indicate '--verbose' in the future.

Change-Id: I9fc084774d6c7a39a944b07680b3eb8be8e34f9c
---
 bin/disk-image-create | 12 +++++++++++-
 requirements.txt      |  1 +
 2 files changed, 12 insertions(+), 1 deletion(-)

diff --git a/bin/disk-image-create b/bin/disk-image-create
index 2b6a94d96..455ebda81 100755
--- a/bin/disk-image-create
+++ b/bin/disk-image-create
@@ -135,6 +135,7 @@ function show_options () {
         echo "    -p package[,package,package] -- list of packages to install in the image"
     fi
     echo "    -h|--help -- display this help and exit"
+    echo "    --version -- display version and exit"
     echo
     echo "ELEMENTS_PATH will allow you to specify multiple locations for the elements."
     echo
@@ -155,6 +156,14 @@ function show_options () {
     fi
 }
 
+function show_version() {
+    if [ -n "$_DIB_PYTHON_INSTALL" ]; then
+        python -c "from diskimage_builder import version; print(version.version_info.version_string())"
+    else
+        echo "diskimage-builder is not installed."
+    fi
+}
+
 INSTALL_PACKAGES=""
 IMAGE_TYPES=("qcow2")
 COMPRESS_IMAGE="true"
@@ -163,7 +172,7 @@ DIB_DEFAULT_INSTALLTYPE=${DIB_DEFAULT_INSTALLTYPE:-"source"}
 MKFS_OPTS=""
 ACI_MANIFEST=${ACI_MANIFEST:-}
 DOCKER_TARGET=""
-TEMP=`getopt -o a:ho:t:xucnp: -l no-tmpfs,offline,help,min-tmpfs:,image-size:,image-cache:,max-online-resize:,mkfs-options:,qemu-img-options:,ramdisk-element:,root-label:,install-type:,docker-target: -n $SCRIPTNAME -- "$@"`
+TEMP=`getopt -o a:ho:t:xucnp: -l no-tmpfs,offline,help,version,min-tmpfs:,image-size:,image-cache:,max-online-resize:,mkfs-options:,qemu-img-options:,ramdisk-element:,root-label:,install-type:,docker-target: -n $SCRIPTNAME -- "$@"`
 if [ $? -ne 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi
 
 # Note the quotes around `$TEMP': they are essential!
@@ -175,6 +184,7 @@ while true ; do
         -o) export IMAGE_NAME=$2; shift 2 ;;
         -t) IFS="," read -a IMAGE_TYPES <<< "$2"; export IMAGE_TYPES ; shift 2 ;;
         -h|--help) show_options; exit 0;;
+        --version) show_version; exit 0;;
         -x) shift; export DIB_DEBUG_TRACE=$(( $DIB_DEBUG_TRACE + 1 )); set -x;;
         -u) shift; export COMPRESS_IMAGE="";;
         -c) shift ; export CLEAR_ENV=1;;
diff --git a/requirements.txt b/requirements.txt
index dc970f0f1..6c4180f9b 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -3,6 +3,7 @@
 # process, which may cause wedges in the gate later.
 Babel>=1.3 # BSD
 dib-utils # Apache-2.0
+pbr>=1.6  # Apache-2.0
 PyYAML>=3.1.0 # MIT
 flake8<2.6.0,>2.4.1 # MIT
 six>=1.9.0 # MIT