alias remove command, alias list --json option - refs #2

This commit is contained in:
Simon Willison 2022-11-21 19:00:20 -08:00
commit ff3b64f425
3 changed files with 103 additions and 5 deletions

View file

@ -98,8 +98,73 @@ Options:
--help Show this message and exit.
Commands:
add Add an alias
list List aliases
add Add an alias
list List aliases
remove Remove an alias
```
<!-- [[[end]]] -->
### dclient alias list --help
<!-- [[[cog
import cog
result = runner.invoke(cli.cli, ["alias", "list", "--help"])
help = result.output.replace("Usage: cli", "Usage: dclient")
cog.out(
"```\n{}\n```".format(help)
)
]]] -->
```
Usage: dclient alias list [OPTIONS]
List aliases
Options:
--json Output raw JSON
--help Show this message and exit.
```
<!-- [[[end]]] -->
### dclient alias add --help
<!-- [[[cog
import cog
result = runner.invoke(cli.cli, ["alias", "add", "--help"])
help = result.output.replace("Usage: cli", "Usage: dclient")
cog.out(
"```\n{}\n```".format(help)
)
]]] -->
```
Usage: dclient alias add [OPTIONS] NAME URL
Add an alias
Options:
--help Show this message and exit.
```
<!-- [[[end]]] -->
### dclient alias remove --help
<!-- [[[cog
import cog
result = runner.invoke(cli.cli, ["alias", "remove", "--help"])
help = result.output.replace("Usage: cli", "Usage: dclient")
cog.out(
"```\n{}\n```".format(help)
)
]]] -->
```
Usage: dclient alias remove [OPTIONS] NAME
Remove an alias
Options:
--help Show this message and exit.
```
<!-- [[[end]]] -->

View file

@ -49,12 +49,16 @@ def alias():
@alias.command(name="list")
def list_():
@click.option("_json", "--json", is_flag=True, help="Output raw JSON")
def list_(_json):
"List aliases"
aliases_file = get_config_dir() / "aliases.json"
aliases = _load_aliases(aliases_file)
for alias, url in aliases.items():
click.echo(f"{alias} = {url}")
if _json:
click.echo(json.dumps(aliases, indent=2))
else:
for alias, url in aliases.items():
click.echo(f"{alias} = {url}")
@alias.command()
@ -70,6 +74,20 @@ def add(name, url):
aliases_file.write_text(json.dumps(aliases, indent=4))
@alias.command()
@click.argument("name")
def remove(name):
"Remove an alias"
config_dir = get_config_dir()
aliases_file = config_dir / "aliases.json"
aliases = _load_aliases(aliases_file)
if name in aliases:
del aliases[name]
aliases_file.write_text(json.dumps(aliases, indent=4))
else:
raise click.ClickException("No such alias")
def _load_aliases(aliases_file):
if aliases_file.exists():
aliases = json.loads(aliases_file.read_text())

View file

@ -57,6 +57,11 @@ def test_aliases(mocker, tmpdir, httpx_mock):
assert result.exit_code == 0
assert result.output == "foo = https://example.com/foo\n"
# --json mode:
result = runner.invoke(cli, ["alias", "list", "--json"])
assert result.exit_code == 0
assert json.loads(result.output) == {"foo": "https://example.com/foo"}
# Check the aliases file
aliases_file = pathlib.Path(tmpdir) / "aliases.json"
assert json.loads(aliases_file.read_text()) == {"foo": "https://example.com/foo"}
@ -84,3 +89,13 @@ def test_aliases(mocker, tmpdir, httpx_mock):
# Should have hit https://example.com/foo.json
url = httpx_mock.get_request().url
assert url == "https://example.com/foo.json?sql=select+11+%2A+3&_shape=objects"
# Remove alias
result = runner.invoke(cli, ["alias", "remove", "invalid"])
assert result.exit_code == 1
assert result.output == "Error: No such alias\n"
result = runner.invoke(cli, ["alias", "remove", "foo"])
assert result.exit_code == 0
assert result.output == ""
assert json.loads(aliases_file.read_text()) == {}