summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCindy Pallares <cindy.pallaresq@gmail.com>2015-03-17 17:20:45 -0500
committerCindy Pallares <cindy.pallaresq@gmail.com>2015-05-05 13:17:39 -0500
commit583adc34f8cd4abd1048c57c058e88b8c68d5780 (patch)
tree591492ab19ad847f949d4a47fb151ceec1ee953e
parent6db625f287ffcce80df65d25a117ceb0de2ea776 (diff)
downloadpython-glanceclient-583adc34f8cd4abd1048c57c058e88b8c68d5780.tar.gz
Check image-download for redirection
Currently when you download an image without specifying a file or redirecting the output, the image is dumped into the console as gibberish. We can avoid this if we check if file is being redirected or if a file is specified. Change-Id: I257760752f05b82b935cf19fb10573ee7ff1395d
-rw-r--r--glanceclient/v1/shell.py11
-rw-r--r--glanceclient/v2/shell.py16
2 files changed, 20 insertions, 7 deletions
diff --git a/glanceclient/v1/shell.py b/glanceclient/v1/shell.py
index bc689da..286d2a8 100644
--- a/glanceclient/v1/shell.py
+++ b/glanceclient/v1/shell.py
@@ -147,8 +147,8 @@ def do_image_show(gc, args):
@utils.arg('--file', metavar='<FILE>',
help='Local file to save downloaded image data to. '
- 'If this is not specified the image data will be '
- 'written to stdout.')
+ 'If this is not specified and there is no redirection '
+ 'the image data will be not be saved.')
@utils.arg('image', metavar='<IMAGE>', help='Name or ID of image to download.')
@utils.arg('--progress', action='store_true', default=False,
help='Show download progress bar.')
@@ -158,7 +158,12 @@ def do_image_download(gc, args):
body = image.data()
if args.progress:
body = progressbar.VerboseIteratorWrapper(body, len(body))
- utils.save_image(body, args.file)
+ if not (sys.stdout.isatty() and args.file is None):
+ utils.save_image(body, args.file)
+ else:
+ print('No redirection or local file specified for downloaded image '
+ 'data. Please specify a local file with --file to save '
+ 'downloaded image or redirect output to another source.')
@utils.arg('--id', metavar='<IMAGE_ID>',
diff --git a/glanceclient/v2/shell.py b/glanceclient/v2/shell.py
index 2628919..0fd3e46 100644
--- a/glanceclient/v2/shell.py
+++ b/glanceclient/v2/shell.py
@@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
+import sys
+
from glanceclient.common import progressbar
from glanceclient.common import utils
from glanceclient import exc
@@ -44,7 +46,7 @@ def get_image_schema():
' May be used multiple times.'))
@utils.arg('--file', metavar='<FILE>',
help='Local file that contains disk image to be uploaded '
- 'during creation. Alternatively, images can be passed '
+ 'during creation. Must be present if images are not passed '
'to the client via stdin.')
@utils.arg('--progress', action='store_true', default=False,
help='Show upload progress bar.')
@@ -247,8 +249,8 @@ def do_explain(gc, args):
@utils.arg('--file', metavar='<FILE>',
help='Local file to save downloaded image data to. '
- 'If this is not specified the image data will be '
- 'written to stdout.')
+ 'If this is not specified and there is no redirection '
+ 'the image data will be not be saved.')
@utils.arg('id', metavar='<IMAGE_ID>', help='ID of image to download.')
@utils.arg('--progress', action='store_true', default=False,
help='Show download progress bar.')
@@ -257,7 +259,13 @@ def do_image_download(gc, args):
body = gc.images.data(args.id)
if args.progress:
body = progressbar.VerboseIteratorWrapper(body, len(body))
- utils.save_image(body, args.file)
+ if not (sys.stdout.isatty() and args.file is None):
+ utils.save_image(body, args.file)
+ else:
+ msg = ('No redirection or local file specified for downloaded image '
+ 'data. Please specify a local file with --file to save '
+ 'downloaded image or redirect output to another source.')
+ utils.exit(msg)
@utils.arg('--file', metavar='<FILE>',