commit 362adf5051b2995921e906a5868ec340cb2db8d8
Author: Stavros Sachtouris <saxtouri@admin.grnet.gr>
Date:   Wed Jun 5 19:00:38 2013 +0300

    Decide a cloud term, use it if no cloud.url
    
    Refs: #3934

diff --git a/kamaki/cli/__init__.py b/kamaki/cli/__init__.py
index 1ab283c..f8fb420 100644
--- a/kamaki/cli/__init__.py
+++ b/kamaki/cli/__init__.py
@@ -195,15 +195,17 @@ def _setup_logging(silent=False, debug=False, verbose=False, include=False):
 def _check_config_version(cnf):
     guess = cnf.guess_version()
     if guess < 3.0:
-        print('Configuration file "%s" is not updated to v3.0' % (
+        print('Config file format version >= 3.0 is required')
+        print('Configuration file "%s" format is not up to date' % (
             cnf.path))
-        print('Calculating changes while preserving information ...')
+        print('but kamaki can fix this:')
+        print('Calculating changes while preserving information')
         lost_terms = cnf.rescue_old_file()
-        if lost_terms:
-            print 'The following information will not be preserved:'
-            print '...', '\n... '.join(lost_terms)
         print('... DONE')
-        print('Kamaki is ready to transform config file to version 3.0')
+        if lost_terms:
+            print 'The following information will NOT be preserved:'
+            print '\t', '\n\t'.join(lost_terms)
+        print('Kamaki is ready to convert the config file to version 3.0')
         stdout.write('Overwrite file %s ? [Y, y] ' % cnf.path)
         from sys import stdin
         reply = stdin.readline()
@@ -214,10 +216,12 @@ def _check_config_version(cnf):
             print('... ABORTING')
             raise CLIError(
                 'Invalid format for config file %s' % cnf.path,
-                importance=3, details=['Please, update config file to v3.0'])
+                importance=3, details=[
+                    'Please, update config file to v3.0',
+                    'For automatic conversion, rerun and say Y'])
 
 
-def _init_session(arguments):
+def _init_session(arguments, is_non_API=False):
     global _help
     _help = arguments['help'].value
     global _debug
@@ -228,44 +232,50 @@ def _init_session(arguments):
     _verbose = arguments['verbose'].value
     _cnf = arguments['config']
     _check_config_version(_cnf.value)
-    raise CLIError(
-        'Your file is OK, but i am not ready to proceed',
-        importance=3, details=['DO NOT PANIC, EXIT THE BUILDING QUIETLY'])
 
     global _colors
-    _colors = _cnf.get('global', 'colors')
+    _colors = _cnf.value.get_global('colors')
     if not (stdout.isatty() and _colors == 'on'):
         from kamaki.cli.utils import remove_colors
         remove_colors()
     _silent = arguments['silent'].value
     _setup_logging(_silent, _debug, _verbose, _include)
-    picked_cloud = arguments['cloud'].value
-    if picked_cloud:
-        global_url = _cnf.get('remotes', picked_cloud)
-        if not global_url:
-            raise CLIError(
-                'No remote cloud "%s" in kamaki configuration' % picked_cloud,
-                importance=3, details=[
-                    'To check if this remote cloud alias is declared:',
-                    '  /config get remotes.%s' % picked_cloud,
-                    'To set a remote authentication URI aliased as "%s"' % (
-                        picked_cloud),
-                    '  /config set remotes.%s <URI>' % picked_cloud
-                ])
-    else:
-        global_url = _cnf.get('global', 'auth_url')
-    global_token = _cnf.get('global', 'token')
+
+    if _help or is_non_API:
+        return None
+
+    cloud = arguments['cloud'].value or 'default'
+    if not cloud in _cnf.value.keys('remote'):
+        raise CLIError(
+            'No cloud remote "%s" is configured' % cloud,
+            importance=3, details=[
+                'To configure a new cloud remote, find and set the',
+                'single authentication URL and token:',
+                '  kamaki config set remote.%s.url <URL>' % cloud,
+                '  kamaki config set remote.%s.token <t0k3n>' % cloud])
+    url = _cnf.get_remote(cloud, 'url')
+    if not url:
+        kloger.warning(
+            'WARNING: No remote.%s.url, use service urls instead' % cloud)
+        return cloud
+    token = _cnf.get_remote(cloud, 'token')
+    if not token:
+        raise CLIError(
+            'No authentication token provided for %s cloud' % cloud,
+            importance=3, details=[
+                'Get and set a token for %s cloud:' % cloud,
+                '  kamaki config set remote.%s.token <t0k3n>' % cloud])
+
     from kamaki.clients.astakos import AstakosClient as AuthCachedClient
     try:
-        return AuthCachedClient(global_url, global_token)
+        return AuthCachedClient(url, token)
     except AssertionError as ae:
-        kloger.warning('WARNING: Failed to load auth_url %s [ %s ]' % (
-            global_url, ae))
+        kloger.warning(
+            'WARNING: Failed to load auth_url %s [ %s ]' % (url, ae))
         return None
 
 
 def _load_spec_module(spec, arguments, module):
-    #spec_name = arguments['config'].get('cli', spec)
     if not spec:
         return None
     pkg = None
@@ -304,7 +314,6 @@ def _groups_help(arguments):
 
 def _load_all_commands(cmd_tree, arguments):
     _cnf = arguments['config']
-    #specs = [spec for spec in _cnf.get_groups() if _cnf.get(spec, 'cli')]
     for cmd_group, spec in _cnf.get_cli_specs():
         try:
             spec_module = _load_spec_module(spec, arguments, '_commands')
@@ -411,7 +420,7 @@ def set_command_params(parameters):
 def run_one_cmd(exe_string, parser, auth_base):
     global _history
     _history = History(
-        parser.arguments['config'].get('history', 'file'))
+        parser.arguments['config'].get_global('history_file'))
     _history.add(' '.join([exe_string] + argv[1:]))
     from kamaki.cli import one_command
     one_command.run(auth_base, parser, _help)
@@ -424,6 +433,16 @@ def run_shell(exe_string, parser, auth_base):
     shell.run(auth_base, parser)
 
 
+def is_non_API(parser):
+    nonAPIs = ('history', 'config')
+    for term in parser.unparsed:
+        if not term.startswith('-'):
+            if term in nonAPIs:
+                return True
+            return False
+    return False
+
+
 def main():
     try:
         exe = basename(argv[0])
@@ -432,25 +451,25 @@ def main():
         if parser.arguments['version'].value:
             exit(0)
 
-        log_file = parser.arguments['config'].get('global', 'log_file')
+        log_file = parser.arguments['config'].get_global('log_file')
         if log_file:
             logger.set_log_filename(log_file)
         global filelog
         filelog = logger.add_file_logger(__name__.split('.')[0])
         filelog.info('* Initial Call *\n%s\n- - -' % ' '.join(argv))
 
-        auth_base = _init_session(parser.arguments)
+        remote_base = _init_session(parser.arguments, is_non_API(parser))
 
         from kamaki.cli.utils import suggest_missing
         suggest_missing()
 
         if parser.unparsed:
-            run_one_cmd(exe, parser, auth_base)
+            run_one_cmd(exe, parser, remote_base)
         elif _help:
             parser.parser.print_help()
             _groups_help(parser.arguments)
         else:
-            run_shell(exe, parser, auth_base)
+            run_shell(exe, parser, remote_base)
     except CLIError as err:
         print_error_message(err)
         if _debug:
