Skip to content

Commit eea4bd2

Browse files
author
Clark Perkins
committed
First shot at click
1 parent 597cd42 commit eea4bd2

File tree

2 files changed

+145
-28
lines changed

2 files changed

+145
-28
lines changed

setup.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
# limitations under the License.
1616
#
1717

18+
from __future__ import unicode_literals
19+
1820
import os
1921
import sys
2022

@@ -49,8 +51,9 @@ def test_python_version():
4951
requirements = [
5052
'Jinja2==2.7.3',
5153
'PyYAML==3.11',
52-
'cmd2==0.6.7',
54+
'click>=6.0,<7.0',
5355
'keyring==3.7',
56+
'readline',
5457
'requests>=2.4.0,<2.6.0',
5558
'simplejson==3.4.0',
5659
]
@@ -64,8 +67,6 @@ def test_python_version():
6467
if __name__ == "__main__":
6568
test_python_version()
6669

67-
# Call the setup method from setuptools that does all the heavy lifting
68-
# of packaging stackdio-client
6970
setup(
7071
name='stackdio',
7172
version=__version__,
@@ -100,7 +101,6 @@ def test_python_version():
100101
classifiers=[
101102
'Development Status :: 4 - Beta',
102103
'Environment :: Web Environment',
103-
'Framework :: Django',
104104
'Intended Audience :: Developers',
105105
'Intended Audience :: Information Technology',
106106
'Intended Audience :: System Administrators',

stackdio/cli/__init__.py

Lines changed: 141 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,136 @@
22

33
from __future__ import print_function
44

5-
import argparse
65
import json
76
import os
87
import sys
8+
from cmd import Cmd
99

10+
import click
1011
import keyring
11-
from cmd2 import Cmd
1212
from requests import ConnectionError
1313

1414
from stackdio.cli import mixins
1515
from stackdio.client import StackdIO
16+
from stackdio.client.version import __version__
1617

1718

18-
class StackdioShell(Cmd, mixins.bootstrap.BootstrapMixin, mixins.stacks.StackMixin,
19+
CONTEXT_SETTINGS = dict(help_option_names=['-h', '--help'])
20+
21+
CFG_DIR = os.path.expanduser("~/.stackdio-cli/")
22+
CFG_FILE = os.path.join(CFG_DIR, "config.json")
23+
KEYRING_SERVICE = "stackdio_cli"
24+
25+
26+
def get_client():
27+
if not os.path.isfile(CFG_FILE):
28+
click.echo('It looks like you haven\'t used this CLI before. Please run '
29+
'`stackdio-cli configure`'.format(sys.argv[0]))
30+
sys.exit(1)
31+
32+
config = json.load(open(CFG_FILE, 'r'))
33+
config['blueprint_dir'] = os.path.expanduser(config.get('blueprint_dir', ''))
34+
35+
return StackdIO(
36+
base_url=config["url"],
37+
auth=(
38+
config["username"],
39+
keyring.get_password(KEYRING_SERVICE, config.get("username") or "")
40+
),
41+
verify=config.get('verify', True)
42+
)
43+
44+
45+
def get_invoke(ctx, command):
46+
def invoke(self, arg):
47+
return ctx.invoke(command)
48+
return invoke
49+
50+
51+
def get_help(command):
52+
def help(self):
53+
click.echo(command.help)
54+
return help
55+
56+
57+
def get_shell(ctx):
58+
59+
# Make it a new-style class so we can use super!
60+
class StackdioShell(Cmd, object):
61+
62+
def __init__(self):
63+
super(StackdioShell, self).__init__()
64+
65+
prompt = 'stackdio > '
66+
67+
def emptyline(self):
68+
pass
69+
70+
def do_quit(self, arg):
71+
return True
72+
73+
def do_exit(self, arg):
74+
return True
75+
76+
def do_EOF(self, arg):
77+
return True
78+
79+
def get_names(self):
80+
ret = super(StackdioShell, self).get_names()
81+
# We don't want to display
82+
ret.remove('do_EOF')
83+
return ret
84+
85+
for name, command in ctx.command.commands.items():
86+
setattr(StackdioShell, 'do_%s' % name.replace('-', '_'), get_invoke(ctx, command))
87+
88+
if command.help is not None:
89+
setattr(StackdioShell, 'help_%s' % name.replace('-', '_'), get_help(command))
90+
91+
return StackdioShell()
92+
93+
94+
@click.group(context_settings=CONTEXT_SETTINGS, invoke_without_command=True)
95+
@click.version_option(__version__, '-v', '--version')
96+
@click.pass_context
97+
def stackdio(ctx):
98+
ctx.client = get_client()
99+
100+
if ctx.invoked_subcommand is None:
101+
shell = get_shell(ctx)
102+
shell.cmdloop()
103+
104+
105+
@stackdio.group()
106+
def stacks():
107+
pass
108+
109+
110+
@stackdio.group()
111+
def blueprints():
112+
"""
113+
Foo bar
114+
"""
115+
pass
116+
117+
118+
@stackdio.group()
119+
def formulas():
120+
pass
121+
122+
123+
@stackdio.command()
124+
def configure():
125+
pass
126+
127+
128+
@stackdio.command('server-version')
129+
def server_version():
130+
client = get_client()
131+
click.echo('stackdio-server, version {0}'.format(client.get_version()))
132+
133+
134+
class StackdioShell(mixins.bootstrap.BootstrapMixin, mixins.stacks.StackMixin,
19135
mixins.formulas.FormulaMixin, mixins.blueprints.BlueprintMixin):
20136

21137
CFG_DIR = os.path.expanduser("~/.stackdio-cli/")
@@ -30,14 +146,13 @@ class StackdioShell(Cmd, mixins.bootstrap.BootstrapMixin, mixins.stacks.StackMix
30146
"help", "exit", "quit",
31147
]
32148

33-
Cmd.intro = """
34-
######################################################################
35-
s t a c k d . i o
36-
######################################################################
37-
"""
149+
# Cmd.intro = """
150+
# ######################################################################
151+
# s t a c k d . i o
152+
# ######################################################################
153+
# """
38154

39155
def __init__(self):
40-
Cmd.__init__(self)
41156
mixins.bootstrap.BootstrapMixin.__init__(self)
42157
self._load_config()
43158
if 'url' in self.config and self.config['url']:
@@ -186,21 +301,23 @@ def do_account_summary(self, args=None):
186301

187302

188303
def main():
189-
parser = argparse.ArgumentParser(
190-
description="Invoke the stackdio cli")
191-
parser.add_argument("--debug", action="store_true", help="Enable debugging output")
192-
args = parser.parse_args()
193-
194-
# an ugly hack to work around the fact that cmd2 is using optparse to parse
195-
# arguments for the commands; not sure what the "right" fix is, but as long
196-
# as we assume that we don't want any of our arguments to get passed into
197-
# the cmdloop this seems ok
198-
sys.argv = sys.argv[0:1]
199-
200-
shell = StackdioShell()
201-
if args.debug:
202-
shell.debug = True
203-
shell.cmdloop()
304+
stackdio()
305+
306+
# parser = argparse.ArgumentParser(
307+
# description="Invoke the stackdio cli")
308+
# parser.add_argument("--debug", action="store_true", help="Enable debugging output")
309+
# args = parser.parse_args()
310+
#
311+
# # an ugly hack to work around the fact that cmd2 is using optparse to parse
312+
# # arguments for the commands; not sure what the "right" fix is, but as long
313+
# # as we assume that we don't want any of our arguments to get passed into
314+
# # the cmdloop this seems ok
315+
# sys.argv = sys.argv[0:1]
316+
#
317+
# shell = StackdioShell()
318+
# if args.debug:
319+
# shell.debug = True
320+
# shell.cmdloop()
204321

205322

206323
if __name__ == '__main__':

0 commit comments

Comments
 (0)