Repository: zalando-stups/pierone-cli

Issue:
Support reverse image search
As soon as https://github.com/zalando-stups/pierone/issues/33 is done.

Discussion:

Patch:
diff --git a/pierone/cli.py b/pierone/cli.py
index 786b144..436bacd 100644
--- a/pierone/cli.py
+++ b/pierone/cli.py
@@ -208,5 +208,31 @@ def scm_source(config, team, artifact, tag, output):
                     max_column_widths={'revision': 10})
 
 
+@cli.command('image')
+@click.argument('image')
+@output_option
+@click.pass_obj
+def image(config, image, output):
+    '''List tags that point to this image'''
+    token = get_token()
+
+    resp = request(config.get('url'), '/tags/{}'.format(image), token['access_token'])
+
+    if resp.status_code == 404:
+        click.echo('Image {} not found'.format(image))
+        return
+
+    if resp.status_code == 412:
+        click.echo('Prefix {} matches more than one image.'.format(image))
+        return
+
+    tags = resp.json()
+
+    with OutputFormat(output):
+        print_table(['team', 'artifact', 'name'],
+                    tags,
+                    titles={'name': 'Tag', 'artifact': 'Artifact', 'team': 'Team'})
+
+
 def main():
     cli()


Test patch:
diff --git a/tests/test_cli.py b/tests/test_cli.py
index f2b360c..b319cb3 100644
--- a/tests/test_cli.py
+++ b/tests/test_cli.py
@@ -49,3 +49,20 @@ def test_scm_source(monkeypatch, tmpdir):
         result = runner.invoke(cli, ['scm-source', 'myteam', 'myart', '1.0'], catch_exceptions=False)
         assert 'myrev123' in result.output
         assert 'git:somerepo' in result.output
+
+def test_image(monkeypatch, tmpdir):
+    response = MagicMock()
+    response.json.return_value = [{'name': '1.0', 'team': 'stups', 'artifact': 'kio'}]
+
+    runner = CliRunner()
+    monkeypatch.setattr('pierone.cli.CONFIG_FILE_PATH', 'config.yaml')
+    monkeypatch.setattr('pierone.cli.get_named_token', MagicMock(return_value={'access_token': 'tok123'}))
+    monkeypatch.setattr('os.path.expanduser', lambda x: x.replace('~', str(tmpdir)))
+    monkeypatch.setattr('requests.get', MagicMock(return_value=response))
+    with runner.isolated_filesystem():
+        with open('config.yaml', 'w') as fd:
+            fd.write('')
+        result = runner.invoke(cli, ['image', 'abcd'], catch_exceptions=False)
+        assert 'kio' in result.output
+        assert 'stups' in result.output
+        assert '1.0' in result.output