Skip to content

Commit 4421f2b

Browse files
committed
CLOUDSTACK-1708: Let cloudmonkey accept cfg passed in cmd line
The patch adds feature in cloudmonkey to have multiple profiles by passing custom cfg file to set custom profile in both interactive shell and cmd line tool use cases. Signed-off-by: Rohit Yadav <bhaisaab@apache.org>
1 parent e28aa09 commit 4421f2b

1 file changed

Lines changed: 39 additions & 8 deletions

File tree

tools/cli/cloudmonkey/cloudmonkey.py

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@
2929
import types
3030

3131
from cachemaker import loadcache, savecache, monkeycache, splitverbsubject
32-
from config import __version__, cache_file
33-
from config import read_config, write_config
32+
from config import __version__, __description__, __projecturl__
33+
from config import read_config, write_config, config_file
34+
from optparse import OptionParser
3435
from prettytable import PrettyTable
3536
from printer import monkeyprint
3637
from requester import monkeyrequest
@@ -63,13 +64,14 @@ class CloudMonkeyShell(cmd.Cmd, object):
6364
intro = ("☁ Apache CloudStack 🐵 cloudmonkey " + __version__ +
6465
". Type help or ? to list commands.\n")
6566
ruler = "="
66-
cache_file = cache_file
6767
config_options = []
6868
verbs = []
6969

70-
def __init__(self, pname):
70+
def __init__(self, pname, cfile):
7171
self.program_name = pname
72-
self.config_options = read_config(self.get_attr, self.set_attr)
72+
self.config_file = cfile
73+
self.config_options = read_config(self.get_attr, self.set_attr,
74+
self.config_file)
7375
self.loadcache()
7476
self.prompt = self.prompt.strip() + " " # Cosmetic fix for prompt
7577

@@ -364,7 +366,7 @@ def do_set(self, args):
364366
key, value = (args[0], args[2])
365367
setattr(self, key, value) # keys and attributes should have same names
366368
self.prompt = self.prompt.strip() + " " # prompt fix
367-
write_config(self.get_attr)
369+
write_config(self.get_attr, self.config_file)
368370

369371
def complete_set(self, text, line, begidx, endidx):
370372
mline = line.partition(" ")[2]
@@ -458,10 +460,39 @@ def do_quit(self, args):
458460
return self.do_EOF(args)
459461

460462

463+
class MonkeyParser(OptionParser):
464+
def format_help(self, formatter=None):
465+
if formatter is None:
466+
formatter = self.formatter
467+
result = []
468+
if self.usage:
469+
result.append("Usage: cloudmonkey [options] [cmds] [params]\n\n")
470+
if self.description:
471+
result.append(self.format_description(formatter) + "\n")
472+
result.append(self.format_option_help(formatter))
473+
result.append("\nTry cloudmonkey [help|?]\n")
474+
return "".join(result)
475+
476+
461477
def main():
462-
shell = CloudMonkeyShell(sys.argv[0])
478+
parser = MonkeyParser()
479+
parser.add_option("-c", "--config-file",
480+
dest="cfile", default=config_file,
481+
help="config file for cloudmonkey", metavar="FILE")
482+
parser.add_option("-v", "--version",
483+
action="store_true", dest="version", default=False,
484+
help="prints cloudmonkey version information")
485+
486+
(options, args) = parser.parse_args()
487+
print 'args', args
488+
print 'options', options
489+
if options.version:
490+
print "cloudmonkey", __version__
491+
print __description__, "(%s)" % __projecturl__
492+
493+
shell = CloudMonkeyShell(sys.argv[0], options.cfile)
463494
if len(sys.argv) > 1:
464-
shell.onecmd(' '.join(sys.argv[1:]))
495+
shell.onecmd(' '.join(args))
465496
else:
466497
shell.cmdloop()
467498

0 commit comments

Comments
 (0)