From 6c02b9ebaebaaa7021df8863066c78c1e89bfe6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ege=20G=C3=BCne=C5=9F?= Date: Wed, 19 Jun 2019 08:16:13 +0300 Subject: [PATCH] Add multi account support --- redmine/cli/config.py | 12 ++++++++---- redmine/cli/main.py | 7 ++++--- redmine/cli/options.py | 1 + 3 files changed, 13 insertions(+), 7 deletions(-) diff --git a/redmine/cli/config.py b/redmine/cli/config.py index 417d7bd..fe1b6f2 100644 --- a/redmine/cli/config.py +++ b/redmine/cli/config.py @@ -5,7 +5,7 @@ class Config: - def __init__(self, *args, **kwargs): + def __init__(self, account=None): HOME = os.getenv("HOME") self.paths = [ os.path.join(HOME, ".redmine.conf"), @@ -15,6 +15,7 @@ def __init__(self, *args, **kwargs): self.url = None self.api_key = None self.aliases = {} + self.account = account URL = os.getenv("REDMINE_URL") API_KEY = os.getenv("REDMINE_API_KEY") @@ -32,9 +33,12 @@ def read_from_file(self): config.read(path) break - self.url = config["redmine"]["url"] - self.api_key = config["redmine"]["key"] - self.ssl_verify = config["redmine"].getboolean("ssl_verify") + if self.account is None: + self.account = config["accounts"]["default"] + + self.url = config[self.account]["url"] + self.api_key = config[self.account]["key"] + self.ssl_verify = config[self.account].getboolean("ssl_verify") try: self.aliases.update(config.items("aliases")) diff --git a/redmine/cli/main.py b/redmine/cli/main.py index 2b5553f..b7580a1 100644 --- a/redmine/cli/main.py +++ b/redmine/cli/main.py @@ -5,7 +5,7 @@ from requests.exceptions import HTTPError from redmine.cli.alias import AliasedGroup -from redmine.cli.config import pass_config +from redmine.cli.config import Config, pass_config from redmine.cli.helpers import get_description, get_note from redmine.cli.options import OPTIONS from redmine.issue import Issue, IssueStatus @@ -27,9 +27,10 @@ show_default=True, default=False, ) -@pass_config +@click.option(OPTIONS["account"]["long"]) @click.pass_context -def cli(ctx, cfg, **kwargs): +def cli(ctx, **kwargs): + cfg = Config(kwargs.get("account")) redmine = Redmine( cfg.url, cfg.api_key, cfg.ssl_verify, invalidate_cache=kwargs.get("force") ) diff --git a/redmine/cli/options.py b/redmine/cli/options.py index b8cfcea..4b0a4ff 100644 --- a/redmine/cli/options.py +++ b/redmine/cli/options.py @@ -25,4 +25,5 @@ "edit": {"long": "--edit/--no-edit", "short": "-e/-E"}, "force": {"long": "--force/--no-force", "help": "Invalidate cache"}, "json": {"long": "--json/--no-json"}, + "account": {"long": "--account"}, }