Skip to content

Commit

Permalink
Add ability to skip some fields in table outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxbey committed Sep 12, 2017
1 parent b687644 commit dd72b50
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 32 deletions.
2 changes: 2 additions & 0 deletions termius/core/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class AbstractCommand(PasswordPromptMixin, Command):
get_strategy = GetStrategy
delete_strategy = SoftDeleteStrategy

skip_fields = ['remote_instance']

def __init__(self, app, app_args, cmd_name=None):
"""Construct new command."""
super(AbstractCommand, self).__init__(app, app_args, cmd_name)
Expand Down
26 changes: 15 additions & 11 deletions termius/core/commands/mixins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
"""Module with different CLI commands mixins."""
import getpass
import os
from operator import attrgetter
from functools import partial
from cached_property import cached_property
Expand Down Expand Up @@ -77,7 +78,7 @@ def prepare_fields(self):

def prepare_result(self, found_list):
"""Return tuple with data in format for Lister."""
fields = self.prepare_fields
fields = sorted(list(set(self.prepare_fields) - set(self.skip_fields)))
getter = DefaultAttrGetter(*fields)
return fields, [getter(i) for i in found_list]

Expand Down Expand Up @@ -187,7 +188,7 @@ def create_instance(self, args):
saved_instance = self.storage.save(instance)
instance.id = saved_instance.id
self.update_children(instance, args)
self.log_create()
self.log_create(saved_instance)

def update_instance(self, args, instance):
"""Update model entry."""
Expand All @@ -196,7 +197,7 @@ def update_instance(self, args, instance):
self.pre_save(instance)
self.storage.save(instance)
self.update_children(instance, args)
self.log_update()
self.log_update(instance)

# pylint: disable=no-self-use,unused-argument
def pre_save(self, instance):
Expand All @@ -214,21 +215,24 @@ def delete_instance(self, instance):
"""Delete model entry."""
with self.storage:
self.storage.delete(instance)
self.log_delete()
self.log_delete(instance)

def log_create(self):
def log_create(self, entry):
"""Log creating new model entry."""
self._general_log('Entry created.')
self._general_log(entry, 'Entry created.')

def log_update(self):
def log_update(self, entry):
"""Log updating model entry."""
self._general_log('Entry updated.')
self._general_log(entry, 'Entry updated.')

def log_delete(self):
def log_delete(self, entry):
"""Log deleting model entry."""
self._general_log('Entry deleted.')
self._general_log(entry, 'Entry deleted.')

def _general_log(self, entry, message):
if os.getenv('TERMIUS_CLI_DEBUG'):
self.app.stdout.write('{}\n'.format(entry.id))

def _general_log(self, message):
self.log.info(message)


Expand Down
2 changes: 1 addition & 1 deletion termius/handlers/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from operator import attrgetter
from cached_property import cached_property
from ..core.commands import DetailCommand, ListCommand
from ..core.commands.mixins import GroupStackGetterMixin, SshConfigPrepareMixin
from ..core.commands.mixins import GroupStackGetterMixin
from ..core.models.terminal import Group
from ..core.commands.single import RequiredOptions
from ..core.storage.strategies import RelatedGetStrategy
Expand Down
4 changes: 3 additions & 1 deletion termius/handlers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@ def get_ssh_key_field(self, args):
'You can not use ssh key and identity file together!'
)
if args.identity_file:
return self.generate_ssh_key_instance(args.identity_file)
return self.generate_ssh_key_instance(
args.identity_file, self.storage
)
if args.ssh_key:
return self.get_safely_instance(SshKey, args.ssh_key)

Expand Down
2 changes: 1 addition & 1 deletion termius/handlers/ssh_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def fields(self):
def get_ssh_key_field(self, args):
"""Create ssh key instance from args."""
return args.identity_file and self.generate_ssh_key_instance(
args.identity_file
args.identity_file, self.command.storage
)

# pylint: disable=no-self-use
Expand Down
10 changes: 5 additions & 5 deletions termius/handlers/ssh_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ class SshKeyGeneratorMixin(object):
"""Mixin for create new ssh key from file."""

# pylint: disable=no-self-use
def generate_ssh_key_instance(self, path):
def generate_ssh_key_instance(self, path, storage):
"""Generate ssh key from file."""
private_key_path = Path(path)
instance = SshKey(
private_key=private_key_path.read_text(),
label=private_key_path.name
)
self.validate_ssh_key(instance)
self.validate_ssh_key(instance, storage)
return instance

def validate_ssh_key(self, instance):
def validate_ssh_key(self, instance, storage):
"""Raise an error when any instances exist with same label."""
with_same_label = self.command.storage.filter(
with_same_label = storage.filter(
SshKey, **{'label': instance.label, 'id.ne': instance.id}
)
if with_same_label:
Expand Down Expand Up @@ -63,7 +63,7 @@ def get_private_key(self, args):

def validate(self, instance):
"""Raise an error when any instances exist with same label."""
self.validate_ssh_key(instance)
self.validate_ssh_key(instance, self.storage)


class SshKeysCommand(ListCommand):
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/group.bats
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ setup() {

@test "Add group to main group" {
group=$(termius group -L 'test group' --port 2 --username 'use r name')
run termius group --port 22 --parent-group $group
run termius group --port 22 --parent-group $group -L subgroup
[ "$status" -eq 0 ]
[ $(get_models_set_length 'group_set') -eq 2 ]
[ $(get_models_set_length 'sshconfig_set') -eq 2 ]
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/host.bats
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ setup() {
}

@test "Add host to group" {
group=$(termius group --port 2022)
group=$(termius group --port 2022 -L group)
run termius host -L test --group $group --address localhost --debug
[ "$status" -eq 0 ]
[ $(get_models_set_length 'host_set') -eq 1 ]
Expand Down Expand Up @@ -79,7 +79,7 @@ setup() {
}

@test "Update host add to group" {
group=$(termius group --port 2022)
group=$(termius group --port 2022 -L group)
host=$(termius host --address localhost -L test)
run termius host --group $group $host
[ "$status" -eq 0 ]
Expand Down
16 changes: 8 additions & 8 deletions tests/integration/hosts.bats
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ setup() {
}

@test "List hosts in a group" {
group=$(termius group --port 2022)
group=$(termius group --port 2022 -L group)
termius host -L test --address localhost --username root --password password
host_id=$(termius host -L test --group $group --address localhost --username root --password password)

Expand All @@ -47,7 +47,7 @@ setup() {
}

@test "List hosts in the root group" {
group=$(termius group --port 2022)
group=$(termius group --port 2022 -L group)
host_id=$(termius host -L test --group $group --address localhost --username root --password password)

run termius hosts -f csv -c id
Expand All @@ -59,8 +59,8 @@ setup() {
}

@test "List hosts in child groups, too" {
group=$(termius group --port 2022)
child_group=$(termius group --parent-group $group --port 2022)
group=$(termius group --port 2022 -L group)
child_group=$(termius group --parent-group $group --port 2022 -L subgroup)
termius host -L test --address localhost --username root --password password
host_id=$(termius host -L test --group $child_group --address localhost --username root --password password)

Expand All @@ -73,8 +73,8 @@ setup() {
}

@test "List hosts only in child groups" {
group=$(termius group --port 2022)
child_group=$(termius group --parent-group $group --port 2022)
group=$(termius group --port 2022 -L group)
child_group=$(termius group --parent-group $group --port 2022 -L subgroup)
termius host -L test --address localhost --username root --password password
termius host -L test --group $group --address localhost --username root --password password
host_id=$(termius host -L test --group $child_group --address localhost --username root --password password)
Expand All @@ -88,7 +88,7 @@ setup() {
}

@test "List hosts in a group filter by the tag" {
group=$(termius group --port 2022)
group=$(termius group --port 2022 -L group)
host_id=$(termius host -L test --group $group --address localhost --username root --password password -t A)
termius host -L test --address localhost --username root --password password -t A

Expand All @@ -100,7 +100,7 @@ setup() {
}

@test "List hosts filter by a tag acrous all gropus" {
group=$(termius group --port 2022)
group=$(termius group --port 2022 -L group)
host_id=$(termius host -L test --group $group --address localhost --username root --password password -t A)

run termius hosts --tag A -f csv -c id
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/info.bats
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ setup() {

@test "info host in 2 groups with visible identity" {
termius settings --agent-forwarding yes
identity=$(termius identity --username user)
identity=$(termius identity --username user -L identity)
grandgroup=$(termius group -L test --port 22 --identity $identity)
group=$(termius group --parent-group $grandgroup -L test --port 2022 --username local)
host=$(termius host --group $group --address localhost -L test --username root)
Expand All @@ -93,7 +93,7 @@ setup() {

@test "info host in 2 groups with visible identity without agent forwarding" {
termius settings --agent-forwarding no
identity=$(termius identity --username user)
identity=$(termius identity --username user -L identity)
grandgroup=$(termius group -L test --port 22 --identity $identity)
group=$(termius group --parent-group $grandgroup -L test --port 2022 --username local)
host=$(termius host --group $group --address localhost -L test --username root)
Expand Down
1 change: 1 addition & 0 deletions tests/integration/snippet.bats
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ setup() {
run termius snippet --script 'cd /' $snippet
[ "$status" -eq 0 ]
[ $(get_models_set_length 'snippet_set') -eq 1 ]

[ "$(get_model_field 'snippet_set' $snippet 'label')" = '"test"' ]
[ "$(get_model_field 'snippet_set' $snippet 'script')" = "\"cd /\"" ]
}
Expand Down

0 comments on commit dd72b50

Please sign in to comment.