Fix LP#1004793 : Console: Enable use of connect in non-interactive mode

This commit is contained in:
Calum Lind 2012-06-27 23:51:43 +01:00
parent d1f2776463
commit 313a04f9a6
2 changed files with 23 additions and 12 deletions

View File

@ -55,8 +55,9 @@ class Command(BaseCommand):
def do_connect(): def do_connect():
d = client.connect(host, port, username, password) d = client.connect(host, port, username, password)
def on_connect(result): def on_connect(result):
self.console.write("{!success!}Connected to %s:%s!" % (host, port)) if self.console.interactive:
component.start() self.console.write("{!success!}Connected to %s:%s" % (host, port))
return component.start()
def on_connect_fail(result): def on_connect_fail(result):
try: try:
@ -64,6 +65,7 @@ class Command(BaseCommand):
except: except:
msg = result.value.args[0] msg = result.value.args[0]
self.console.write("{!error!}Failed to connect to %s:%s with reason: %s" % (host, port, msg)) self.console.write("{!error!}Failed to connect to %s:%s with reason: %s" % (host, port, msg))
return result
d.addCallback(on_connect) d.addCallback(on_connect)
d.addErrback(on_connect_fail) d.addErrback(on_connect_fail)
@ -71,7 +73,8 @@ class Command(BaseCommand):
if client.connected(): if client.connected():
def on_disconnect(result): def on_disconnect(result):
do_connect() self.console.statusbars.update_statusbars()
client.disconnect().addCallback(on_disconnect) return do_connect()
return client.disconnect().addCallback(on_disconnect)
else: else:
do_connect() return do_connect()

View File

@ -160,10 +160,11 @@ class ConsoleUI(component.Component):
# Set the interactive flag to indicate where we should print the output # Set the interactive flag to indicate where we should print the output
self.interactive = True self.interactive = True
if args: if args:
args = ' '.join(args) # Multiple commands split by ";"
commands = [arg.strip() for arg in ' '.join(args).split(';')]
self.interactive = False self.interactive = False
# Try to connect to the localhost daemon # Try to connect to the daemon (localhost by default)
def on_connect(result): def on_connect(result):
def on_started(result): def on_started(result):
if not self.interactive: if not self.interactive:
@ -172,9 +173,7 @@ class ConsoleUI(component.Component):
return self.do_command(cmd) return self.do_command(cmd)
d = defer.succeed(None) d = defer.succeed(None)
# If we have args, lets process them and quit # If we have commands, lets process them, then quit.
# allow multiple commands split by ";"
commands = [arg.strip() for arg in args.split(';')]
for command in commands: for command in commands:
d.addCallback(do_command, command) d.addCallback(do_command, command)
@ -187,8 +186,17 @@ class ConsoleUI(component.Component):
component.start().addCallback(on_started) component.start().addCallback(on_started)
def on_connect_fail(result): def on_connect_fail(result):
pass if not self.interactive:
d = client.connect() self.do_command('quit')
connect_cmd = 'connect'
if not self.interactive:
if commands[0].startswith(connect_cmd):
connect_cmd = commands.pop(0)
elif 'help' in commands:
self.do_command('help')
return
d = self.do_command(connect_cmd)
d.addCallback(on_connect) d.addCallback(on_connect)
d.addErrback(on_connect_fail) d.addErrback(on_connect_fail)