mirror of
https://github.com/tahoe-lafs/tahoe-lafs.git
synced 2025-04-15 06:36:40 +00:00
Merge pull request #452 from meejah/list-aliases-json.1
Add --json option to 'tahoe list aliases'
This commit is contained in:
commit
a1cb401f06
@ -85,6 +85,7 @@ class ListAliasesOptions(FileStoreOptions):
|
||||
description = """Display a table of all configured aliases."""
|
||||
optFlags = [
|
||||
("readonly-uri", None, "Show read-only dircaps instead of readwrite"),
|
||||
("json", None, "Show JSON output"),
|
||||
]
|
||||
|
||||
class ListOptions(FileStoreOptions):
|
||||
|
@ -1,6 +1,7 @@
|
||||
|
||||
import os.path
|
||||
import codecs
|
||||
import json
|
||||
|
||||
from allmydata.util.assertutil import precondition
|
||||
|
||||
@ -95,24 +96,46 @@ def create_alias(options):
|
||||
print >>stdout, "Alias %s created" % (quote_output(alias),)
|
||||
return 0
|
||||
|
||||
|
||||
def _get_alias_details(nodedir):
|
||||
aliases = get_aliases(nodedir)
|
||||
alias_names = sorted(aliases.keys())
|
||||
data = {}
|
||||
for name in alias_names:
|
||||
dircap = uri.from_string(aliases[name])
|
||||
data[name] = {
|
||||
"readwrite": dircap.to_string(),
|
||||
"readonly": dircap.get_readonly().to_string(),
|
||||
}
|
||||
return data
|
||||
|
||||
|
||||
def list_aliases(options):
|
||||
nodedir = options['node-directory']
|
||||
stdout = options.stdout
|
||||
stderr = options.stderr
|
||||
aliases = get_aliases(nodedir)
|
||||
alias_names = sorted(aliases.keys())
|
||||
max_width = max([len(quote_output(name)) for name in alias_names] + [0])
|
||||
|
||||
data = _get_alias_details(nodedir)
|
||||
|
||||
max_width = max([len(quote_output(name)) for name in data.keys()] + [0])
|
||||
fmt = "%" + str(max_width) + "s: %s"
|
||||
rc = 0
|
||||
for name in alias_names:
|
||||
dircap = uri.from_string(aliases[name])
|
||||
if options['readonly-uri']:
|
||||
dircap = dircap.get_readonly()
|
||||
|
||||
if options['json']:
|
||||
try:
|
||||
print >>stdout, fmt % (unicode_to_output(name), unicode_to_output(dircap.to_string().decode('utf-8')))
|
||||
# XXX why are we presuming utf-8 output?
|
||||
print >>stdout, json.dumps(data, indent=4).decode('utf-8')
|
||||
except (UnicodeEncodeError, UnicodeDecodeError):
|
||||
print >>stderr, fmt % (quote_output(name), quote_output(aliases[name]))
|
||||
print >>stderr, json.dumps(data, indent=4)
|
||||
rc = 1
|
||||
else:
|
||||
for name, details in data.items():
|
||||
dircap = details['readonly'] if options['readonly-uri'] else details['readwrite']
|
||||
try:
|
||||
print >>stdout, fmt % (unicode_to_output(name), unicode_to_output(dircap.decode('utf-8')))
|
||||
except (UnicodeEncodeError, UnicodeDecodeError):
|
||||
print >>stderr, fmt % (quote_output(name), quote_output(dircap))
|
||||
rc = 1
|
||||
|
||||
if rc == 1:
|
||||
print >>stderr, "\nThis listing included aliases or caps that could not be converted to the terminal" \
|
||||
|
104
src/allmydata/test/cli/test_alias.py
Normal file
104
src/allmydata/test/cli/test_alias.py
Normal file
@ -0,0 +1,104 @@
|
||||
import json
|
||||
from mock import patch
|
||||
|
||||
from twisted.trial import unittest
|
||||
from twisted.internet.defer import inlineCallbacks
|
||||
|
||||
from allmydata.util.encodingutil import unicode_to_argv
|
||||
from allmydata.scripts.common import get_aliases
|
||||
from allmydata.test.no_network import GridTestMixin
|
||||
from .common import CLITestMixin
|
||||
|
||||
|
||||
# see also test_create_alias
|
||||
|
||||
|
||||
class ListAlias(GridTestMixin, CLITestMixin, unittest.TestCase):
|
||||
|
||||
@inlineCallbacks
|
||||
def test_list(self):
|
||||
self.basedir = "cli/ListAlias/test_list"
|
||||
self.set_up_grid(oneshare=True)
|
||||
|
||||
rc, stdout, stderr = yield self.do_cli(
|
||||
"create-alias",
|
||||
unicode_to_argv(u"tahoe"),
|
||||
)
|
||||
|
||||
self.failUnless(unicode_to_argv(u"Alias 'tahoe' created") in stdout)
|
||||
self.failIf(stderr)
|
||||
aliases = get_aliases(self.get_clientdir())
|
||||
self.failUnless(u"tahoe" in aliases)
|
||||
self.failUnless(aliases[u"tahoe"].startswith("URI:DIR2:"))
|
||||
|
||||
rc, stdout, stderr = yield self.do_cli("list-aliases", "--json")
|
||||
|
||||
self.assertEqual(0, rc)
|
||||
data = json.loads(stdout)
|
||||
self.assertIn(u"tahoe", data)
|
||||
data = data[u"tahoe"]
|
||||
self.assertIn("readwrite", data)
|
||||
self.assertIn("readonly", data)
|
||||
|
||||
@inlineCallbacks
|
||||
def test_list_unicode_mismatch_json(self):
|
||||
"""
|
||||
pretty hack-y test, but we want to cover the 'except' on Unicode
|
||||
errors paths and I can't come up with a nicer way to trigger
|
||||
this
|
||||
"""
|
||||
self.basedir = "cli/ListAlias/test_list_unicode_mismatch_json"
|
||||
self.set_up_grid(oneshare=True)
|
||||
|
||||
rc, stdout, stderr = yield self.do_cli(
|
||||
"create-alias",
|
||||
unicode_to_argv(u"tahoe\u263A"),
|
||||
)
|
||||
|
||||
self.failUnless(unicode_to_argv(u"Alias 'tahoe\u263A' created") in stdout)
|
||||
self.failIf(stderr)
|
||||
|
||||
booms = []
|
||||
|
||||
def boom(out, indent=4):
|
||||
if not len(booms):
|
||||
booms.append(out)
|
||||
raise UnicodeEncodeError("foo", u"foo", 3, 5, "foo")
|
||||
return str(out)
|
||||
|
||||
with patch("allmydata.scripts.tahoe_add_alias.json.dumps", boom):
|
||||
aliases = get_aliases(self.get_clientdir())
|
||||
self.failUnless(u"tahoe\u263A" in aliases)
|
||||
self.failUnless(aliases[u"tahoe\u263A"].startswith("URI:DIR2:"))
|
||||
|
||||
rc, stdout, stderr = yield self.do_cli("list-aliases", "--json")
|
||||
|
||||
self.assertEqual(1, rc)
|
||||
self.assertIn("could not be converted", stderr)
|
||||
|
||||
@inlineCallbacks
|
||||
def test_list_unicode_mismatch(self):
|
||||
self.basedir = "cli/ListAlias/test_list_unicode_mismatch"
|
||||
self.set_up_grid(oneshare=True)
|
||||
|
||||
rc, stdout, stderr = yield self.do_cli(
|
||||
"create-alias",
|
||||
unicode_to_argv(u"tahoe\u263A"),
|
||||
)
|
||||
|
||||
def boom(out):
|
||||
print("boom {}".format(out))
|
||||
return out
|
||||
raise UnicodeEncodeError("foo", u"foo", 3, 5, "foo")
|
||||
|
||||
with patch("allmydata.scripts.tahoe_add_alias.unicode_to_output", boom):
|
||||
self.failUnless(unicode_to_argv(u"Alias 'tahoe\u263A' created") in stdout)
|
||||
self.failIf(stderr)
|
||||
aliases = get_aliases(self.get_clientdir())
|
||||
self.failUnless(u"tahoe\u263A" in aliases)
|
||||
self.failUnless(aliases[u"tahoe\u263A"].startswith("URI:DIR2:"))
|
||||
|
||||
rc, stdout, stderr = yield self.do_cli("list-aliases")
|
||||
|
||||
self.assertEqual(1, rc)
|
||||
self.assertIn("could not be converted", stderr)
|
Loading…
x
Reference in New Issue
Block a user