summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMax Wittig <max.wittig95@gmail.com>2018-11-03 15:40:20 +0100
committerMax Wittig <max.wittig95@gmail.com>2018-11-03 15:50:38 +0100
commit194327a712ed6d7aa6a72ce22c74499d3f4ee360 (patch)
treea96c0afb7a17665be56cd84e76e75b28c9c286dd
parent74623cf38d20fe93183cd3721b751796019ab98c (diff)
downloadgitlab-194327a712ed6d7aa6a72ce22c74499d3f4ee360.tar.gz
fix(cli): exit on config parse error, instead of crashing
Exit and hint user about possible errors Fixes #622
-rw-r--r--gitlab/cli.py11
-rw-r--r--gitlab/config.py17
2 files changed, 25 insertions, 3 deletions
diff --git a/gitlab/cli.py b/gitlab/cli.py
index e79ac6d..17917f5 100644
--- a/gitlab/cli.py
+++ b/gitlab/cli.py
@@ -17,6 +17,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
+
import argparse
import functools
import importlib
@@ -143,9 +144,13 @@ def main():
# load the propermodule (v3 or v4) accordingly. At that point we don't have
# any subparser setup
(options, args) = parser.parse_known_args(sys.argv)
-
- config = gitlab.config.GitlabConfigParser(options.gitlab,
- options.config_file)
+ try:
+ config = gitlab.config.GitlabConfigParser(
+ options.gitlab,
+ options.config_file
+ )
+ except gitlab.config.ConfigError as e:
+ sys.exit(e)
cli_module = importlib.import_module('gitlab.v%s.cli' % config.api_version)
# Now we build the entire set of subcommands and do the complete parsing
diff --git a/gitlab/config.py b/gitlab/config.py
index 9f4c11d..1c76594 100644
--- a/gitlab/config.py
+++ b/gitlab/config.py
@@ -37,10 +37,27 @@ class GitlabDataError(ConfigError):
pass
+class GitlabConfigMissingError(ConfigError):
+ pass
+
+
class GitlabConfigParser(object):
def __init__(self, gitlab_id=None, config_files=None):
self.gitlab_id = gitlab_id
_files = config_files or _DEFAULT_FILES
+ file_exist = False
+ for file in _files:
+ if os.path.exists(file):
+ file_exist = True
+ if not file_exist:
+ raise GitlabConfigMissingError(
+ "Config file not found. \nPlease create one in "
+ "one of the following locations: {} \nor "
+ "specify a config file using the '-c' parameter.".format(
+ ", ".join(_DEFAULT_FILES)
+ )
+ )
+
self._config = configparser.ConfigParser()
self._config.read(_files)