Skip to content

Commit

Permalink
Fix #48 add config options to CLI help output
Browse files Browse the repository at this point in the history
This should add config options to CLI output as well as include
optional user directory configuration.

Signed-off-by: David Brown <dmlb2000@gmail.com>
  • Loading branch information
dmlb2000 committed May 17, 2019
1 parent 51ca03e commit af8ba36
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 22 deletions.
9 changes: 8 additions & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,19 @@ test_script:
cd tests;
cp -r ../travis .;
cp ../README.md .;
coverage run --include='*/site-packages/pacifica/cli/*' -m pacifica.cli --config ../travis/uploader.json --help;
mkdir ~/.pacifica_cli;
coverage run --include='*/site-packages/pacifica/cli/*' -m pacifica.cli --help;
coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli upload --help;
coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli download --help;
coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli --config ../travis/uploader.json --help;
coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli --config=../travis/uploader.json --help;
$env:UPLOADER_CONFIG = '../travis/uploader.json';
coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli upload --help;
coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli download --help;
coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli configure --help;
$env:UPLOADER_CONFIG = ''; cp '../travis/uploader.json' ~/.pacifica_cli/uploader.json;
coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli upload --help;
$env:UPLOADER_CONFIG = '../travis/uploader.json';
printf '\n\n\n\n\n\n\n' | coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli configure;
printf '\n\n\n\n\n\nTrue\nclientssl\n~/.pacifica-cli/my.key\n~/.pacifica-cli/my.cert\n' | coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli configure;
printf '\n\n\n\n\n\nFalse\nbasic\nusername\npassword\n' | coverage run --include='*/site-packages/pacifica/cli/*' -a -m pacifica.cli configure;
Expand Down
10 changes: 10 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ environment variable `UPLOADER_CONFIG`. By default the environment
variable is set to `uploader.json`. However, it could be managed at a
system level or changed on the command line by the `--config` option.

The directories the `UPLOADER_CONFIG` are looked for in order are:

- `/etc/pacifica-cli/uploader.json`
- `VIRTUAL_ENV_ROOT/pacifica-cli/uploader.json`
- `~/.pacifica_cli/uploader.json`
- `$PWD/uploader.json`

The command line is evaluated last so it will override any of the
previous paths.

The contents of the metadata configuration file is complex and should
be read from
[here](https://pacifica-uploader.readthedocs.io/en/latest/metadataconfig.html).
Expand Down
57 changes: 36 additions & 21 deletions pacifica/cli/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"""The CLI module contains all the logic needed to run a CLI."""
import sys
import argparse
from os import getenv
from os import getenv, path
from pacifica.uploader.metadata import metadata_decode
from .methods import upload, configure, download
from .utils import system_config_path, compressor_generator
from .utils import system_config_path, user_config_path, compressor_generator


def arg_to_compressor_obj(str_obj=None):
Expand Down Expand Up @@ -35,6 +35,35 @@ def mangle_config_argument(argv):
return (None, argv)


def parse_uploader_config(upload_parser):
"""Find the uploader metadata config and parse arguments out of it."""
upload_file_name = 'uploader.json'
default_config = getenv(
'UPLOADER_CONFIG', system_config_path(upload_file_name))
if default_config == upload_file_name and path.isfile(user_config_path(upload_file_name)):
default_config = user_config_path(upload_file_name)
config_file, argv = mangle_config_argument(sys.argv)
if not config_file:
config_file = default_config
if path.isfile(config_file):
config_data = metadata_decode(open(config_file).read())
for config_part in config_data:
if not config_part.value:
upload_parser.add_argument(
'--{}-regex'.format(config_part.metaID), required=False,
dest='{}_regex'.format(config_part.metaID),
help='{} regular expression match.'.format(
config_part.displayTitle)
)
upload_parser.add_argument(
'--{}'.format(config_part.metaID), '-{}'.format(
config_part.metaID[0]),
help=config_part.displayTitle, required=False
)
return config_file, argv, config_data
return default_config, sys.argv, None


def main():
"""Main method to deal with command line argument parsing."""
parser = argparse.ArgumentParser()
Expand All @@ -46,25 +75,11 @@ def main():
download_parser = subparsers.add_parser(
'download', help='download help', description='perform download')

default_config = getenv(
'UPLOADER_CONFIG', system_config_path('uploader.json'))
config_file, argv = mangle_config_argument(sys.argv)
if not config_file:
config_file = default_config
config_data = metadata_decode(open(config_file).read())
for config_part in config_data:
if not config_part.value:
upload_parser.add_argument(
'--{}-regex'.format(config_part.metaID), required=False,
dest='{}_regex'.format(config_part.metaID),
help='{} regular expression match.'.format(
config_part.displayTitle)
)
upload_parser.add_argument(
'--{}'.format(config_part.metaID), '-{}'.format(
config_part.metaID[0]),
help=config_part.displayTitle, required=False
)
default_config, argv, config_data = parse_uploader_config(upload_parser)
parser.add_argument(
'--config', dest='config', default=default_config,
help='Upload configuration metadata.', required=False
)
parser.add_argument(
'--verbose', dest='verbose', default='info',
help='Enable verbose logging.', required=False
Expand Down
8 changes: 8 additions & 0 deletions travis/unit-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,20 @@ cp -r ../README.md ../travis .
# Help commands
############################
COV_RUN="coverage run --include=*/site-packages/pacifica/cli/*"
mkdir ~/.pacifica_cli
$COV_RUN -m pacifica.cli --help
$COV_RUN -m pacifica.cli upload --help
$COV_RUN -m pacifica.cli download --help
$COV_RUN -m pacifica.cli --config ../travis/uploader.json --help
$COV_RUN -a -m pacifica.cli --config=../travis/uploader.json --help
export UPLOADER_CONFIG=$PWD/../travis/uploader.json
$COV_RUN -a -m pacifica.cli upload --help
$COV_RUN -a -m pacifica.cli download --help
$COV_RUN -a -m pacifica.cli configure --help
unset UPLOADER_CONFIG
cp $PWD/../travis/uploader.json ~/.pacifica_cli/uploader.json
$COV_RUN -a -m pacifica.cli upload --help
export UPLOADER_CONFIG=$PWD/../travis/uploader.json

############################
# Configure commands
Expand Down

0 comments on commit af8ba36

Please sign in to comment.