88from cmd import Cmd
99
1010import click
11- import click_shell
12- import keyring
1311from requests import ConnectionError
1412
15- from stackdio .cli import mixins
13+ from stackdio .cli . mixins import blueprints , bootstrap , formulas , stacks
1614from stackdio .client import StackdioClient
17- from stackdio .client .config import StackdioConfig
1815from stackdio .client .version import __version__
1916
2017
2118CONTEXT_SETTINGS = dict (help_option_names = ['-h' , '--help' ])
2219
2320HIST_FILE = os .path .join (os .path .expanduser ('~' ), '.stackdio-cli' , 'history' )
2421
25- KEYRING_SERVICE = 'stackdio_cli'
2622
27-
28- def load_config (fail_on_misconfigure , section = 'stackdio' ):
29- try :
30- return StackdioConfig (section )
31- except MissingConfigException :
32- if fail_on_misconfigure :
33- click .echo ('It looks like you haven\' t used this CLI before. Please run '
34- '`stackdio-cli configure`' .format (sys .argv [0 ]))
35- sys .exit (1 )
36- else :
37- return None
38-
39-
40- def get_client (config ):
41- return StackdioClient (
42- base_url = config ['url' ],
43- auth = (
44- config ['username' ],
45- keyring .get_password (KEYRING_SERVICE , config .get ('username' ) or '' )
46- ),
47- verify = config .get ('verify' , True )
48- )
49-
50-
51- class StackdioObj (object ):
52-
53- def __init__ (self , ctx , fail_on_misconfigure ):
54- super (StackdioObj , self ).__init__ ()
55- self .config = load_config (fail_on_misconfigure )
56- if self .config :
57- self .client = get_client (self .config )
58-
59-
60- @click_shell .shell (prompt = 'stackdio > ' , intro = 'stackdio shell v{0}' .format (__version__ ),
61- hist_file = HIST_FILE , context_settings = CONTEXT_SETTINGS )
23+ @click .group (context_settings = CONTEXT_SETTINGS )
6224@click .version_option (__version__ , '-v' , '--version' )
6325@click .pass_context
6426def stackdio (ctx ):
65- ctx .obj = StackdioObj (ctx , ctx .invoked_subcommand != 'configure' )
66-
27+ client = StackdioClient ()
28+ if ctx .invoked_subcommand not in ('configure' , None ) and not client .usable ():
29+ raise click .UsageError ('It looks like you haven\' t used this CLI before. Please run '
30+ '`stackdio-cli configure`' )
6731
68- @stackdio .command ()
69- def configure ():
70- config = StackdioConfig (create = True )
32+ # Put the client in the obj
33+ ctx .obj ['client' ] = client
7134
72- config .prompt_for_config ()
7335
74- config .save ()
36+ @stackdio .command ()
37+ @click .pass_obj
38+ def configure (obj ):
39+ client = obj ['client' ]
40+ print ('configuring' )
7541
7642
7743@stackdio .command ('server-version' )
7844@click .pass_obj
7945def server_version (obj ):
80- client = obj . client
46+ client = obj [ ' client' ]
8147 click .echo ('stackdio-server, version {0}' .format (client .get_version ()))
8248
8349
84- class StackdioShell (Cmd , mixins .bootstrap .BootstrapMixin , mixins .stacks .StackMixin ,
85- mixins .formulas .FormulaMixin , mixins .blueprints .BlueprintMixin ):
50+ # Add all our other commands
51+ stackdio .add_command (blueprints .blueprints )
52+
53+
54+ class StackdioShell (Cmd , bootstrap .BootstrapMixin , stacks .StackMixin ,
55+ formulas .FormulaMixin ):
8656
8757 CFG_DIR = os .path .expanduser ("~/.stackdio-cli/" )
8858 CFG_FILE = os .path .join (CFG_DIR , "config.json" )
@@ -109,33 +79,6 @@ def __init__(self):
10979 self ._init_stacks ()
11080 self ._validate_auth ()
11181
112- def preloop (self ):
113- self ._setprompt ()
114-
115- def precmd (self , line ):
116- self ._setprompt ()
117- return line
118-
119- def postloop (self ):
120- print ("\n Goodbye!" )
121-
122- def get_names (self ):
123- if self .validated :
124- return ["do_%s" % c for c in self .HELP_CMDS ]
125- else :
126- return ["do_initial_setup" , "do_help" ]
127-
128- def _init_stacks (self ):
129- """Instantiate a StackdioClient object"""
130- self .stacks = StackdioClient (
131- base_url = self .config ["url" ],
132- auth = (
133- self .config ["username" ],
134- keyring .get_password (self .KEYRING_SERVICE , self .config .get ("username" ) or "" )
135- ),
136- verify = self .config .get ('verify' , True )
137- )
138-
13982 def _load_config (self ):
14083 """Attempt to load config file, otherwise fallback to DEFAULT_CONFIG"""
14184
@@ -213,23 +156,6 @@ def _setprompt(self):
213156 "## Your account is missing the public key, run 'bootstrap' to fix" ,
214157 "red" ))
215158
216- def _print_summary (self , title , components ):
217- num_components = len (components )
218- print ("## {0} {1}{2}" .format (
219- num_components ,
220- title ,
221- "s" if num_components == 0 or num_components > 1 else "" ))
222-
223- for item in components :
224- print ("- Title: {0}\n Description: {1}" .format (
225- item .get ("title" ), item .get ("description" )))
226-
227- if "status_detail" in item :
228- print (" Status Detail: {0}\n " .format (
229- item .get ("status_detail" )))
230- else :
231- print ("" )
232-
233159 def do_account_summary (self , args = None ):
234160 """Get a summary of your account."""
235161 sys .stdout .write ("Polling {0} ... " .format (self .config ["url" ]))
@@ -251,24 +177,9 @@ def do_account_summary(self, args=None):
251177
252178
253179def main ():
180+ # Just run our CLI tool
254181 stackdio (obj = {})
255182
256- # parser = argparse.ArgumentParser(
257- # description="Invoke the stackdio cli")
258- # parser.add_argument("--debug", action="store_true", help="Enable debugging output")
259- # args = parser.parse_args()
260- #
261- # # an ugly hack to work around the fact that cmd2 is using optparse to parse
262- # # arguments for the commands; not sure what the "right" fix is, but as long
263- # # as we assume that we don't want any of our arguments to get passed into
264- # # the cmdloop this seems ok
265- # sys.argv = sys.argv[0:1]
266- #
267- # shell = StackdioShell()
268- # if args.debug:
269- # shell.debug = True
270- # shell.cmdloop()
271-
272183
273184if __name__ == '__main__' :
274185 main ()
0 commit comments