Skip to content

Commit

Permalink
Remove prompts for credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
adhilto committed Jan 14, 2025
1 parent bc19050 commit 5a74b57
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 66 deletions.
9 changes: 2 additions & 7 deletions scubagoggles/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class UserConfig:

_main = _defaults['scubagoggles']

_defaults['scubagoggles']['credentials'] = (f'{_main["output_dir"]}/'
'credentials.json')
_defaults['scubagoggles']['credentials'] = (f'./credentials.json')

_default_config_file = Path('~/.scubagoggles/userdefaults.yaml').expanduser()

Expand All @@ -45,7 +44,7 @@ def __init__(self, config_file: Union[str, os.PathLike] = None):
configuration stored in the class.
:param Path config_file: [optional] user configuration file. By
default, this is ~/.scubagoggles
default, this is ~/.scubagoggles/userdefaults.yaml
"""

self._config_file = (Path(os.path.expandvars(config_file))
Expand Down Expand Up @@ -102,10 +101,6 @@ def credentials_file(self) -> Union[Path, None]:

credentials = self._get_path_config('credentials')

if self._check and credentials and (not credentials.exists()
or not credentials.is_file()):
raise FileNotFoundError(f'? {credentials} - credentials not found')

return credentials

@credentials_file.setter
Expand Down
9 changes: 3 additions & 6 deletions scubagoggles/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,12 @@ def gws_dispatch(args):
metavar = '<output-JSON-file>',
help = help_msg)

try:
default_credentials = user_config.credentials_file
except FileNotFoundError:
default_credentials = str(user_dir / 'credentials.json')
help_msg = ('The location and name of the OAuth / service account '
f'credentials json file. Defaults to "{default_credentials}".')
'credentials json file. Defaults to '
f'"{user_config.credentials_file}".')
parser.add_argument('--credentials',
'-c',
default = Path(default_credentials),
default = Path(user_config.credentials_file),
metavar = '<credentials-JSON-file>',
type = path_parser,
help = help_msg)
Expand Down
60 changes: 7 additions & 53 deletions scubagoggles/user_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def user_directory(arguments: argparse.Namespace):
# Gimme a break...
# pylint: disable=too-many-branches

print('Setup: output directory')
log.debug('Setup: output directory')

config = arguments.user_config
create_dir = arguments.mkdir
Expand Down Expand Up @@ -186,7 +186,7 @@ def opa_directory(arguments: argparse.Namespace):
configuration; False otherwise.
"""

print('Setup: OPA executable directory')
log.debug('Setup: OPA executable directory')

check = not arguments.nocheck
config = arguments.user_config
Expand Down Expand Up @@ -295,7 +295,7 @@ def validate_opa_dir(opa_dir: Path = None):

if not opa_exe:
where = opa_dir or 'PATH'
log.warning('OPA executable not found in %s', where)
log.info('OPA executable not found in %s', where)

return opa_exe is not None

Expand All @@ -310,71 +310,25 @@ def credentials_file(arguments: argparse.Namespace):
configuration; False otherwise.
"""

print('Setup: Google API credentials file')
log.debug('Setup: Google API credentials file')

check = not arguments.nocheck
config = arguments.user_config
credentials = arguments.credentials
prompt = not arguments.noprompt

if credentials:

# The user has explicitly specified the credentials file. We only
# need to check whether the file exists.

if check and not credentials.is_file():
raise FileNotFoundError(f'? {credentials} - credentials not found')
raise FileNotFoundError(f'? {credentials} - file not found')

print(f' specified file: {credentials}')
log.debug(f' specified file: %s', credentials)

config.credentials_file = credentials.resolve()

return True

if not config.file_exists:

# The user has no configuration file, so they're starting from scratch.
# If the credentials file exists, it may be in the output directory
# because of a "legacy" setup.

legacy_credentials = config.output_dir / 'credentials.json'

if legacy_credentials.is_file():
print(f' found: {legacy_credentials}')
config.credentials_file = legacy_credentials
return True

elif config.credentials_file.is_file() or not check:
print(f' {config.credentials_file}')
return False
else:
log.error('? %s - Google credential files missing',
config.credentials_file)
if not prompt:
return False

# There's no configuration file found, so we have to ask for the location.

while not credentials:
answer = prompt_string('Google credentials (JSON) file',
config.credentials_file)

if not answer:
continue

answer = Path(os.path.expandvars(answer)).expanduser().absolute()

if not answer.exists():
log.warning('Google credentials file not found in %s', answer)
else:
print(f' {config.credentials_file}')

credentials = answer

config.credentials_file = credentials.resolve()

return True

return False

def find_legacy_dir(config):

Expand Down

0 comments on commit 5a74b57

Please sign in to comment.