# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function

import click

from polyaxon_cli.managers.config import GlobalConfigManager


def validate_options(ctx, param, value):
    possible_values = ['verbose', 'host', 'working_directory']
    if value and value not in possible_values:
        raise click.BadParameter(
            "Value `{}` is not supported, must one of the value {}".format(value, possible_values))
    return value


@click.group(invoke_without_command=True)
@click.option('--list', '-l', is_flag=True, help='List all global config values.')
@click.option('--get', callback=validate_options,
              help='Get a specific global config value, e.g. polyaxon config --get host')
def config(list, get):
    """Command for setting and getting global configurations.

    Example:

    ```
    polyaxon config --get host
    ```

    ```
    polyaxon set --host http://...
    ```
    """
    if list:
        config = GlobalConfigManager.get_config()
        click.echo(config.to_dict())

    if get:
        v = GlobalConfigManager.get_value(get)
        click.echo(v)


@config.command()
@click.option('--verbose', type=bool, help='To set the verbosity of the client')
@click.option('--host', type=str, help='To set the server endpoint')
@click.option('--working_directory', type=click.Path(exists=True),
              help='To set the working directory')
def set(verbose, host, working_directory):
    """Command for setting global config values."""
    config = GlobalConfigManager.get_config() or GlobalConfigManager.CONFIG()
    if verbose is not None:
        config.verbose = verbose

    if host is not None:
        config.host = host

    if working_directory is not None:
        config.working_directory = working_directory

    GlobalConfigManager.set_config(config)

@@ -245,29 +245,8 @@
 ost'
-, 'working_directory'
 %5D%0A  
@@ -572,148 +572,8 @@
 .')%0A
-@click.option('--get', callback=validate_options,%0A              help='Get a specific global config value, e.g. polyaxon config --get host')%0A
 def 
@@ -587,13 +587,8 @@
 list
-, get
 ):%0A 
@@ -875,87 +875,550 @@
 ))%0A%0A
-    if get:%0A        v = GlobalConfigManager.get_value(get)%0A        click.echo(v
+%0A@config.command()%0A@click.argument('keys', type=str, nargs=-1)%0Adef get(keys):%0A    %22%22%22Command for getting global config values by keys.%0A%0A    e.g. polyaxon config get host http_port%0A    %22%22%22%0A    config = GlobalConfigManager.get_config() or GlobalConfigManager.CONFIG()%0A    if len(keys) == 0:%0A        return%0A%0A    print_values = %7B%7D%0A    for key in keys:%0A        if hasattr(config, key):%0A            print_values%5Bkey%5D = getattr(config, key)%0A        else:%0A            click.echo('Key %60%7B%7D%60 is not recognised.'.format(key))%0A%0A    click.echo(print_values
 )%0A%0A%0A
@@ -1606,33 +1606,91 @@
 ('--
-working_directory
+http_port', type=int, help='To set the http port')%0A@click.option('--ws_port
 ', type=
 clic
@@ -1689,46 +1689,83 @@
 ype=
-click.Path(exists=True),%0A             
+int, help='To set the stream port')%0A@click.option('--use_https', type=bool,
  hel
@@ -1782,25 +1782,13 @@
 the 
-working directory
+https
 ')%0Ad
@@ -1809,33 +1809,45 @@
 , host, 
-working_directory
+http_port, ws_port, use_https
 ):%0A    %22
@@ -1889,16 +1889,81 @@
  values.
+%0A%0A    e.g. polyaxon config set --hots=localhost http_port=80%0A    
 %22%22%22%0A    
@@ -2032,24 +2032,25 @@
 er.CONFIG()%0A
+%0A
     if verbo
@@ -2159,33 +2159,91 @@
 %0A    if 
-working_directory
+http_port is not None:%0A        config.http_port = http_port%0A%0A    if ws_port
  is not 
@@ -2268,44 +2268,92 @@
 ig.w
-orking_directory
+s_port
  = w
-orking_directory
+s_port%0A%0A    if use_https is not None:%0A        config.use_https = use_https
 %0A%0A  

Update config commands