22
33from __future__ import print_function
44
5- import json
65import os
7- import sys
8- from cmd import Cmd
96
107import click
11- from requests import ConnectionError
128
139from stackdio .cli .mixins import blueprints , bootstrap , formulas , stacks
1410from stackdio .client import StackdioClient
@@ -36,13 +32,19 @@ def stackdio(ctx):
3632@stackdio .command ()
3733@click .pass_obj
3834def configure (obj ):
35+ """
36+ Configure the client
37+ """
3938 client = obj ['client' ]
4039 print ('configuring' )
4140
4241
4342@stackdio .command ('server-version' )
4443@click .pass_obj
4544def server_version (obj ):
45+ """
46+ Print the version of the server
47+ """
4648 client = obj ['client' ]
4749 click .echo ('stackdio-server, version {0}' .format (client .get_version ()))
4850
@@ -52,131 +54,6 @@ def server_version(obj):
5254stackdio .add_command (stacks .stacks )
5355
5456
55- class StackdioShell (Cmd , bootstrap .BootstrapMixin , stacks .StackMixin ,
56- formulas .FormulaMixin ):
57-
58- CFG_DIR = os .path .expanduser ("~/.stackdio-cli/" )
59- CFG_FILE = os .path .join (CFG_DIR , "config.json" )
60- BOOTSTRAP_FILE = os .path .join (CFG_DIR , "bootstrap.yaml" )
61- KEYRING_SERVICE = "stackdio_cli"
62- PROMPT = "\n {username} @ {url}\n > "
63- HELP_CMDS = [
64- "account_summary" ,
65- "stacks" , "blueprints" , "formulas" ,
66- "initial_setup" , "bootstrap" ,
67- "help" , "exit" , "quit" ,
68- ]
69-
70- intro = """
71- ######################################################################
72- s t a c k d . i o
73- ######################################################################
74- """
75-
76- def __init__ (self ):
77- mixins .bootstrap .BootstrapMixin .__init__ (self )
78- self ._load_config ()
79- if 'url' in self .config and self .config ['url' ]:
80- self ._init_stacks ()
81- self ._validate_auth ()
82-
83- def _load_config (self ):
84- """Attempt to load config file, otherwise fallback to DEFAULT_CONFIG"""
85-
86- try :
87- self .config = json .loads (open (self .CFG_FILE ).read ())
88- self .config ['blueprint_dir' ] = os .path .expanduser (self .config .get ('blueprint_dir' , '' ))
89-
90- except ValueError :
91- print (self .colorize (
92- "What happened?! The config file is not valid JSON. A "
93- "re-install is likely the easiest fix." , "red" ))
94- raise
95- except IOError :
96- self .config = {
97- 'url' : None ,
98- 'username' : None ,
99- }
100- print (self .colorize (
101- "It seems like this is your first time using the CLI. Please run "
102- "'initial_setup' to configure." , "green" ))
103- # print(self.colorize(
104- # "What happened?! Unable to find a config file. A re-install "
105- # "is likely the easiest fix.", "red"))
106- # raise
107-
108- self .has_public_key = None
109-
110- def _validate_auth (self ):
111- """Verify that we can connect successfully to the api"""
112-
113- # If there is no config, just force the user to go through initial setup
114- if self .config ['url' ] is None :
115- return
116-
117- try :
118- self .stacks .get_root ()
119- status_code = 200
120- self .validated = (200 <= status_code <= 299 )
121- except ConnectionError :
122- print (self .colorize (
123- "Unable to connect to {0}" .format (self .config ["url" ]),
124- "red" ))
125- raise
126-
127- if self .validated :
128- print (self .colorize (
129- "Config loaded and validated" , "blue" ))
130- self .has_public_key = self .stacks .get_public_key ()
131- else :
132- print (self .colorize (
133- "ERROR: Unable to validate config" , "red" ))
134- self .has_public_key = None
135-
136- def _setprompt (self ):
137-
138- Cmd .prompt = self .colorize (
139- self .PROMPT .format (** self .config ),
140- "blue" )
141-
142- if not self .validated and self .config ['url' ] is not None :
143- print (self .colorize ("""
144- ##
145- ## Unable to validate connection - one of several possibilities exist:
146- ## If this is the first time you've fired this up, you need to run
147- ## 'initial_setup' to configure your account details. If you've already
148- ## done that, there could be a network connection issue anywhere between
149- ## your computer and your stackd.io instance,
150- ## or your password may be incorrect, or ... etc.
151- ##
152- """ ,
153- "green" ))
154-
155- if self .validated and not self .has_public_key :
156- print (self .colorize (
157- "## Your account is missing the public key, run 'bootstrap' to fix" ,
158- "red" ))
159-
160- def do_account_summary (self , args = None ):
161- """Get a summary of your account."""
162- sys .stdout .write ("Polling {0} ... " .format (self .config ["url" ]))
163- sys .stdout .flush ()
164-
165- public_key = self .stacks .get_public_key ()
166- formulas = self .stacks .list_formulas ()
167- blueprints = self .stacks .list_blueprints ()
168- stacks = self .stacks .list_stacks ()
169-
170- sys .stdout .write ("done\n " )
171-
172- print ("## Username: {0}" .format (self .config ["username" ]))
173- print ("## Public Key:\n {0}" .format (public_key ))
174-
175- self ._print_summary ("Formula" , formulas )
176- self ._print_summary ("Blueprint" , blueprints )
177- self ._print_summary ("Stack" , stacks )
178-
179-
18057def main ():
18158 # Just run our CLI tool
18259 stackdio (obj = {})
0 commit comments