From b9d7220b102b56a8c6b85f82e548ba634699cacb Mon Sep 17 00:00:00 2001 From: Matthias Lehmann Date: Wed, 29 Jan 2014 15:54:19 +0100 Subject: [PATCH] check --ssl-cert and --ssl-key to be files --- httpie/cli.py | 3 +++ httpie/utils.py | 10 ++++++++++ 2 files changed, 13 insertions(+) diff --git a/httpie/cli.py b/httpie/cli.py index 5de07a37..229ac274 100644 --- a/httpie/cli.py +++ b/httpie/cli.py @@ -19,6 +19,7 @@ from .input import (Parser, AuthCredentialsArgType, KeyValueArgType, OUT_REQ_HEAD, OUT_REQ_BODY, OUT_RESP_HEAD, OUT_RESP_BODY, OUTPUT_OPTIONS, OUTPUT_OPTIONS_DEFAULT, PRETTY_MAP, PRETTY_STDOUT_TTY_ONLY, SessionNameValidator) +from .utils import existing_file class HTTPieHelpFormatter(RawDescriptionHelpFormatter): @@ -468,6 +469,7 @@ network.add_argument( network.add_argument( '--ssl-cert', default=None, + type=existing_file, help=""" You can specify a local cert to use as client side SSL certificate. This file may either contain both private key and certificate or you may @@ -479,6 +481,7 @@ network.add_argument( network.add_argument( '--ssl-key', default=None, + type=existing_file, help=""" The private key to use with SSL. Only needed if --ssl-cert is given and the certificate file does not contain the private key. diff --git a/httpie/utils.py b/httpie/utils.py index fe721c4e..3200cce2 100644 --- a/httpie/utils.py +++ b/httpie/utils.py @@ -1,4 +1,5 @@ from __future__ import division +import argparse def humanize_bytes(n, precision=2): @@ -44,3 +45,12 @@ def humanize_bytes(n, precision=2): break return '%.*f %s' % (precision, n / factor, suffix) + + +def existing_file(filename): + try: + open(filename, 'rb') + except IOError as ex: + raise argparse.ArgumentTypeError( + '%s: %s' % (filename, ex.args[1])) + return filename