Skip to content

Commit

Permalink
Merge pull request #139 from operable/nmohoric/default-profile-command
Browse files Browse the repository at this point in the history
Add command to set default profile
  • Loading branch information
nmohoric authored May 18, 2018
2 parents f6af5a3 + 7da6261 commit bec6647
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 14 deletions.
8 changes: 8 additions & 0 deletions cogctl/cli/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ def add(self, profile_name, profile):

self._config[profile_name] = ordered

def set_default(self, profile_name):
""" Update the default profile. Raise KeyError if no such profile exists
"""
if profile_name not in self.profiles():
raise KeyError("Profile does not exist")

self._config['defaults']['profile'] = profile_name

def write(self):
# We manage the writing ourselves, because the object may have
# been initialized with a file that does not exist. Using
Expand Down
24 changes: 24 additions & 0 deletions cogctl/cli/profile.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import click
import cogctl
from click_didyoumean import DYMGroup


def validate_profile(context, param, value):
"""
Validates existance of profile.
Returns the profile name if it exists; otherwise throws BadParameter
"""
if value in context.obj.configuration.profiles():
return value
else:
raise click.BadParameter("\"%s\" was not found" % value)


@click.group(invoke_without_command=True, cls=DYMGroup)
@click.pass_context
def profile(ctx):
Expand Down Expand Up @@ -43,3 +55,15 @@ def create(state, name, url, user, password):
"user": user,
"password": password})
state.configuration.write()


@profile.command()
@click.argument("name", callback=validate_profile)
@click.pass_obj
@cogctl.error_handler
def default(state, name):
"""
Sets the default profile in the configuration file.
"""
state.configuration.set_default(name)
state.configuration.write()
12 changes: 12 additions & 0 deletions tests/cogctl/cli/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,15 @@ def test_comments_round_trip(config_with_comments):
def test_comment_characters_in_values_can_be_quoted(config_with_comments):
password = config_with_comments.profile("localhost")["password"]
assert password == "sooperseekrit#with_a_hash"


def test_set_default_updates_default(classic_config):
classic_config.set_default("testing")
assert classic_config.default() == {'password': 'testpass',
'url': 'https://cog.testing.com:1234',
'user': 'tester'}


def test_set_default_missing_profile(classic_config):
with pytest.raises(KeyError):
classic_config.set_default("missing")
104 changes: 90 additions & 14 deletions tests/cogctl/cli/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,26 @@ def config_file(cogctl):
with open("config", "w") as f:
f.write("""\
[defaults]
profile=default_profile
profile = default_profile
[default_profile]
host=default_host
password=default_password
port=4000
secure=false
user=default_user
host = default_host
password = default_password
port = 4000
secure = false
user = default_user
[testing]
host=cog.testing.com
password=testpass
port=1234
secure=true
user=tester
host = cog.testing.com
password = testpass
port = 1234
secure = true
user = tester
[new-style]
url=https://cog.newstyle.com:1234
user=new_user
password=new_password
url = https://cog.newstyle.com:1234
user = new_user
password = new_password
""")

return "{}/{}".format(os.getcwd(), "config")
Expand Down Expand Up @@ -109,3 +109,79 @@ def test_add_new_profile(cogctl, config_file):
url = https://myserver.com:1234
user = my_user
"""


def test_change_default_profile(cogctl, config_file):
result = cogctl(["--config-file", config_file,
"profile", "default",
"testing"])

assert result.exit_code == 0
assert result.output == ""

with open(config_file) as f:
contents = f.read()

assert contents == """\
[defaults]
profile = testing
[default_profile]
host = default_host
password = default_password
port = 4000
secure = false
user = default_user
[testing]
host = cog.testing.com
password = testpass
port = 1234
secure = true
user = tester
[new-style]
url = https://cog.newstyle.com:1234
user = new_user
password = new_password
"""


def test_change_default_invalid_profile(cogctl, config_file):
result = cogctl(["--config-file", config_file,
"profile", "default",
"missing"])

assert result.exit_code == 2
assert result.output == """\
Usage: cli profile default [OPTIONS] NAME
Error: Invalid value for \"name\": \"missing\" was not found
"""

with open(config_file) as f:
contents = f.read()

assert contents == """\
[defaults]
profile = default_profile
[default_profile]
host = default_host
password = default_password
port = 4000
secure = false
user = default_user
[testing]
host = cog.testing.com
password = testpass
port = 1234
secure = true
user = tester
[new-style]
url = https://cog.newstyle.com:1234
user = new_user
password = new_password
"""

0 comments on commit bec6647

Please sign in to comment.