def delete(profile): config = utils.get_cli_config() if profile not in config: utils.error("Could not find profile '%s' in CLI config" % profile) config.pop(profile, None) utils.save_cli_config(config) print("Profile deleted.")
def view_token(): """Show token for current profile.""" config = utils.get_cli_config() _token = None selected = None for key in config.keys(): if nexuscli.config.SELECTED_KEY in config[key] and config[key][ nexuscli.config.SELECTED_KEY] is True: selected = key if TOKEN_KEY in config[key]: _token = config[key][TOKEN_KEY] print(_token) decoded = jwt.decode(_token, verify=False) print("\nDecoded token:") utils.print_json(decoded, colorize=True) expiry_utc = datetime.utcfromtimestamp(decoded['exp']) expires_in = expiry_utc.timestamp() - datetime.now().timestamp( ) if expires_in > 0: when = "in %s" % utils.print_time(expires_in) message_color = 'green' else: when = "%s ago" % utils.print_time(abs(expires_in)) message_color = 'red' msg = "\nExpiry: %s (%s)" % ( utils.datetime_from_utc_to_local(expiry_utc), when) click.echo(click.style(msg, fg=message_color)) if _token is None: if selected is None: utils.error("You haven't selected a profile.") else: utils.error('No tokens in selected profile: %s' % selected)
def current(): config = utils.get_cli_config() found = False for key in config.keys(): if _SELECTED_KEY_ in config[key] and key != select: print(key) found = True if not found: print("No profile selected.")
def select(profile): config = utils.get_cli_config() if profile not in config: utils.error("Could not find profile '%s' in CLI config" % delete) for key in config.keys(): if _SELECTED_KEY_ in config[key] and key != select: config[key].pop(_SELECTED_KEY_, None) config[profile][_SELECTED_KEY_] = True utils.save_cli_config(config) print("Selected profile: %s" % profile)
def create(profile, url): config = utils.get_cli_config() if profile in config and 'url' in config[profile]: utils.error("This deployment already exist (%s) with url: %s" % (create, config[create])) url = validate_nexus_url(url) config[profile] = {_URL_KEY_: url} if len(config) == 1: config[profile][_SELECTED_KEY_] = True utils.save_cli_config(config) print("Profile created.")
def current(): config = utils.get_cli_config() profile, selected_config = utils.get_selected_deployment_config(config) if selected_config is None: utils.error("You must first select a profile using the 'profiles' command") default_project = utils.get_default_project() if default_project is not None: print(default_project) else: utils.error("No default project selected in profile '%s'" % profile)
def list_profiles(): config = utils.get_cli_config() table = PrettyTable(['Profile', 'Selected', 'URL', 'Token']) table.align["Profile"] = "l" table.align["URL"] = "l" for key in config.keys(): selected = "" if 'selected' in config[key] and config[key]['selected'] is True: selected = "Yes" token = 'None' if 'token' in config[key]: decoded = jwt.decode(config[key]['token'], verify=False) expiry_utc = datetime.utcfromtimestamp(decoded['exp']) token = "Expiry: %s" % utils.datetime_from_utc_to_local(expiry_utc) table.add_row([key, selected, config[key]['url'], token]) print(table)
def set_token(token): """Registers the user token in the current profile.""" config = utils.get_cli_config() selected_profile = None for key in config.keys(): if nexuscli.config._SELECTED_KEY_ in config[key] and config[key][nexuscli.config._SELECTED_KEY_] is True: selected_profile = key break if selected_profile is None: utils.error("No profile selected, please use the profiles select to do that.") try: jwt.decode(token, verify=False) except jwt.exceptions.DecodeError: utils.error("Provided tokens could not be decoded. Please provide a valid tokens.") config[selected_profile][_TOKEN_KEY_] = token utils.save_cli_config(config)