Skip to content

Commit

Permalink
Document config file
Browse files Browse the repository at this point in the history
Also hide it in output of mapbox-config if there is none.
  • Loading branch information
Sean Gillies committed Dec 17, 2015
1 parent de0406d commit 8a9c215
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 14 deletions.
30 changes: 23 additions & 7 deletions mapboxcli/scripts/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ def read_config(cfg):
@cligj.verbose_opt
@cligj.quiet_opt
@click.option('--access-token', help="Your Mapbox access token.")
@click.option('--config', '-c', type=click.Path(resolve_path=True),
default=os.path.join(click.get_app_dir('mapbox'), 'mapbox.ini'),
@click.option('--config', '-c', type=click.Path(exists=True, resolve_path=True),
help="Config file")
@click.pass_context
def main_group(ctx, verbose, quiet, access_token, config):
Expand All @@ -50,17 +49,35 @@ def main_group(ctx, verbose, quiet, access_token, config):
$ mapbox --access-token MY_TOKEN ...
or as an environment variable named MAPBOX_ACCESS_TOKEN or
MapboxAccessToken.
as an environment variable named MAPBOX_ACCESS_TOKEN or
MapboxAccessToken
\b
$ export MAPBOX_ACCESS_TOKEN=MY_TOKEN
$ mapbox ...
or in a config file
\b
; configuration file mapbox.ini
[mapbox]
access-token = MY_TOKEN
The OS-dependent default config file path is something like
\b
~/Library/Application Support/mapbox/mapbox.ini
~/.config/mapbox/mapbox.ini
~/.mapbox/mapbox.ini
"""
ctx.obj = {}
ctx.obj['cfg'] = read_config(config)
ctx.default_map = ctx.obj['cfg']
config = config or os.path.join(click.get_app_dir('mapbox'), 'mapbox.ini')
cfg = read_config(config)
if cfg:
ctx.obj['config_file'] = config
ctx.obj['cfg'] = cfg
ctx.default_map = cfg

verbosity = ctx.lookup_default('mapbox.verbosity') or 0
if verbose or quiet:
Expand All @@ -74,4 +91,3 @@ def main_group(ctx, verbose, quiet, access_token, config):

ctx.obj['verbosity'] = verbosity
ctx.obj['access_token'] = access_token
ctx.obj['config_file'] = config
23 changes: 16 additions & 7 deletions mapboxcli/scripts/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,32 @@
@click.command(short_help="Show all config settings.")
@click.pass_context
def config(ctx):
"""Show config settings from command line, environment, and config
files."""
ctx.default_map = ctx.obj['cfg']
"""Show access token and other configuration settings.
The access token and command verbosity level can be set on the
command line, as environment variables, and in mapbox.ini config
files.
"""
ctx.default_map = ctx.obj['cfg']
click.echo("CLI:")
click.echo("access-token = {0}".format(ctx.obj['access_token']))
click.echo("verbosity = {0}".format(ctx.obj['verbosity']))
click.echo("")

click.echo("Environment:")
if 'MAPBOX_ACCESS_TOKEN' in os.environ:
click.echo("MAPBOX_ACCESS_TOKEN = {0}".format(
os.environ['MAPBOX_ACCESS_TOKEN']))
elif 'MapboxAccessToken' in os.environ:
click.echo("MapboxAccessToken = {0}".format(
os.environ['MapboxAccessToken']))
if 'MAPBOX_VERBOSE' in os.environ:
click.echo("MAPBOX_VERBOSE = {0}".format(
os.environ['MAPBOX_VERBOSE']))
click.echo("")
click.echo("Config file {0}:".format(ctx.obj['config_file']))
for key, value in ctx.default_map.items():
click.echo("{0} = {1}".format(key, value))
click.echo("")

if 'config_file' in ctx.obj:
click.echo("Config file {0}:".format(ctx.obj['config_file']))
for key, value in ctx.default_map.items():
click.echo("{0} = {1}".format(key, value))
click.echo("")
4 changes: 4 additions & 0 deletions tests/test_cli.py → tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ def test_config_file(tmpdir):
cfg.write("verbosity = 11\n")
runner = CliRunner()
result = runner.invoke(main_group, ['-c', config, 'config'], catch_exceptions=False)
assert config in result.output
assert "access-token = pk.test_config_file" in result.output
assert "verbosity = 11" in result.output

Expand All @@ -23,6 +24,7 @@ def test_config_options():
main_group,
['--access-token', 'pk.test_config_options', '-vvvv', '-q', 'config'],
catch_exceptions=False)
assert "Config file" not in result.output
assert "access-token = pk.test_config_options" in result.output
assert "verbosity = 3" in result.output

Expand All @@ -32,6 +34,7 @@ def test_config_envvar(monkeypatch):
monkeypatch.setenv('MAPBOX_ACCESS_TOKEN', 'pk.test_config_envvar_2')
runner = CliRunner()
result = runner.invoke(main_group, ['config'], catch_exceptions=False)
assert "Config file" not in result.output
assert "access-token = pk.test_config_envvar_2" in result.output
monkeypatch.undo()

Expand All @@ -41,5 +44,6 @@ def test_config_envvar_2(monkeypatch):
monkeypatch.setenv('MapboxAccessToken', 'pk.test_config_envvar_2')
runner = CliRunner()
result = runner.invoke(main_group, ['config'], catch_exceptions=False)
assert "Config file" not in result.output
assert "access-token = pk.test_config_envvar_2" in result.output
monkeypatch.undo()

0 comments on commit 8a9c215

Please sign in to comment.