From 04cf3f5ecc742f6b3b2f0ec08ceda4bbed290c12 Mon Sep 17 00:00:00 2001 From: "Alex Lau (AvengerMoJo)" Date: Mon, 4 Dec 2023 15:12:52 +0800 Subject: [PATCH] Add env var as credential input in cli set default none Signed-off-by: Alex Lau (AvengerMoJo) --- README.md | 9 +++++++++ osc/conf.py | 21 ++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 522a9cc0a3..a485e5563f 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,15 @@ which is easier if you develop on osc. When you use osc for the first time, it will ask you for your username and password, and store it in `~/.config/osc/oscrc`. +Alternatively, you can set the environment variable to bypass the interactive input +session with the OSC_USERNAME as the username, OSC_PASSWORD as the password and +OSC_CREDENTIAL as ['Kernel keyring', 'Secret Service', 'Transient', 'Obfuscated', +'Config'] the credentials configuration like the following: + + OSC_USERNAME ='username' + OSC_PASSWORD ='password' + OSC_CREDENTIAL = 'Kernel keyring' + ## Keyrings diff --git a/osc/conf.py b/osc/conf.py index 07dfc0b94b..c81f7cc2e0 100644 --- a/osc/conf.py +++ b/osc/conf.py @@ -1999,10 +1999,14 @@ def interactive_config_setup(conffile, apiurl, initial=True): print() apiurl_no_scheme = urlsplit(apiurl)[1] or apiurl - user_prompt = f"Username [{apiurl_no_scheme}]: " - user = raw_input(user_prompt) - pass_prompt = f"Password [{user}@{apiurl_no_scheme}]: " - passwd = getpass.getpass(pass_prompt) + user = os.environ.get("OSC_USERNAME", None) + if not user: + user_prompt = f"Username [{apiurl_no_scheme}]: " + user = raw_input(user_prompt) + passwd = os.environ.get("OSC_PASSWORD", None) + if not passwd: + pass_prompt = f"Password [{user}@{apiurl_no_scheme}]: " + passwd = getpass.getpass(pass_prompt) creds_mgr_descr = select_credentials_manager_descr() if initial: config = {'user': user, 'pass': passwd} @@ -2020,9 +2024,13 @@ def select_credentials_manager_descr(): print('To use keyrings please install python%d-keyring.' % sys.version_info.major) creds_mgr_descriptors = credentials.get_credentials_manager_descriptors() + cred = os.environ.get("OSC_CREDENTIAL", None) + cred_pick = None rows = [] for i, creds_mgr_descr in enumerate(creds_mgr_descriptors, 1): rows += [str(i), creds_mgr_descr.name(), creds_mgr_descr.description()] + if creds_mgr_descr.name() == cred: + cred_pick = str(i) from .core import build_table headline = ('NUM', 'NAME', 'DESCRIPTION') @@ -2031,7 +2039,10 @@ def select_credentials_manager_descr(): for row in table: print(row) - i = raw_input('Select credentials manager [default=1]: ') + if cred_pick: + i = cred_pick + else: + i = raw_input('Select credentials manager [default=1]: ') if not i: i = "1" if not i.isdigit():